-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauth.ts
More file actions
137 lines (117 loc) · 3.93 KB
/
Copy pathauth.ts
File metadata and controls
137 lines (117 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as jwt from "jsonwebtoken";
import NextAuth from "next-auth";
import { JWT } from "next-auth/jwt";
import Google from "next-auth/providers/google";
import KakaoProvider from "next-auth/providers/kakao";
import { CustomPrismaAdapter } from "./lib/custom-prisma-adapter";
import { prisma } from "./prisma";
import { getMockUser, updateMockUser } from "@/mocks/sample/Prisma-mock";
export const { handlers, auth, signIn, signOut } = NextAuth({
useSecureCookies: process.env.NODE_ENV === "production",
trustHost: true,
adapter: process.env.NEXT_PUBLIC_MSW_MODE === "true" ? undefined : CustomPrismaAdapter(prisma),
secret: process.env.AUTH_SECRET,
experimental: {
enableWebAuthn: false,
},
providers: [
KakaoProvider({
clientId: process.env.NEXT_PUBLIC_KAKAO_CLIENT_ID,
clientSecret: process.env.NEXT_PUBLIC_KAKAO_CLIENT_SECRET,
}),
Google({
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_PW,
}),
],
session: {
strategy: "jwt",
},
jwt: {
encode: async ({ token, secret }) => {
// Mock 모드에서는 단순한 토큰 생성
if (process.env.NEXT_PUBLIC_MSW_MODE === "true") {
try {
const jsonString = JSON.stringify(token);
const utf8Bytes = new TextEncoder().encode(jsonString);
const base64String = btoa(String.fromCharCode(...utf8Bytes));
return `mock.${base64String}.signature`;
} catch (error) {
console.error("Token encoding failed:", error);
return "mock.eyJzdWIiOiLtlZjqs6Dst53g66qo7YG5IOychOyggCIsImVtYWlsIjoidGVzdEBkaWdyb3VuZC5sb2NhbCJ9.signature";
}
}
return jwt.sign(token as jwt.JwtPayload, secret as string);
},
decode: async ({ token, secret }) => {
// Mock 모드
if (process.env.NEXT_PUBLIC_MSW_MODE === "true") {
const mockUser = getMockUser();
if (!token) {
return {
sub: mockUser.id,
email: mockUser.email,
} as JWT;
}
return { sub: mockUser.id, email: mockUser.email } as JWT;
}
return jwt.verify(token as string, secret as string) as JWT;
},
},
pages: {
signIn: "/signin",
},
callbacks: {
async jwt({ token, user, account }) {
// Mock 모드일 때 - 업데이트된 Mock 데이터 사용
if (process.env.NEXT_PUBLIC_MSW_MODE === "true") {
const mockUser = getMockUser();
return {
sub: mockUser.id,
email: mockUser.email,
name: mockUser.name,
picture: mockUser.image,
};
}
// 실제 로그인 시
if (account && user) {
token.sub = user.id;
}
return token;
},
async session({ session, token }) {
// Mock 모드일 때
if (token.sub) {
session.user.id = token.sub;
session.user.email = token.email || "";
session.user.image = token.picture;
session.user.name = token.name;
}
return session;
},
async signIn({ user, account, profile }) {
// Mock 모드일 때는 별도 처리 없이 바로 로그인 허용
if (process.env.NEXT_PUBLIC_MSW_MODE === "true") {
const mockName = user?.name ? `${user.name}_${account?.provider}_모킹유저` : "모킹유저";
updateMockUser({
id: `mock_user_${Date.now()}`,
name: mockName,
email: user?.email || "mock@diground.local",
image:
"https://cdnimg.melon.co.kr/cm2/photo/images/000/802/83/025/80283025_20241216144433_org.jpg/melon/quality/80/optimize",
});
return true;
}
// 실제 모드에서만 OAuth 처리
if (account?.provider === "kakao" && profile) {
if (!user.email) {
user.email = `kakao_${profile.id}@diground.local`;
}
}
return true;
},
async redirect({ baseUrl }) {
return `${baseUrl}/playlists`;
},
},
});