Skip to content

Commit

Permalink
refactor: 로컬 스토리지에 세션 스토리지 추가하여 토큰 임시 저장 처리(#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddhelop committed Oct 28, 2024
1 parent 6ec3c8b commit fcbe157
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
15 changes: 8 additions & 7 deletions src/features/login/model/useGoogleAuth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// src/features/auth/hooks/useKakaoAuth.ts
import { useRouter } from 'next/navigation'
import { useEffect, useState } from 'react'
import { useRecoilState } from 'recoil'
import { accessTokenState } from '@/context/recoil-context'
import { googleLogin, kakaoLogin } from '../api/authApi'
import { LoginResponse } from './authType'

export const useGoogleAuth = (code: string | null) => {
export const useKakaoAuth = (code: string | null) => {
const router = useRouter()
const [accessToken, setAccessToken] = useRecoilState(accessTokenState)
const [loading, setLoading] = useState(true)
Expand All @@ -16,14 +15,15 @@ export const useGoogleAuth = (code: string | null) => {
if (!code) return
try {
const responseData: LoginResponse = await googleLogin(code)
const { accessToken, email, isMemberBasicInform } = responseData.result

setAccessToken(responseData.result.accessToken)

if (responseData.result.existMemberBasicInform) {
if (isMemberBasicInform) {
// 기존 회원: Recoil 상태에 토큰 저장 후 홈으로 이동
setAccessToken(accessToken)
router.push('/')
} else {
// URL에 이메일과 이름을 쿼리 파라미터로 포함하여 온보딩 페이지로 이동
const email = responseData.result.email
// 신규 회원: 세션 스토리지에 토큰 저장 후 온보딩 페이지로 이동
sessionStorage.setItem('accessToken', accessToken)
router.push(`/login/onboarding-info?email=${email}`)
}
} catch (error) {
Expand All @@ -32,6 +32,7 @@ export const useGoogleAuth = (code: string | null) => {
setLoading(false)
}
}

login()
}, [code, router, setAccessToken])

Expand Down
13 changes: 7 additions & 6 deletions src/features/login/model/useKakaoAuth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// src/features/auth/hooks/useKakaoAuth.ts
import { useRouter } from 'next/navigation'
import { useEffect, useState } from 'react'
import { useRecoilState } from 'recoil'
Expand All @@ -16,14 +15,15 @@ export const useKakaoAuth = (code: string | null) => {
if (!code) return
try {
const responseData: LoginResponse = await kakaoLogin(code)
const { accessToken, email, isMemberBasicInform } = responseData.result

setAccessToken(responseData.result.accessToken)

if (responseData.result.existMemberBasicInform) {
if (isMemberBasicInform) {
// 기존 회원: Recoil 상태에 토큰 저장 후 홈으로 이동
setAccessToken(accessToken)
router.push('/')
} else {
// URL에 이메일과 이름을 쿼리 파라미터로 포함하여 온보딩 페이지로 이동
const email = responseData.result.email
// 신규 회원: 세션 스토리지에 토큰 저장 후 온보딩 페이지로 이동
sessionStorage.setItem('accessToken', accessToken)
router.push(`/login/onboarding-info?email=${email}`)
}
} catch (error) {
Expand All @@ -32,6 +32,7 @@ export const useKakaoAuth = (code: string | null) => {
setLoading(false)
}
}

login()
}, [code, router, setAccessToken])

Expand Down
15 changes: 8 additions & 7 deletions src/features/login/model/useNaverAuth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// src/features/auth/hooks/useKakaoAuth.ts
import { useRouter } from 'next/navigation'
import { useEffect, useState } from 'react'
import { useRecoilState } from 'recoil'
import { accessTokenState } from '@/context/recoil-context'
import { kakaoLogin, naverLogin } from '../api/authApi'
import { LoginResponse } from './authType'

export const useNaverAuth = (code: string | null) => {
export const useKakaoAuth = (code: string | null) => {
const router = useRouter()
const [accessToken, setAccessToken] = useRecoilState(accessTokenState)
const [loading, setLoading] = useState(true)
Expand All @@ -16,14 +15,15 @@ export const useNaverAuth = (code: string | null) => {
if (!code) return
try {
const responseData: LoginResponse = await naverLogin(code)
const { accessToken, email, isMemberBasicInform } = responseData.result

setAccessToken(responseData.result.accessToken)

if (responseData.result.existMemberBasicInform) {
if (isMemberBasicInform) {
// 기존 회원: Recoil 상태에 토큰 저장 후 홈으로 이동
setAccessToken(accessToken)
router.push('/')
} else {
// URL에 이메일과 이름을 쿼리 파라미터로 포함하여 온보딩 페이지로 이동
const email = responseData.result.email
// 신규 회원: 세션 스토리지에 토큰 저장 후 온보딩 페이지로 이동
sessionStorage.setItem('accessToken', accessToken)
router.push(`/login/onboarding-info?email=${email}`)
}
} catch (error) {
Expand All @@ -32,6 +32,7 @@ export const useNaverAuth = (code: string | null) => {
setLoading(false)
}
}

login()
}, [code, router, setAccessToken])

Expand Down

0 comments on commit fcbe157

Please sign in to comment.