Skip to content

Commit

Permalink
Merge pull request #138 from Team-Lecue/feature/Login
Browse files Browse the repository at this point in the history
[ Login ] dev 머지!
  • Loading branch information
doyn511 committed Jan 16, 2024
2 parents 96b0a95 + c8c5575 commit 8905b94
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 12 deletions.
31 changes: 31 additions & 0 deletions src/Login/api/getLoginToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import axios from 'axios';

import {
KAKAO_BASE_URL,
KAKAO_REDIRECT_URI,
KAKAO_REST_API_KEY,
} from './oAuth';

export const getLoginToken = async () => {
const AUTHORIZE_CODE = new URL(window.location.href).searchParams.get('code');
const GRANT_TYPE = 'authorization_code';

if (AUTHORIZE_CODE) {
try {
const response = await axios.post(
`${KAKAO_BASE_URL}/token?grant_type=${GRANT_TYPE}&client_id=${KAKAO_REST_API_KEY}&redirect_uri=${KAKAO_REDIRECT_URI}&code=${AUTHORIZE_CODE}`,
{},
{
headers: {
'Content-type': 'application/x-www-form-urlencoded;charset=utf-8',
},
},
);

return response.data.access_token;
} catch (error) {
console.error('로그인 토큰을 가져오는 중 에러 발생:', error);
throw error;
}
}
};
5 changes: 5 additions & 0 deletions src/Login/api/oAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const KAKAO_REST_API_KEY = import.meta.env.VITE_KAKAO_API_KEY;
export const KAKAO_REDIRECT_URI = import.meta.env.VITE_KAKAO_REDIRECT_URI;
export const KAKAO_BASE_URL = import.meta.env.VITE_KAKAO_BASE_URL;

export const KAKAO_AUTH_URL = `${KAKAO_BASE_URL}/authorize?response_type=code&client_id=${KAKAO_REST_API_KEY}&redirect_uri=${KAKAO_REDIRECT_URI}`;
21 changes: 21 additions & 0 deletions src/Login/api/postLoginToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { api } from '../../libs/api';

export const postLoginToken = async (token: string) => {
const response = await api.post(
'/api/login',
{
socialPlatform: 'KAKAO',
},
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
},
);

return {
nickname: response.data.data.nickname,
tokenDto: response.data.data.tokenDto,
};
};
4 changes: 2 additions & 2 deletions src/Login/components/LoginBtnContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BtnKakaologin, ImgKakaoStarOrange } from '../../../assets';
import { KAKAO_AUTH_URL } from '../../api/oAuth';
import * as S from './LoginBtnContainer.style';

function LoginBtnContainer() {
const handleClickLoginBtn = () => {
//로그인 버튼 클릭 시 이벤트
alert('로그인 버튼 클릭');
window.location.href = KAKAO_AUTH_URL;
};

return (
Expand Down
34 changes: 34 additions & 0 deletions src/Login/components/LoginCallback/LoginCallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

import { getLoginToken } from '../../api/getLoginToken';
import { postLoginToken } from '../../api/postLoginToken';

function LoginCallback() {
const navigate = useNavigate();

useEffect(() => {
const fetchData = async () => {
try {
const tokenRes = await getLoginToken();

const { nickname, tokenDto } = await postLoginToken(tokenRes);

if (nickname === null) {
navigate('/register', { state: { token: tokenDto.accessToken } });
} else {
window.localStorage.setItem('token', tokenDto.accessToken);
window.localStorage.setItem('nickname', nickname);
}
} catch (error) {
console.error('로딩-fetchData() 에러 발생:', error);
}
};

fetchData();
}, []);

return <div>로딩중</div>;
}

export default LoginCallback;
11 changes: 7 additions & 4 deletions src/Register/components/NicknameInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import React, { useEffect, useState } from 'react';

import * as S from './NicknameInput.style';

type setIsActiveProps = {
interface NicknameInputProps {
nickname: string;
setNickname: React.Dispatch<React.SetStateAction<string>>;
setIsActive: React.Dispatch<React.SetStateAction<boolean>>;
};
}

function NicknameInput(props: NicknameInputProps) {
const { nickname, setNickname, setIsActive } = props;

function NicknameInput({ setIsActive }: setIsActiveProps) {
const [wordCnt, setWordCnt] = useState(0);
const [nickname, setNickname] = useState('');

/** 영어, 숫자, 문자, 공백인지 체크하는 정규식 함수 */
const checkInputRange = (str: string) => {
Expand Down
6 changes: 6 additions & 0 deletions src/Register/components/SubmitButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import * as S from './SubmitButton.style';

type SubmitButtonProps = {
isActive: boolean;
token: string;
nickname: string;
};

function SubmitButton({ isActive }: SubmitButtonProps) {
const handelClickSubmitBtn = (token: string, nickname: string) => {
//닉네임 PATCH API 연결하기
};

return (
<S.ButtonWrapper>
<Button variant="complete" disabled={!isActive}>
Expand Down
13 changes: 11 additions & 2 deletions src/Register/page/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from 'react';
import { useLocation } from 'react-router-dom';

import NicknameInput from '../components/NicknameInput';
import RegisterLogo from '../components/RegisterLogo';
Expand All @@ -7,12 +8,20 @@ import * as S from './Register.style';

function Register() {
const [isActive, setIsActive] = useState(false);
const [nickname, setNickname] = useState('');
const location = useLocation();

const { token } = location.state && location.state;

return (
<S.Wrapper>
<RegisterLogo />
<NicknameInput setIsActive={setIsActive} />
<SubmitButton isActive={isActive} />
<NicknameInput
setIsActive={setIsActive}
nickname={nickname}
setNickname={setNickname}
/>
<SubmitButton isActive={isActive} nickname={nickname} token={token} />
</S.Wrapper>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import DetailPage from './Detail/page/DetailPage';
import HealthTest from './HealthTest';
import Home from './Home/page';
import LecueNotePage from './LecueNote/page/LeceuNotePage';
import LoginCallback from './Login/components/LoginCallback/LoginCallback';
import Login from './Login/page';
import Mypage from './Mypage/page';
import Register from './Register/page';
Expand Down Expand Up @@ -47,6 +48,7 @@ function Router() {
<Route path="/create-book" element={<CreateBook />} />
<Route path="/sticker-attach" element={<StickerAttach />} />
<Route path="/select-book" element={<SelectBookPage />} />
<Route path="/loading" element={<LoginCallback />} />
</Routes>
</BrowserRouter>
);
Expand Down
7 changes: 3 additions & 4 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import ReactDOM from 'react-dom/client';

import App from './App.tsx';
Expand All @@ -20,7 +19,7 @@ import App from './App.tsx';
// });

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
// <React.StrictMode>
<App />,
// </React.StrictMode>,
);

0 comments on commit 8905b94

Please sign in to comment.