forked from fga-eps-mds/2022-2-Alectrion-EquipamentApi
-
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.
#149 feat: validacao de usuario de consulta
- Loading branch information
1 parent
2b22d6d
commit 8360bbb
Showing
3 changed files
with
78 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Request, Response } from 'express' | ||
import { decode } from 'jsonwebtoken' | ||
|
||
export const checkIfIsQueryUser = ( | ||
req: Request, | ||
resp: Response, | ||
next: () => void | ||
): void => { | ||
const token = req.headers.authorization?.split(' ')[1] | ||
if (!token) { | ||
resp.status(401).json({ error: 'Token não informado' }) | ||
return | ||
} | ||
|
||
const { userId, role } = decode(token) as { userId: string; role: string } | ||
|
||
if (role === 'consulta') { | ||
resp.status(403).json({ | ||
error: 'Usuários de consulta não têm acesso a essa funcionalidade' | ||
}) | ||
return | ||
} | ||
|
||
req.userId = userId | ||
next() | ||
} |
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