This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create POST /auth/login and user route permissions
- Loading branch information
Showing
6 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,59 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
class AuthHandler { | ||
static async generateAuthToken(req, res) { | ||
const { | ||
userId, | ||
userName, | ||
userEmail, | ||
userStatus, | ||
} = req; | ||
|
||
const token = jwt.sign({ | ||
userId, | ||
userName, | ||
userEmail, | ||
userStatus, | ||
}, process.env.JWT_SECRET); | ||
|
||
res.status(200).json({ | ||
status: 'success', | ||
message: 'user logged in successfully', | ||
auth_token: token, | ||
}); | ||
} | ||
|
||
static authenticate(req, res, next) { | ||
const token = req.header('x-auth'); | ||
|
||
try { | ||
const decoded = jwt.verify(token, process.env.JWT_SECRET); | ||
req.userId = decoded.userId; | ||
req.userName = decoded.userName; | ||
req.userEmail = decoded.userEmail; | ||
req.userStatus = decoded.userStatus; | ||
return next(); | ||
} catch (error) { | ||
return res.status(401).json({ | ||
status: 'error', | ||
message: 'you must be logged in to use this route', | ||
}); | ||
} | ||
} | ||
|
||
static authenticateAdmin(req, res, next) { | ||
if (req.userStatus !== 'admin') { | ||
return res.status(401).json({ | ||
status: 'error', | ||
message: 'only admins can use this route', | ||
}); | ||
} | ||
|
||
return next(); | ||
} | ||
} | ||
|
||
export default AuthHandler; |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { Router } from 'express'; | ||
import AuthController from '../controllers/Auth'; | ||
import Sanitize from '../middleware/Sanitizer'; | ||
import AuthHandler from '../middleware/AuthHandler'; | ||
|
||
const router = new Router(); | ||
|
||
router.post('/signup', Sanitize.signup, AuthController.signup); | ||
router.post('/login', Sanitize.signin, AuthController.signin, AuthHandler.generateAuthToken); | ||
|
||
export default router; |