@@ -3,13 +3,9 @@ import User from '../models/user.model.js';
33import { generateToken } from '../config/passport.config.js' ;
44import crypto from 'crypto' ;
55
6- // WARNING: cookie-parser middleware must be enabled in app.js for the state cookie to work
7- // e.g. app.use(cookieParser());
8-
96// Redirect to Google OAuth (generates a per-request state stored in an httpOnly cookie)
107export const googleAuth = ( req , res , next ) => {
118 const state = crypto . randomBytes ( 16 ) . toString ( 'hex' ) ;
12- // store state in an httpOnly cookie for CSRF protection, short lived
139 res . cookie ( 'oauth_state' , state , {
1410 httpOnly : true ,
1511 secure : process . env . NODE_ENV === 'production' ,
@@ -37,7 +33,6 @@ export const googleCallback = (req, res, next) => {
3733 res . clearCookie ( 'oauth_state' ) ;
3834 return res . status ( 403 ) . json ( { success : false , message : 'Invalid OAuth state' } ) ;
3935 }
40- // clear cookie early
4136 res . clearCookie ( 'oauth_state' ) ;
4237
4338 passport . authenticate ( 'google' , { session : false } , async ( err , user , info ) => {
@@ -69,7 +64,6 @@ export const googleCallback = (req, res, next) => {
6964 return res . redirect ( `${ frontend } /auth/success#token=${ token } ` ) ;
7065 }
7166
72- // return safe public user view (include email for owner)
7367 return res . json ( {
7468 success : true ,
7569 message : 'Authentication successful' ,
@@ -91,59 +85,41 @@ export const googleCallback = (req, res, next) => {
9185export const getProfile = async ( req , res ) => {
9286 try {
9387 const user = await User . findById ( req . user . userId ) ;
94-
9588 if ( ! user ) {
96- return res . status ( 404 ) . json ( {
97- success : false ,
98- message : 'User not found'
99- } ) ;
89+ return res . status ( 404 ) . json ( { success : false , message : 'User not found' } ) ;
10090 }
101-
102- // return public view including email because requester is the owner (authenticated)
103- res . json ( {
104- success : true ,
105- user : user . toPublic ( true )
106- } ) ;
91+ res . json ( { success : true , user : user . toPublic ( true ) } ) ;
10792 } catch ( error ) {
10893 console . error ( 'Get profile error:' , error ?. stack || error ) ;
109- res . status ( 500 ) . json ( {
110- success : false ,
111- message : 'Failed to get user profile'
112- } ) ;
94+ return res . status ( 500 ) . json ( { success : false , message : 'Failed to get user profile' } ) ;
11395 }
11496} ;
11597
11698// Logout (invalidate tokens for this user by bumping tokenVersion)
11799export const logout = async ( req , res ) => {
118100 try {
119- // increment tokenVersion to revoke all existing tokens for this user
120101 await User . incrementTokenVersion ( req . user . userId ) ;
121- // clear token cookie if used
122102 if ( process . env . SEND_TOKEN_COOKIE === 'true' ) {
123103 res . clearCookie ( 'token' , { httpOnly : true , sameSite : 'lax' , secure : process . env . NODE_ENV === 'production' } ) ;
124104 }
125- return res . json ( {
126- success : true ,
127- message : 'Logout successful. Tokens invalidated on server.'
128- } ) ;
105+ return res . json ( { success : true , message : 'Logout successful. Tokens invalidated on server.' } ) ;
129106 } catch ( error ) {
130- console . error ( 'Logout error:' , error ) ;
107+ console . error ( 'Logout error:' , error ?. stack || error ) ;
131108 return res . status ( 500 ) . json ( { success : false , message : 'Failed to logout' } ) ;
132109 }
133110} ;
134111
135- // Refresh token: ensure tokenVersion still matches before issuing new token matches before issuing new token
136- export const refreshToken = async ( req , res ) => { const refreshToken = async ( req , res ) => {
137- try { ry {
138- const user = await User . findById ( req . user . userId ) . select ( '-__v' ) ; const user = await User . findById ( req . user . userId ) . select ( '-__v' ) ;
139- if ( ! user ) { if ( ! user ) {
140- return res . status ( 404 ) . json ( { success : false , message : 'User not found' } ) ; not found ' } ) ;
112+ // Refresh token: issue a new JWT if the authenticated user's tokenVersion matches current
113+ export const refreshToken = async ( req , res ) => {
114+ try {
115+ const user = await User . findById ( req . user . userId ) . select ( '-__v' ) ;
116+ if ( ! user ) {
117+ return res . status ( 404 ) . json ( { success : false , message : 'User not found' } ) ;
141118 }
142- // issue new JWT reflecting current tokenVersionissue new JWT reflecting current tokenVersion
143119 const token = generateToken ( user ) ;
144- res . json ( { success : true , message : 'Token refreshed successfully' , token } ) ; d successfully ', token });
120+ return res . json ( { success : true , message : 'Token refreshed successfully' , token } ) ;
145121 } catch ( error ) {
146- console . error ( 'Refresh token error:' , error ?. stack || error ) ; ack || error ) ;
122+ console . error ( 'Refresh token error:' , error ?. stack || error ) ;
147123 return res . status ( 500 ) . json ( { success : false , message : 'Failed to refresh token' } ) ;
148124 }
149125} ;
0 commit comments