Skip to content

Commit

Permalink
refactor: restructure login feature to FSD architecture (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddhelop committed Oct 26, 2024
1 parent 0266ebc commit 4a03e42
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 185 deletions.
2 changes: 1 addition & 1 deletion src/app/login/oauth2/callback/google.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import GoogleRedirect from '@/components/Login/GoogleRedirect'
import GoogleRedirect from '@/features/login/components/GoogleRedirect'

export default GoogleRedirect
2 changes: 1 addition & 1 deletion src/app/login/oauth2/callback/kakao.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import KakaoRedirect from '@/components/Login/KakaoRedirect'
import KakaoRedirect from '@/features/login/components/KakaoRedirect'

export default KakaoRedirect
2 changes: 1 addition & 1 deletion src/app/login/oauth2/callback/kakao/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import KakaoRedirect from '@/components/Login/KakaoRedirect'
import KakaoRedirect from '@/features/login/components/KakaoRedirect'

export default KakaoRedirect
2 changes: 1 addition & 1 deletion src/app/login/oauth2/callback/naver/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import NaverRedirect from '@/components/Login/NaverRedirect'
import NaverRedirect from '@/features/login/components/NaverRedirect'

export default NaverRedirect
2 changes: 1 addition & 1 deletion src/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Login from '@/components/Login/login'
import Login from '@/features/login/components/login'

export default function LoginPage() {
return (
Expand Down
59 changes: 0 additions & 59 deletions src/components/Login/GoogleRedirect.tsx

This file was deleted.

62 changes: 0 additions & 62 deletions src/components/Login/KakaoRedirect.tsx

This file was deleted.

59 changes: 0 additions & 59 deletions src/components/Login/NaverRedirect.tsx

This file was deleted.

41 changes: 41 additions & 0 deletions src/features/login/api/authApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 구글 로그인
export const googleLogin = async (code: string) => {
const response = await fetch(`${process.env.NEXT_PUBLIC_LINKIT_SERVER_URL}/login/google`, {
method: 'POST',
headers: { 'Content-Type': 'application/json;charset=utf-8' },
credentials: 'include',
body: JSON.stringify({ code }),
})

if (!response.ok) throw new Error('Failed to log in with Google')

return response.json()
}

// 카카오 로그인
export const kakaoLogin = async (code: string) => {
const response = await fetch(`${process.env.NEXT_PUBLIC_LINKIT_SERVER_URL}/login/kakao`, {
method: 'POST',
headers: { 'Content-Type': 'application/json;charset=utf-8' },
credentials: 'include',
body: JSON.stringify({ code }),
})

if (!response.ok) throw new Error('Failed to log in with Kakao')

return response.json()
}

// 네이버 로그인
export const naverLogin = async (code: string) => {
const response = await fetch(`${process.env.NEXT_PUBLIC_LINKIT_SERVER_URL}/login/naver`, {
method: 'POST',
headers: { 'Content-Type': 'application/json;charset=utf-8' },
credentials: 'include',
body: JSON.stringify({ code }),
})

if (!response.ok) throw new Error('Failed to log in with Naver')

return response.json()
}
25 changes: 25 additions & 0 deletions src/features/login/components/GoogleRedirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// src/features/auth/components/GoogleRedirect.tsx
'use client'

import { useSearchParams } from 'next/navigation'
import { motion } from 'framer-motion'
import { useGoogleAuth } from '../model/useGoogleAuth'

const GoogleRedirect: React.FC = () => {
const params = useSearchParams()
const code = params.get('code')
const { loading } = useGoogleAuth(code)

return loading ? (
<div className="flex h-screen flex-col items-center justify-center">
<motion.div
className="h-12 w-12 animate-spin rounded-full border-4 border-blue-200 border-t-blue-500"
animate={{ rotate: 360 }}
transition={{ repeat: Infinity, duration: 1 }}
/>
<p className="mt-4 text-lg">Loading...</p>
</div>
) : null
}

export default GoogleRedirect
25 changes: 25 additions & 0 deletions src/features/login/components/KakaoRedirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// src/features/auth/components/KakaoRedirect.tsx
'use client'

import { useSearchParams } from 'next/navigation'
import { motion } from 'framer-motion'
import { useKakaoAuth } from '../model/useKakaoAuth'

const KakaoRedirect: React.FC = () => {
const params = useSearchParams()
const code = params.get('code')
const { loading } = useKakaoAuth(code)

return loading ? (
<div className="flex h-screen flex-col items-center justify-center">
<motion.div
className="h-12 w-12 animate-spin rounded-full border-4 border-blue-200 border-t-blue-500"
animate={{ rotate: 360 }}
transition={{ repeat: Infinity, duration: 1 }}
/>
<p className="mt-4 text-lg">Loading...</p>
</div>
) : null
}

export default KakaoRedirect
25 changes: 25 additions & 0 deletions src/features/login/components/NaverRedirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// src/features/auth/components/NaverRedirect.tsx
'use client'

import { useSearchParams } from 'next/navigation'
import { motion } from 'framer-motion'
import { useNaverAuth } from '../model/useNaverAuth'

const NaverRedirect: React.FC = () => {
const params = useSearchParams()
const code = params.get('code')
const { loading } = useNaverAuth(code)

return loading ? (
<div className="flex h-screen flex-col items-center justify-center">
<motion.div
className="h-12 w-12 animate-spin rounded-full border-4 border-blue-200 border-t-blue-500"
animate={{ rotate: 360 }}
transition={{ repeat: Infinity, duration: 1 }}
/>
<p className="mt-4 text-lg">Loading...</p>
</div>
) : null
}

export default NaverRedirect
File renamed without changes.
6 changes: 6 additions & 0 deletions src/features/login/model/authType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// src/features/auth/model/authTypes.ts
export interface LoginResponse {
accessToken: string
email: string
existMemberBasicInform: boolean
}
39 changes: 39 additions & 0 deletions src/features/login/model/useGoogleAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use client'
// src/features/auth/hooks/useGoogleAuth.ts
import { useRouter } from 'next/navigation'
import { useEffect, useState } from 'react'
import { useRecoilState } from 'recoil'
import { accessTokenState, authState, emailState } from '@/context/recoil-context'
import { googleLogin } from '../api/authApi'
import { LoginResponse } from './authType'

export const useGoogleAuth = (code: string | null) => {
const router = useRouter()
const [toEmail, setToEmail] = useRecoilState(emailState)
const [accessToken, setAccessToken] = useRecoilState(accessTokenState)
const [loading, setLoading] = useState(true)

useEffect(() => {
const login = async () => {
if (!code) return
try {
const responseData: LoginResponse = await googleLogin(code)
setAccessToken(responseData.accessToken)
setToEmail(responseData.email)

if (responseData.existMemberBasicInform) {
router.push('/')
} else {
router.push('/onBoarding')
}
} catch (error) {
console.error('Login failed:', error)
} finally {
setLoading(false)
}
}
login()
}, [code, router, setToEmail, setAccessToken])

return { loading }
}
Loading

0 comments on commit 4a03e42

Please sign in to comment.