@@ -26,10 +26,10 @@ const noReturnUrls = [
2626exports . signup = async ( req , res ) => {
2727 try {
2828 const user = await UserService . create ( req . body ) ;
29- const token = jwt . sign ( { userId : user . id } , config . jwt . secret ) ;
29+ const token = jwt . sign ( { userId : user . id } , config . jwt . secret , { expiresIn : config . jwt . expiresIn } ) ;
3030 return res . status ( 200 )
3131 . cookie ( 'TOKEN' , token , { httpOnly : true } )
32- . json ( { user, tokenExpiresIn : Date . now ( ) + ( 3600 * 24 * 1000 ) } ) ;
32+ . json ( { user, tokenExpiresIn : Date . now ( ) + ( config . jwt . expiresIn * 1000 ) } ) ;
3333 } catch ( err ) {
3434 responses . error ( res , 422 , errors . getMessage ( err ) ) ( err ) ;
3535 }
@@ -43,35 +43,36 @@ exports.signup = async (req, res) => {
4343 */
4444exports . signin = async ( req , res ) => {
4545 const user = req . user ;
46- const token = jwt . sign ( { userId : user . id } , configuration . jwt . secret ) ;
46+ const token = jwt . sign ( { userId : user . id } , configuration . jwt . secret , { expiresIn : config . jwt . expiresIn } ) ;
4747 return res . status ( 200 )
4848 . cookie ( 'TOKEN' , token , { httpOnly : true } )
49- . json ( { user, tokenExpiresIn : Date . now ( ) + ( 3600 * 24 * 1000 ) } ) ;
49+ . json ( { user, tokenExpiresIn : Date . now ( ) + ( config . jwt . expiresIn * 1000 ) } ) ;
5050} ;
5151
5252/**
53- * @desc Endpoint to generate a token
53+ * @desc Endpoint to get a new token if old is ok
5454 * @param {Object } req - Express request object
5555 * @param {Object } res - Express response object
5656 */
5757exports . token = async ( req , res ) => {
58- try {
59- // Authenticate the user based on credentials
60- // @TODO be consistent with whether the login field for user identification
61- // is a username or an email
62- const user = await UserService . authenticate ( req . body . email , req . body . password ) ;
63- // Create the token and send
64- // @TODO properly create the token with all of its metadata
65- const payload = {
66- id : user . id ,
58+ let user = null ;
59+ if ( req . user ) {
60+ user = {
61+ id : req . user . id ,
62+ provider : escape ( req . user . provider ) ,
63+ username : escape ( req . user . username ) ,
64+ roles : req . user . roles ,
65+ profileImageURL : req . user . profileImageURL ,
66+ email : escape ( req . user . email ) ,
67+ lastName : escape ( req . user . lastName ) ,
68+ firstName : escape ( req . user . firstName ) ,
69+ additionalProvidersData : req . user . additionalProvidersData ,
6770 } ;
68- // @TODO properly sign the token, not with a shared secret (use pubkey instead),
69- // and specify proper expiration, issuer, algorithm, etc.
70- const token = jwt . sign ( payload , config . jwt . secret ) ;
71- return res . status ( 200 ) . cookies ( 'TOKEN' , token ) ;
72- } catch ( err ) {
73- responses . error ( res , 422 , errors . getMessage ( err ) ) ( err ) ;
7471 }
72+ const token = jwt . sign ( { userId : user . id } , configuration . jwt . secret , { expiresIn : config . jwt . expiresIn } ) ;
73+ return res . status ( 200 )
74+ . cookie ( 'TOKEN' , token , { httpOnly : true } )
75+ . json ( { user, tokenExpiresIn : Date . now ( ) + ( config . jwt . expiresIn * 1000 ) } ) ;
7576} ;
7677
7778/**
0 commit comments