-
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.
created middlewere for authantications
- Loading branch information
1 parent
1c7d190
commit 3c17791
Showing
6 changed files
with
104 additions
and
18 deletions.
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
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
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,29 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const {jsonStatus, jsonStatusError, jsonStatusSuccess} = require('../operations/errorhandlingOperations'); | ||
require('dotenv').config(); | ||
|
||
const JWT_SECRET = process.env.JWT_ACCESS_TOKEN; | ||
|
||
const authanticateJwtToken = (req,res,next)=>{ | ||
|
||
const authHeader = req.headers['authorization']; | ||
console.log(req.headers); | ||
|
||
if (!authHeader || !authHeader.startsWith('Bearer ')) { | ||
return res.send(jsonStatusError({message: "Unauthorised", statusCode: 401})) | ||
} | ||
|
||
const token = authHeader.split(' ')[1]; | ||
|
||
try { | ||
const decoded = jwt.verify(token, JWT_SECRET); | ||
req.user = decoded; | ||
next(); | ||
} catch (err) { | ||
// Token verification failed | ||
return res.send(jsonStatusError({message: err, statusCode: 401})) | ||
} | ||
} | ||
|
||
|
||
module.exports = {authanticateJwtToken} |
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
const { Router } = require('express') | ||
const userProfileRouter = Router() | ||
const {getUserProfileInformations, getUserInformationsByName, getUserInformationsByNameFromDb} = require('../controller/userProfile.controller'); | ||
const {getUserProfileInformations, getUserInformationsByName, getUserInformationsByNameFromDb, getprofileinfoprotect} = require('../controller/userProfile.controller'); | ||
const {authanticateJwtToken} = require('../middleware/Auth') | ||
|
||
userProfileRouter.post('/getuserprofileinfo', getUserProfileInformations); | ||
userProfileRouter.get('/getprofileinfoprotect', authanticateJwtToken, getprofileinfoprotect ); | ||
userProfileRouter.post('/getprofileinfo', getUserInformationsByNameFromDb); | ||
|
||
module.exports = {userProfileRouter}; |