generated from polijrorg/template-node-ts-prisma
-
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.
Merge pull request #6 from polijrorg/feat/ensure-routes-are-authentic…
…ated feat: update, read and delete user authenticated
- Loading branch information
Showing
3 changed files
with
58 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import { Router } from 'express'; | ||
|
||
import ensureAuthenticated from '@shared/infra/http/middlewares/EnsureAuthenticated'; | ||
import UsersController from '../controller/UsersController'; | ||
|
||
const usersRoutes = Router(); | ||
|
||
const usersController = new UsersController(); | ||
|
||
usersRoutes.post('/register', usersController.create); | ||
usersRoutes.get('/read', usersController.readAll); | ||
usersRoutes.get('/read/:id', usersController.readById); | ||
usersRoutes.patch('/update/:id', usersController.update); | ||
usersRoutes.delete('/delete/:id', usersController.delete); | ||
usersRoutes.get('/readAll', usersController.readAll); | ||
usersRoutes.get('/read', ensureAuthenticated, usersController.readById); | ||
usersRoutes.patch('/update', ensureAuthenticated, usersController.update); | ||
usersRoutes.delete('/delete', ensureAuthenticated, usersController.delete); | ||
|
||
export default usersRoutes; |
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,31 @@ | ||
import auth from '@config/auth'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { Secret, verify } from 'jsonwebtoken'; | ||
|
||
import AppError from '@shared/errors/AppError'; | ||
|
||
interface ITokenPayload { | ||
iss: string; | ||
sub: string; | ||
exp: number; | ||
iat: number; | ||
} | ||
|
||
export default function ensureAuthenticated(request: Request, _response: Response, next: NextFunction): void { | ||
const authHeader = request.headers.authorization; | ||
|
||
if (!authHeader) { throw new AppError('Token não enviado'); } | ||
|
||
const token = authHeader.split(' ')[1]; | ||
|
||
try { | ||
const decoded = verify(token, auth.jwt.secret as Secret); | ||
|
||
const { sub: id } = decoded as ITokenPayload; | ||
request.token = { id }; | ||
|
||
return next(); | ||
} catch (error) { | ||
throw new AppError('Token inválido'); | ||
} | ||
} |