-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft storing token in cookies and adding token validator
- Loading branch information
Showing
5 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const jwtSecretKey = process.env.JWT_SECRETKEY; | ||
|
||
if (!jwtSecretKey) { | ||
throw new Error('JWT_SECRETKEY is not defined in the environment variables.'); | ||
} | ||
|
||
export const verifiedToken = (token: string): any => { | ||
try { | ||
return jwt.verify(token, jwtSecretKey); | ||
} catch (err) { | ||
console.error(err); | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Request, Response, NextFunction, RequestHandler } from 'express'; | ||
import { verifiedToken } from '../helper/verify'; | ||
|
||
export interface DecodedUser { | ||
userType: string; | ||
id: string; | ||
email: string; | ||
} | ||
|
||
export interface CustomeRequest extends Request { | ||
user?: DecodedUser; | ||
} | ||
|
||
export const isTokenValide: RequestHandler = async ( | ||
req: CustomeRequest, | ||
res: Response, | ||
next: NextFunction | ||
): Promise<void> => { | ||
try { | ||
const token = req.cookies.token; | ||
const user = verifiedToken(token) as DecodedUser; | ||
if (!user) { | ||
res.status(401).json({ Message: 'Sorry, You are not authorized' }); | ||
return; | ||
} | ||
req.user = user; | ||
return next(); | ||
} catch (error) { | ||
console.error('Error in token Validation middleware:\n', error); | ||
res.status(401).json({ Message: 'Sorry, Something went wrong' }); | ||
return; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters