-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.config.js
More file actions
172 lines (141 loc) · 5.72 KB
/
auth.config.js
File metadata and controls
172 lines (141 loc) · 5.72 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import dotenv from "dotenv";
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
import { prisma } from "./db.config.js";
import { Strategy as KakaoStrategy } from "passport-kakao";
import { Strategy as NaverStrategy } from "passport-naver";
import { Strategy as TwitterStrategy} from "passport-twitter";
dotenv.config();
export const googleStrategy = new GoogleStrategy(
{
clientID: process.env.PASSPORT_GOOGLE_CLIENT_ID,
clientSecret: process.env.PASSPORT_GOOGLE_CLIENT_SECRET,
callbackURL: `${process.env.BASE_URL}/api/users/oauth2/callback/google`,
scope: ["email", "profile"],
state: true,
},
(accessToken, refreshToken, profile, cb) => {
return googleVerify(profile)
.then((user) => cb(null, user))
.catch((err) => cb(err));
}
);
const googleVerify = async (profile) => {
console.log("profile -> ",profile);
const user = await prisma.account.findFirst({
where: {oauthId : profile.id, provider:profile.provider},
include:{users:true, artists:true},
});
console.log("user -> ", user);
if (user && user.users.length>0) {
return { id: user.users[0].id, nickname: user.users[0].nickname, accountId : user.id.toString(), userId: user.users[0].id.toString(), role:'client', provider: user.provider, oauthId:user.oauthId };
}
if(user && user.artists.length>0){
return{id:user.artists[0].id, nickname:user.artists[0].nickname, accountId:user.id.toString(), artistId: user.artists[0].id.toString(), role:'artist', provider:user.provider, oauthId: user.oauthId};
}
// 사용자가 없으면 회원가입 페이지로 이동하도록 응답
// ex. 프론트에서 signupRequired : true를 응답받으면 회원가입 페이지로 이동
return {
signupRequired : true,
provider : profile.provider,
oauth_id : profile.id,
};
};
export const kakaoStrategy = new KakaoStrategy(
{
clientID: process.env.PASSPORT_KAKAO_CLIENT_ID,
clientSecret: process.env.PASSPORT_KAKAO_CLIENT_SECRET,
callbackURL: `${process.env.BASE_URL}/api/users/oauth2/callback/kakao`,
scope: undefined,
state: true,
},
(accessToken, refreshToken, profile, cb) => {
return kakaoVerify(profile)
.then((user) => cb(null, user))
.catch((err) => cb(err));
}
);
const kakaoVerify = async (profile) => {
console.log(profile);
const user = await prisma.account.findFirst({
where: {oauthId : profile.id.toString(), provider:profile.provider},
include:{users:true, artists:true},
});
console.log(user);
if (user && user.users.length>0) {
return { id: user.users[0].id, nickname: user.users[0].nickname, accountId : user.id.toString(), userId: user.users[0].id.toString(), role:'client', provider: user.provider, oauthId:user.oauthId };
}
if(user && user.artists.length>0){
return{id:user.artists[0].id, nickname:user.artists[0].nickname, accountId:user.id.toString(), artistId: user.artists[0].id.toString(), role:'artist', provider:user.provider, oauthId: user.oauthId};
}
// 사용자가 없으면 회원가입 페이지로 이동하도록 응답
// ex. 프론트에서 signupRequired : true를 응답받으면 회원가입 페이지로 이동
return {
signupRequired : true,
provider : profile.provider,
oauth_id : profile.id.toString(),
};
};
export const naverStrategy = new NaverStrategy(
{
clientID: process.env.PASSPORT_NAVER_CLIENT_ID,
clientSecret: process.env.PASSPORT_NAVER_CLIENT_SECRET,
callbackURL: "http://localhost:3000/api/users/oauth2/callback/naver",
scope: undefined,
state: true,
},
(accessToken, refreshToken, profile, cb) => {
return naverVerify(profile)
.then((user) => cb(null, user))
.catch((err) => cb(err));
}
);
const naverVerify = async (profile) => {
const user = await prisma.account.findFirst({
where: {oauthId : profile.id.toString(), provider:profile.provider},
include:{users:true, artists:true},
});
console.log(user);
if (user && user.users.length>0) {
return { id: user.users[0].id, nickname: user.users[0].nickname, accountId : user.id.toString(), userId: user.users[0].id.toString(), role:'client', provider: user.provider, oauthId:user.oauthId };
}
if(user && user.artists.length>0){
return{id:user.artists[0].id, nickname:user.artists[0].nickname, accountId:user.id.toString(), artistId: user.artists[0].id.toString(), role:'artist', provider:user.provider, oauthId: user.oauthId};
}
// 사용자가 없으면 회원가입 페이지로 이동하도록 응답
// ex. 프론트에서 signupRequired : true를 응답받으면 회원가입 페이지로 이동
return {
signupRequired : true,
provider : profile.provider,
oauth_id : profile.id,
};
};
export const twitterStrategy = new TwitterStrategy(
{
consumerKey: process.env.PASSPORT_TWITTER_CLIENT_ID,
consumerSecret: process.env.PASSPORT_TWITTER_CLIENT_SECRET,
callbackURL: "http://localhost:3000/api/users/oauth2/callback/twitter",
scope: undefined,
state: true,
},
(accessToken, refreshToken, profile, cb) => {
return twitterVerify(profile)
.then((user) => cb(null, user))
.catch((err) => cb(err));
}
);
const twitterVerify = async (profile) => {
const user = await prisma.account.findFirst({
where: {oauthId : profile.id.toString(), provider:profile.provider},
include:{users:true},
});
if (user !== null) {
return { id: user.users[0].id, nickname: user.users[0].nickname, account_id : user.id.toString() };
}
// 사용자가 없으면 회원가입 페이지로 이동하도록 응답
// ex. 프론트에서 signupRequired : true를 응답받으면 회원가입 페이지로 이동
return {
signupRequired : true,
provider : profile.provider,
oauth_id : profile.id,
};
};