Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
307 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
PORT=[server_port_number] | ||
DATABASE_URL=postgres://[username]@[server]:[port]/[database_name] | ||
TEST_DATABASE_URL=postgres://[username]@[server]:[port]/[database_name] | ||
JWT_SECRET=[your_supersecret_secret] |
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,58 @@ | ||
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 authorize(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 authorizeAdmin(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,8 +1,11 @@ | ||
import { Router } from 'express'; | ||
import AuthController from '../controllers/authController'; | ||
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; |
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
Oops, something went wrong.