11import { RequestHandler , Router } from "express" ;
22import passport from "passport" ;
33import cookieConfig from "../config/cookie.config" ;
4- import createToken from "../utils/token.utils" ;
4+ import createToken , { Payload } from "../utils/token.utils" ;
55import {
66 FRONTEND_URL ,
77 KAKAO_CALLBACK_URL ,
88 KAKAO_CLIENT_ID ,
99 NAVER_CLIENT_ID ,
1010 NAVER_REDIRECT_URI ,
1111} from "../env" ;
12+ import CustomError from "../utils/interfaces/customError" ;
1213
1314const router = Router ( ) ;
1415
@@ -90,7 +91,7 @@ const handleOAuthCallback: RequestHandler = (req, res) => {
9091 if ( ! req . user ) {
9192 return res . redirect ( "/login" ) ;
9293 }
93- const user = req . user as any ;
94+ const user = req . user as Payload ;
9495 const userType = req . query . state as string ;
9596
9697 const accessToken = createToken ( user , "access" ) ;
@@ -99,21 +100,28 @@ const handleOAuthCallback: RequestHandler = (req, res) => {
99100 res . cookie ( "accessToken" , accessToken , cookieConfig . accessTokenOption ) ;
100101 res . cookie ( "refreshToken" , refreshToken , cookieConfig . refreshTokenOption ) ;
101102
102- const messages : Record < string , string > = {
103- customer : "고객 프로필을 등록해주세요." ,
104- mover : "기사 프로필을 등록해주세요." ,
105- } ;
106-
107- const redirectUrls : Record < string , string > = {
108- customer : "/me/profile" ,
109- mover : "/mover/profile" ,
110- } ;
111-
112- res . status ( 204 ) . send ( {
113- message : messages [ userType ] || "프로필을 등록해주세요." ,
114- redirectUrl : FRONTEND_URL + redirectUrls [ userType ] ,
115- redirect : true ,
116- } ) ;
103+ if ( user . customer ?. id || user . mover ?. id ) {
104+ res . redirect ( FRONTEND_URL ) ;
105+ } else {
106+ const messages : Record < string , string > = {
107+ customer : "고객 프로필을 등록해주세요." ,
108+ mover : "기사 프로필을 등록해주세요." ,
109+ } ;
110+
111+ const redirectUrls : Record < string , string > = {
112+ customer : "/me/profile" ,
113+ mover : "/mover/profile" ,
114+ } ;
115+
116+ const error : CustomError = new Error ( "Forbidden" ) ;
117+ error . status = 403 ;
118+ error . data = {
119+ message : messages [ userType ] || "프로필을 등록해주세요." ,
120+ redirectUrl : FRONTEND_URL + redirectUrls [ userType ] ,
121+ redirect : true ,
122+ } ;
123+ throw error ;
124+ }
117125} ;
118126
119127router . get (
0 commit comments