-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement JWT authorization and login method
- Loading branch information
1 parent
752a917
commit 296c4e0
Showing
3 changed files
with
32 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as jwt from 'jsonwebtoken'; | ||
import crypto from 'crypto'; | ||
const jwtSecret = 'aJWTSecret13121992for13121992jwtTokenInTheRequest13121992'; | ||
class AuthorizationController { | ||
static login(req, res) { | ||
try { | ||
const refreshId = req.body.userId + jwtSecret; | ||
const salt = crypto.randomBytes(16).toString('base64'); | ||
const hash = crypto | ||
.createHmac('sha512', salt) | ||
.update(refreshId) | ||
.digest('base64'); | ||
req.body.refreshKey = salt; | ||
const accessToken = jwt.sign(req.body, jwtSecret); | ||
const refreshToken = Buffer.from(hash).toString('base64'); | ||
const {userId, name, email, roles, provider} = req.body; | ||
const responseData = {accessToken, refreshToken, userId, name, email, roles, provider}; | ||
res.status(201).send(responseData); | ||
} catch (err) { | ||
res.status(500).send({errors: err}); | ||
} | ||
} | ||
} | ||
|
||
export default AuthorizationController; |
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,11 +1,12 @@ | ||
import express from 'express'; | ||
import UserController from '../controllers/user.controller'; | ||
import AuthorizationController from '../auth/authorization.controller'; | ||
|
||
const routes = express.Router(); | ||
|
||
// sign ip with avatar id | ||
routes.post('/signup/:avatarId', UserController.signUpWithAvatar); | ||
routes.post('/signup', UserController.signUp); | ||
routes.post('/login', UserController.login); | ||
routes.post('/login', [UserController.login, AuthorizationController.login]); | ||
|
||
export default routes; |