-
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.
- Loading branch information
Showing
4 changed files
with
40 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
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,35 @@ | ||
import { Response, NextFunction, Request } from 'express'; | ||
import jwt from 'jsonwebtoken'; | ||
import getToken from '../utils/getToken'; | ||
import { Token, Permission } from '../types'; | ||
import { unauthorized, unauthenticated } from '../utils/responses'; | ||
import errorHandler from '../utils/errorHandler'; | ||
|
||
export default (permissions: Array<string>) => async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const token = getToken(req); | ||
if (token) { | ||
const decoded = jwt.verify(token, process.env.APP_TOKEN_SECRET) as Token; | ||
|
||
req.user = decoded; | ||
|
||
if (!permissions) { | ||
return next(); | ||
} | ||
|
||
if (decoded.permissions === Permission.ADMIN) { | ||
return next(); | ||
} | ||
for (const permission of permissions) { | ||
Check failure on line 23 in src/middlewares/hasPermissionInList.ts GitHub Actions / lint (18)
Check failure on line 23 in src/middlewares/hasPermissionInList.ts GitHub Actions / lint (18)
|
||
if (decoded.permissions === permission) { | ||
return next(); | ||
} | ||
} | ||
return unauthorized(res); | ||
} | ||
|
||
return unauthenticated(res); | ||
} catch (err) { | ||
return errorHandler(res, err); | ||
} | ||
}; |
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