@@ -57,7 +57,7 @@ module.exports = {
5757 if ( ! user ) {
5858 res . status ( HttpStatus . NOT_FOUND ) . json ( { msg : 'User not found!' } )
5959 }
60- const token = jwt . sign ( { _id : user . _id , expiry : Date . now ( ) + 10800000 } , ' process.env.JWT_SECRET' )
60+ const token = jwt . sign ( { _id : user . _id , expiry : Date . now ( ) + 10800000 } , process . env . JWT_SECRET )
6161 await user . save ( )
6262 return res . status ( HttpStatus . OK ) . json ( { success : true , token } )
6363 } catch ( error ) {
@@ -72,7 +72,7 @@ module.exports = {
7272 const { password, id } = req . body
7373 const { token } = req . params
7474 try {
75- const decodedToken = jwt . verify ( token , ' process.env.JWT_SECRET' )
75+ const decodedToken = jwt . verify ( token , process . env . JWT_SECRET )
7676
7777 if ( Date . now ( ) <= decodedToken . expiry ) {
7878 const user = await User . findById ( {
@@ -116,7 +116,7 @@ module.exports = {
116116 try {
117117 const { token } = req . params
118118 const decodedToken = jwt . verify ( token , 'process.env.JWT_SECRET' )
119- const expiryTime = decodedToken . iat + 10800000 // 24 hrs
119+ const expiryTime = decodedToken . iat + 24 * 3600 * 1000 // 24 hrs
120120 if ( expiryTime <= Date . now ( ) ) {
121121 const user = await User . findById ( decodedToken . _id )
122122 if ( ! user ) {
@@ -130,5 +130,23 @@ module.exports = {
130130 } catch ( Error ) {
131131 return res . status ( HttpStatus . BAD_REQUEST ) . json ( { Error } )
132132 }
133+ } ,
134+
135+ getInviteLink : async ( req , res , next ) => {
136+ const token = jwt . sign ( { _id : req . user . _id , expiry : Date . now ( ) + 24 * 3600 * 1000 } , process . env . JWT_SECRET )
137+ const inviteLink = `${ req . protocol } ://${ req . get ( 'host' ) } /user/invite/${ token } `
138+ return res . status ( HttpStatus . OK ) . json ( { inviteLink : inviteLink } )
139+ } ,
140+
141+ processInvite : async ( req , res , next ) => {
142+ const { token } = req . params
143+ const decodedToken = jwt . verify ( token , process . env . JWT_SECRET )
144+ // check if token not expired and sender exist in db then valid request
145+ const user = await User . findById ( decodedToken . _id )
146+ if ( user && Date . now ( ) <= decodedToken . expiry ) {
147+ console . log ( 'Valid invite!' )
148+ return res . status ( HttpStatus . OK ) . json ( { success : true , msg : 'Redirect user to register in client side!' } )
149+ }
150+ return res . status ( HttpStatus . BAD_REQUEST ) . json ( { msg : 'Invalid token!' } )
133151 }
134152}
0 commit comments