Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions hustle_web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hustle_web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"react-dom": "^18.2.0",
"react-hook-form": "^7.45.1",
"react-icons": "^4.10.1",
"react-kakao-login": "^2.1.1",
"react-query": "^3.39.3",
"react-router-dom": "^6.15.0",
"react-scripts": "5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion hustle_web/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const Router = () => {
<Route index element={<Home />} />
<Route path='/login' element={<Login />} />
<Route path='/signIn' element={<SignIn />} />
<Route path='/kakaoLogin' element={<KakaoLoginRedirect />}/>
<Route path='/oauth/kakaoLogin' element={<KakaoLoginRedirect />}/>
<Route path='/competitions' element={<Competition />} />
<Route
path='/competitions/apply/:competitionId'
Expand Down
76 changes: 48 additions & 28 deletions hustle_web/src/components/Login/Kakao/KakaoLogin.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,63 @@
import axios from 'axios';
import { useEffect } from 'react';
import { useContext, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { AuthContext } from '../../Auth/AuthProvider';
import UniversitySearch from '../../SignIn/UniversitySearch';

const kakaoLogin = () => {
useEffect(() => {
const code = new URL(window.location.href).searchParams.get('code');
const grantType = 'authorization_code';
const REST_API_KEY = process.env.REACT_API_KEY;
const REDIRECT_URI = process.env.REDIRECT_URI;

axios.post(
`https://kauth.kakao.com/oauth/token?grant_type=${grantType}&client_id=${REST_API_KEY}&redirect_uri=${REDIRECT_URI}&code=${code}`,
{},
{ headers: { 'Content-type': 'application/x-www-form-urlencoded;charset=utf-8' }}
)
const KakaoLoginRedirect = () => {
const navigate = useNavigate();
const { setIsLoggedIn, setAccessToken, refreshAccessToken } = useContext(AuthContext);
const code = new URL(window.location.href).searchParams.get('code');
const [selectedUniversity, setSelectedUniversity] = useState('');

const handleUniversitySelection = (universityId: number) => {
const university = `${universityId}`;
setSelectedUniversity(university);
console.log(selectedUniversity);
};

const handleSubmit = () => {
axios.get(`https://api.sport-hustle.com/api/oauth/kakao/token?code=${code}`)
.then((res) => {
console.log(res);
const accessToken = res.data.accessToken;
axios.post('https://api.sport-hustle.com/api/auth/signin/oauth',
{},
{
console.log('์นด์นด์˜ค ์ •๋ณด : ',res.data);
console.log('์นด์นด์˜ค accessToken : ',res.data.accessToken);
const token = res.data.accessToken;
const postData = {
kakaoInfo : res.data,
university : selectedUniversity, // ์„ ํƒ๋œ ๋Œ€ํ•™๊ต ์•„์ด๋”” ๊ฐ’
};
axios.post('์นด์นด์˜ค ํšŒ์›๊ฐ€์ž… ์ •๋ณด api', postData,{
headers : {
Authorization : `Bearer ${accessToken}`,
'Content-type' : 'application/x-www-form-urlencoded;charset=utf-8',
'Authorization' : `Bearer ${token}`
}
})
.then((res: any) => {
console.log('seconde : ', res.data.accessToken);
.then((res) => {
console.log('ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต : ', res);
setIsLoggedIn(true);
const accessToken = res.data.accessToken;
localStorage.setItem('์นด์นด์˜ค ํ—ˆ์Šฌ ํ† ํฐ', accessToken);
localStorage.setItem('์นด์นด์˜ค ํ—ˆ์Šฌ ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ', res.data.refreshToken);
setAccessToken(accessToken);
navigate('/');
})
}).catch((error) => {
console.log('์นด์นด์˜ค ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ',error);
if(error.res && error.res.status === 400){
refreshAccessToken();
} else {
navigate('/login');
}
})
.catch((error) => {
console.log(error);
})
},[])
}


return (
<div>
๋กœ๊ทธ์ธ ์ค‘์ž…๋‹ˆ๋‹ค. ์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”
<div>์นด์นด์˜ค ์ถ”๊ฐ€ ์ •๋ณด</div>
<UniversitySearch onSelecteUniversity={handleUniversitySelection} />
<button type='submit' onClick={handleSubmit}>ํšŒ์›๊ฐ€์ž…</button>
</div>
);
};

export default kakaoLogin;
export default KakaoLoginRedirect;
20 changes: 8 additions & 12 deletions hustle_web/src/components/Login/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import * as L from './LoginStyle';
import { useNavigate } from 'react-router-dom';
import { LoginProps } from '../../constants/interfaces';
Expand All @@ -7,8 +7,6 @@ import { defaultLoginValue } from '../../constants/defaultFormOption';
import FormRequirements from '../../constants/FormRequirements';
import axios from 'axios';
import { AuthContext } from '../Auth/AuthProvider';
import { userAtom } from '../../recoil/login/login';
import { useRecoilState } from 'recoil';

const { idRequirements, passwordRequirements } = FormRequirements;
const defaultValue = defaultLoginValue;
Expand Down Expand Up @@ -73,14 +71,12 @@ const LoginMain = () => {
}
};

const kakaoLoginHandler = () => {
const REST_API_KEY = process.env.REACT_API_KEY;
const REDIRECT_URL = process.env.REDIRECT_URI;
const kakaoUrl = `https://kauth.kakao.com/oauth/authorize?client_id=${REST_API_KEY}&redirect_uri=${REDIRECT_URL}&response_type=code`

window.location.href = kakaoUrl;

}
function loginwithKakao() {
const REST_API_KEY = '170a8a097251697cc023f05857280065';
const REDIRECT_URL = 'http://localhost:3000/oauth/kakaoLogin';
const kakaoUrl = `https://kauth.kakao.com/oauth/authorize?client_id=${REST_API_KEY}&redirect_uri=${REDIRECT_URL}&response_type=code`;
window.location.href = kakaoUrl;
}


return (
Expand Down Expand Up @@ -116,7 +112,7 @@ const LoginMain = () => {
<L.Line></L.Line>
<L.ButtonDiv>
<L.KakaoButton
onClick={kakaoLoginHandler}
onClick={loginwithKakao}
>์นด์นด์˜ค ๋กœ๊ทธ์ธ</L.KakaoButton>
</L.ButtonDiv>
<L.ButtonDiv>
Expand Down
80 changes: 17 additions & 63 deletions hustle_web/src/components/SignIn/UniversitySearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,60 @@ import { useState, useEffect } from 'react';
import { UniversityProps } from '../../constants/interfaces';
import Select from 'react-select';

// interface UniversitySearchProps {
// onSelectUniversity: (universityName: string) => void;
// }

const UniversitySearch = ({
onSelecteUniversity
}: {
onSelecteUniversity: (UniversityId: number) => void;
}) => {
const [universityList, setUniversityList] = useState<UniversityProps[]>([]);
const [searchQuery, setSearchQuery] = useState(''); // ๊ฒ€์ƒ‰์–ด ์ƒํƒœ ์ถ”๊ฐ€
const [selectedId, setSelectedId] = useState(0);
const [searchQuery,] = useState(''); // ๊ฒ€์ƒ‰์–ด ์ƒํƒœ ์ถ”๊ฐ€
const [selectedUniversity, setSelectedUniversity] = useState<UniversityProps | null>(null);

const handleSearchUniversity = async () => {
// setUniversityList(dummyUniversityData);
try {
const response = await axios.get(
`https://api.sport-hustle.com/api/university?keyword=${searchQuery}`
);
setUniversityList(response.data.universities);
setUniversityList(response.data.data);
} catch (error) {
console.error('๋Œ€ํ•™๊ต ๊ฒ€์ƒ‰ ์˜ค๋ฅ˜:', error);
setUniversityList([]);
}
};

const options = universityList.map((university) => ({
const options = universityList
.filter((university) => typeof university.id === 'number')
.map((university) => ({
value: university.id,
label: university.name
label: university.name,
address : university.address
}));

// const handleUniversitySelection = (universityId: number) => {
// const selectedUniversity = universityList.find(university => university.id === universityId);

// if(selectedUniversity){
// setSearchQuery(selectedUniversity.name); // ์„ ํƒ๋œ ๋Œ€ํ•™๊ต ์ด๋ฆ„์„ ๊ฒ€์ƒ‰์ฐฝ์— ํ‘œ์‹œ
// onSelecteUniversity(selectedUniversity.id); // ์„ ํƒ๋œ ๋Œ€ํ•™๊ต ์ด๋ฆ„์„ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ
// }
// };

const handleUniversitySelection = (selectedOption: any) => {
if (selectedOption) {
setSearchQuery(selectedOption.label); // ์„ ํƒ๋œ ๋Œ€ํ•™๊ต ์ด๋ฆ„์„ ๊ฒ€์ƒ‰์ฐฝ์— ํ‘œ์‹œ
onSelecteUniversity(selectedOption.value); // ์„ ํƒ๋œ ๋Œ€ํ•™๊ต ID๋ฅผ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ
}
setSelectedUniversity(selectedOption);
};

useEffect(() => {
handleSearchUniversity();
});
},[searchQuery]);

useEffect(() => {
if (selectedUniversity !== null) {
onSelecteUniversity(selectedUniversity.id);
}
}, [selectedUniversity, onSelecteUniversity]);


return (
<>
<S.InputLabel>์†Œ์† ๋Œ€ํ•™๊ต</S.InputLabel>
{/* <S.InputLarge
type='university'
placeholder='์žฌํ•™ ์ค‘์ธ ๋Œ€ํ•™๊ต๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”'
value={searchQuery} // ์ž…๋ ฅ์ฐฝ์— ํ‘œ์‹œ๋  ๊ฐ’์€ searchQuery ์ƒํƒœ
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
setSearchQuery(e.target.value);
}}
/> */}
{universityList.length > 0 && (
<Select
options={options}
onChange={handleUniversitySelection}
placeholder='๋Œ€ํ•™๊ต๋ฅผ ์„ ํƒํ•˜์„ธ์š”'
/>
)}
{/* <S.SubmitButton
type='button'
onClick={() => {
handleSearchUniversity();
}} // ๊ฒ€์ƒ‰ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋Œ€ํ•™๊ต ๋ชฉ๋ก ๊ฒ€์ƒ‰
>
๊ฒ€์ƒ‰
</S.SubmitButton> */}

{/* {universityList.length > 0 && (
<S.UniversityList>
{universityList.map((university, id) => (
<div key={id}>
<S.NameText>{university.name}</S.NameText>
<div>
<S.AddressText>{university.address}</S.AddressText>
</div>
<S.RightContainer>
<button
type='button'
onClick={() => {
handleUniversitySelection(university.id);
}}
>
์„ ํƒ
</button>
</S.RightContainer>
</div>
))}
</S.UniversityList>
)} */}
</>
);
};
Expand Down
4 changes: 4 additions & 0 deletions hustle_web/src/constants/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export interface kakaoLoginProps {
onSuccess: string;
}

export interface kakaoSignupProps {
locationaddress: string;
}

export interface AuthContextType {
isLoggedIn: boolean;
setIsLoggedIn: (value: boolean) => void;
Expand Down
1 change: 0 additions & 1 deletion hustle_web/src/recoil/login/loginState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ export const TokenState = atom<string | null>({
default: null,
effects_UNSTABLE: [persistAtom],
});

2 changes: 1 addition & 1 deletion hustle_web/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const routes = [
{ path: '/maincompetition', element: MainCompetition },
{ path: '/maincompetition/apply', element: ApplyCompetition },
{ path: '/ranking', element: RankingPage },
{ path: '/kakaoLogin', element: KakaoLoginRedirect }
{ path: '/oauth/kakaoLogin', element: KakaoLoginRedirect }
];

export default routes;