@@ -34,9 +34,12 @@ export class OAuthService {
3434 ) { }
3535
3636 // OAuth Login
37- async oauthLogin ( email : string ) : Promise < LoginOutput > {
37+ async oauthLogin ( email : string , provider : PROVIDER ) : Promise < LoginOutput > {
3838 try {
39- const user : User = await this . userRepository . findOneByOrFail ( { email } ) ;
39+ const user : User = await this . userRepository . findOneByOrFail ( {
40+ email,
41+ provider,
42+ } ) ;
4043 if ( user ) {
4144 const payload : Payload = this . jwtService . createPayload (
4245 user . email ,
@@ -98,9 +101,12 @@ export class OAuthService {
98101 throw new BadRequestException ( 'Please Agree to share your email' ) ;
99102 }
100103
101- const user = await this . userRepository . findOneByEmail ( email ) ;
104+ const user = await this . userRepository . findOneByEmailAndProvider (
105+ email ,
106+ PROVIDER . KAKAO ,
107+ ) ;
102108 if ( user ) {
103- return this . oauthLogin ( user . email ) ;
109+ return this . oauthLogin ( user . email , PROVIDER . KAKAO ) ;
104110 }
105111
106112 // 회원가입인 경우 기본 카테고리 생성 작업 진행
@@ -115,51 +121,25 @@ export class OAuthService {
115121 await this . userRepository . createOne ( newUser ) ;
116122 await this . categoryRepository . createDefaultCategories ( newUser ) ;
117123
118- return this . oauthLogin ( newUser . email ) ;
124+ return this . oauthLogin ( newUser . email , PROVIDER . KAKAO ) ;
119125 } catch ( e ) {
120126 throw e ;
121127 }
122128 }
123129
124- async createOneWithKakao ( { authorizationToken } : KakaoLoginDto ) {
125- const { userInfo } =
126- await this . oauthUtil . getKakaoUserInfo ( authorizationToken ) ;
127-
128- const email = userInfo . kakao_account . email ;
129- if ( ! email ) {
130- throw new BadRequestException ( 'Please Agree to share your email' ) ;
131- }
132-
133- const user = await this . userRepository . findOneByEmail ( email ) ;
134-
135- if ( user ) {
136- return this . oauthLogin ( user . email ) ;
137- }
138-
139- // 회원가입인 경우 기본 카테고리 생성 작업 진행
140- const newUser = User . of ( {
141- email,
142- name : userInfo . kakao_account . profile . nickname ,
143- profileImage : userInfo . kakao_account . profile ?. profile_image_url ,
144- password : this . encodePasswordFromEmail ( email , process . env . KAKAO_JS_KEY ) ,
145- provider : PROVIDER . KAKAO ,
146- } ) ;
147-
148- await this . userRepository . createOne ( newUser ) ;
149- await this . categoryRepository . createDefaultCategories ( newUser ) ;
150- return this . oauthLogin ( newUser . email ) ;
151- }
152-
153130 // Login with Google account info
154131 async googleOauth ( {
155132 email,
156133 name,
157134 picture,
158135 } : googleUserInfo ) : Promise < LoginOutput > {
159- const user = await this . userRepository . findOneByEmail ( email ) ;
136+ const user = await this . userRepository . findOneByEmailAndProvider (
137+ email ,
138+ PROVIDER . GOOGLE ,
139+ ) ;
160140
161141 if ( user ) {
162- return this . oauthLogin ( user . email ) ;
142+ return this . oauthLogin ( user . email , PROVIDER . GOOGLE ) ;
163143 }
164144
165145 // 회원가입인 경우 기본 카테고리 생성 작업 진행
@@ -177,7 +157,7 @@ export class OAuthService {
177157 await this . userRepository . createOne ( newUser ) ;
178158 await this . categoryRepository . createDefaultCategories ( newUser ) ;
179159
180- return this . oauthLogin ( newUser . email ) ;
160+ return this . oauthLogin ( newUser . email , PROVIDER . GOOGLE ) ;
181161 }
182162
183163 private encodePasswordFromEmail ( email : string , key ?: string ) : string {
@@ -225,10 +205,13 @@ export class OAuthService {
225205
226206 const { sub : id , email } = this . jwtService . decode ( data . id_token ) ;
227207
228- const user = await this . userRepository . findOneByEmail ( email ) ;
208+ const user = await this . userRepository . findOneByEmailAndProvider (
209+ email ,
210+ PROVIDER . APPLE ,
211+ ) ;
229212
230213 if ( user ) {
231- return this . oauthLogin ( user . email ) ;
214+ return this . oauthLogin ( user . email , PROVIDER . APPLE ) ;
232215 }
233216
234217 const newUser = User . of ( {
@@ -244,6 +227,6 @@ export class OAuthService {
244227 await this . userRepository . createOne ( newUser ) ;
245228 await this . categoryRepository . createDefaultCategories ( newUser ) ;
246229
247- return this . oauthLogin ( newUser . email ) ;
230+ return this . oauthLogin ( newUser . email , PROVIDER . APPLE ) ;
248231 }
249232}
0 commit comments