|
1 | 1 | import CredentialsProvider from 'next-auth/providers/credentials'; |
| 2 | +import KakaoProvider from 'next-auth/providers/kakao'; |
2 | 3 | import { NextAuthOptions, Session, User } from 'next-auth'; |
3 | 4 | import { JWT } from 'next-auth/jwt'; |
4 | 5 | import dayjs from 'dayjs'; |
5 | 6 | import { |
| 7 | + postKaKaoLogin, |
6 | 8 | postLogin, |
7 | 9 | postRefreshToken, |
8 | 10 | postRegister, |
@@ -37,60 +39,108 @@ export const authOptions: NextAuthOptions = { |
37 | 39 | placeholder: 'λΉλ°λ²νΈλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.', |
38 | 40 | }, |
39 | 41 | }, |
40 | | - async authorize( |
41 | | - credentials: Record<'name' | 'email' | 'password', string> | undefined, |
42 | | - ) { |
43 | | - // κΈ°μ‘΄ authorize ν¨μ λ΄μ© μ μ§ |
| 42 | + async authorize(credentials) { |
44 | 43 | if (!credentials) { |
45 | | - throw new Error('No credentials provided.'); |
| 44 | + return null; |
46 | 45 | } |
| 46 | + |
47 | 47 | const { name, email, password } = credentials; |
48 | 48 |
|
49 | | - // νμκ°μ
|
50 | | - if (name) { |
| 49 | + try { |
| 50 | + // νμκ°μ
|
| 51 | + if (name) { |
| 52 | + const { |
| 53 | + result: token, |
| 54 | + success, |
| 55 | + message, |
| 56 | + } = await postRegister({ |
| 57 | + username: name, |
| 58 | + email: email, |
| 59 | + password: password, |
| 60 | + }); |
| 61 | + |
| 62 | + if (success && token) { |
| 63 | + return { |
| 64 | + id: token.id.toString(), |
| 65 | + name: token.username, |
| 66 | + email: token.email, |
| 67 | + accessToken: token.accessToken, |
| 68 | + refreshToken: token.refreshToken, |
| 69 | + expiredAt: token.expiredAt, |
| 70 | + }; |
| 71 | + } else { |
| 72 | + throw new Error(message || 'Signup failed.'); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // λ‘κ·ΈμΈ |
51 | 77 | const { |
52 | 78 | result: token, |
53 | 79 | success, |
54 | 80 | message, |
55 | | - } = await postRegister({ |
56 | | - username: name, |
| 81 | + } = await postLogin({ |
57 | 82 | email: email, |
58 | 83 | password: password, |
59 | 84 | }); |
60 | 85 |
|
61 | | - if (success) { |
62 | | - return { id: '1', name, email, ...token }; |
| 86 | + if (success && token) { |
| 87 | + return { |
| 88 | + id: token.id.toString(), |
| 89 | + name: token.username, |
| 90 | + email: token.email, |
| 91 | + accessToken: token.accessToken, |
| 92 | + refreshToken: token.refreshToken, |
| 93 | + expiredAt: token.expiredAt, |
| 94 | + }; |
63 | 95 | } else { |
64 | | - const error = new Error('Signup failed.'); |
65 | | - (error as Error).message = message; |
66 | | - throw error; |
| 96 | + throw new Error(message || 'Login failed.'); |
67 | 97 | } |
68 | | - } |
69 | | - |
70 | | - // λ‘κ·ΈμΈ |
71 | | - const { |
72 | | - result: token, |
73 | | - success, |
74 | | - message, |
75 | | - } = await postLogin({ |
76 | | - email: email, |
77 | | - password: password, |
78 | | - }); |
79 | | - |
80 | | - if (success) { |
81 | | - return { id: '1', name, email, ...token }; |
82 | | - } else { |
83 | | - const error = new Error('Login failed.'); |
84 | | - (error as Error).message = message; |
85 | | - throw error; |
| 98 | + } catch (error) { |
| 99 | + // eslint-disable-next-line no-console |
| 100 | + console.error('Authentication error:', error); |
| 101 | + return null; |
86 | 102 | } |
87 | 103 | }, |
88 | 104 | }), |
| 105 | + KakaoProvider({ |
| 106 | + clientId: process.env.KAKAO_CLIENT_ID!, |
| 107 | + clientSecret: process.env.KAKAO_CLIENT_SECRET!, |
| 108 | + }), |
89 | 109 | ], |
90 | 110 | pages: { |
91 | 111 | signIn: '/signin', |
92 | 112 | }, |
93 | 113 | callbacks: { |
| 114 | + async signIn({ user, account }) { |
| 115 | + // μΉ΄μΉ΄μ€ λ‘κ·ΈμΈ μλ² κ²μ¦ |
| 116 | + if (account?.provider === 'kakao' && account.access_token) { |
| 117 | + try { |
| 118 | + const { |
| 119 | + result: token, |
| 120 | + success, |
| 121 | + message, |
| 122 | + } = await postKaKaoLogin({ |
| 123 | + accessToken: account.access_token, |
| 124 | + }); |
| 125 | + if (success && token) { |
| 126 | + user.id = token.id.toString(); |
| 127 | + user.name = token.username; |
| 128 | + user.email = token.email; |
| 129 | + user.accessToken = token.accessToken; |
| 130 | + user.refreshToken = token.refreshToken; |
| 131 | + user.expiredAt = token.expiredAt; |
| 132 | + return true; |
| 133 | + } else { |
| 134 | + throw new Error(message || 'KAKAO Login failed.'); |
| 135 | + } |
| 136 | + } catch (error) { |
| 137 | + // eslint-disable-next-line no-console |
| 138 | + console.error('Error during Kakao login:', error); |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + return true; |
| 143 | + }, |
94 | 144 | async jwt({ token, user }: { token: JWT; user?: IMyUser }) { |
95 | 145 | if (user) { |
96 | 146 | token.id = Number(user.id); |
|
0 commit comments