-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: 카카오 OAuth 로그인 기능 구현 #23
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3aa7f2f
Feat: 카카오 로그인 요청 구현
ParkSohyunee 4e61b92
Chore: api 도메인 환경변수 설정
ParkSohyunee 90b8bbc
Feat: 카카오 로그인 리다이렉트 페이지 구현
ParkSohyunee 2215694
Feat: 카카오 리다이렉트 페이지에서 서버로 최종 로그인 요청 기능 구현
ParkSohyunee 726f8b7
Feat: reactStricMode true 설정에 따른 axios 중복 호출 처리(AbortController 사용)
ParkSohyunee 008ae65
Refactor: 프로미스를 async/await로 구현, 에러 메세지 처리
ParkSohyunee 449ea31
Feat: 사용자 정보 상태관리를 위한 useUser store 생성
ParkSohyunee 2be7651
Feat: useUserStore updateUser 함수 수정, devtools 연동 해제
ParkSohyunee 9afe690
Feat: 카카오 로그인 후, 사용자 id, nickname store에 저장하는 로직 구현
ParkSohyunee 482c5db
Feat: user 타입 파일명 수정 및 타입 프로퍼티 추가
ParkSohyunee 8bb5246
Feat: zustand persist를 사용하여 로그인 후 사용자 정보 업데이트 로직 구현
ParkSohyunee 698f238
Feat: request 요청시, authorization headers에 accessToken 함께 보내는 axios 인터…
ParkSohyunee 8a37178
Style: 로그인페이지 폴더구조 변경, 주석 정리
ParkSohyunee d2c5d65
Feat: 리다이렉트페이지 직접 접근시 로그인페이지로 이동하도록 처리
ParkSohyunee 52752a2
Style: 주석정리 및 tsconfig prettier 적용
ParkSohyunee 3c7545e
Feat: zustand persist store에 devtools 적용
ParkSohyunee ae97f60
Style: 불필요한 코드 정리
ParkSohyunee 371c219
Style: 불필요한 코드 정리
ParkSohyunee 848a645
Merge branch 'dev' into feature/login
ParkSohyunee 42a79a5
Fix: useEffect 의존성 배열에 code 변수 추가
ParkSohyunee 58f6657
Merge branch 'feature/login' of https://github.com/ParkSohyunee/Listy…
ParkSohyunee d62fc9a
Fix: searchParams 타입오류 수정
ParkSohyunee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use client'; | ||
|
||
import { useSearchParams, useRouter } from 'next/navigation'; | ||
import { useEffect } from 'react'; | ||
import { AxiosError } from 'axios'; | ||
|
||
import axiosInstance from '@/lib/axios/axiosInstance'; | ||
import { useUser } from '@/store/useUser'; | ||
import { UserOnLoginType } from '@/lib/types/user'; | ||
|
||
export default function KakaoRedirectPage() { | ||
const router = useRouter(); | ||
const { updateUser } = useUser(); | ||
const searchParams = useSearchParams(); | ||
const code = searchParams ? searchParams.get('code') : null; | ||
|
||
useEffect(() => { | ||
const controller = new AbortController(); | ||
|
||
if (!code) { | ||
router.push('/login'); | ||
return; | ||
} | ||
|
||
const loginKakao = async () => { | ||
try { | ||
const res = await axiosInstance.get<UserOnLoginType>(`/auth/redirect/kakao?code=${code}`, { | ||
signal: controller.signal, | ||
}); | ||
|
||
const { id, accessToken } = res.data; | ||
updateUser({ id, accessToken }); | ||
|
||
router.push('/'); | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
if (!controller.signal.aborted) { | ||
console.error(error.message); | ||
} else { | ||
console.log('Request canceled:', error.message); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
loginKakao(); | ||
|
||
return () => { | ||
controller.abort(); // 마운트 해제 및 axios 요청 취소 | ||
}; | ||
}, [code]); | ||
|
||
return <div>로그인 중입니다.</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// 보일러플레이트용 임시 파일 | ||
// 추후 이 파일은 지워주세요 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
TODO | ||
- [ ] 로그인 만료 확인, refreshToken(추후) | ||
- [ ] 로그인 페이지 UI | ||
*/ | ||
|
||
import Link from 'next/link'; | ||
|
||
const oauthType = { | ||
kakao: 'kakao', | ||
naver: 'naver', | ||
google: 'google', | ||
}; | ||
|
||
export default function LoginPage() { | ||
return ( | ||
<div> | ||
로그인페이지 | ||
<div> | ||
<Link href={`https://dev.api.listywave.com/auth/${oauthType.kakao}`}>카카오 로그인</Link> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
import axios from 'axios'; | ||
import { useUser } from '@/store/useUser'; | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: 'https://dev.api.listywave.com', | ||
withCredentials: true, | ||
withCredentials: true, // refreshToken을 고려해서 true로 설정 | ||
}); | ||
|
||
axiosInstance.interceptors.request.use( | ||
(config) => { | ||
const accessToken = useUser.getState().user.accessToken; | ||
|
||
if (accessToken) { | ||
config.headers.Authorization = `${accessToken}`; // Bearer option 추가 예정 | ||
} | ||
|
||
return config; | ||
}, | ||
(error) => { | ||
console.log(error); | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
export default axiosInstance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// 로그인한 사용자 리스폰스 타입 | ||
export interface UserOnLoginType { | ||
id: number; | ||
nickname: string; | ||
description?: string; | ||
profileImageUrl?: string; | ||
backgroundImageUrl?: string; | ||
followerCount: number; | ||
followingCount: number; | ||
isFirst: boolean; | ||
accessToken: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { create } from 'zustand'; | ||
import { devtools, persist } from 'zustand/middleware'; | ||
import { UserOnLoginType } from '@/lib/types/user'; | ||
|
||
interface UserStateType { | ||
user: Pick<UserOnLoginType, 'id' | 'accessToken'>; | ||
updateUser: (user: Pick<UserOnLoginType, 'id' | 'accessToken'>) => void; | ||
} | ||
|
||
const initialValue = { | ||
id: 0, | ||
accessToken: '', | ||
}; | ||
|
||
// 사용자 정보(id) 및 상태(로그인, 로그아웃)를 저장하는 store | ||
const useUserStore = create<UserStateType>()( | ||
devtools( | ||
persist( | ||
(set) => ({ | ||
user: initialValue, | ||
updateUser: (user) => | ||
set((state) => ({ | ||
user: { | ||
...state.user, | ||
...user, | ||
}, | ||
})), | ||
}), | ||
{ | ||
name: 'user-storage', // localStorage에 저장될 이름 | ||
} | ||
), | ||
{ | ||
name: 'user-store', // Devtools에서 사용될 스토어 이름 | ||
} | ||
) | ||
); | ||
|
||
export const useUser = useUserStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
소현님 요청취소 구현하신거 보고 처음 알아갑니다!
로그인 과정에서 요청을 취소하게 되는 상황이 있어서 구현하신건가요?
어떨때 필요해서 구현하신건지 단순히 궁금했습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
유진님! 맞습니다~! useEffect 콜백함수에서 api 요청을 보내고 있는데 중복으로 호출되는 문제가 있어서 remount가 될 때 이전 api 요청에 대해서는 취소처리가 필요하여 구현하였습니다. 자세한 내용은 관련 이슈에 남겨 놓았습니다. 참고 부탁드립니다~!😊