-
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
1 parent
88b8f15
commit 6f1b167
Showing
6 changed files
with
100 additions
and
56 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint-disable comma-dangle */ | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
|
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 @@ | ||
/* eslint-disable comma-dangle */ | ||
import { config } from "dotenv"; | ||
import { Sequelize } from "sequelize"; | ||
|
||
|
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,50 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { Request, Response, NextFunction } from "express"; | ||
import jwt from "jsonwebtoken"; | ||
import { UsersAttributes } from "../databases/models/users"; | ||
import authRepository from "../modules/auth/repository/authRepositories"; | ||
|
||
const SECRET: string = process.env.JWT_SECRET; | ||
|
||
interface ExtendedRequest extends Request { | ||
user: UsersAttributes; | ||
} | ||
|
||
export const authorize = function (roles: string[]) { | ||
return async (req: ExtendedRequest, res: Response, next: NextFunction) => { | ||
try { | ||
let token: string; | ||
if (req.headers.authorization?.startsWith("Bearer")) { | ||
token = req.headers.authorization.split(" ").at(-1); | ||
} | ||
|
||
if (!token) throw new Error("Login to get access to this resource"); | ||
|
||
const decoded: any = await jwt.verify(token, SECRET); | ||
|
||
const user = await authRepository.findUserByAttributes("id", decoded.id); | ||
|
||
if (!user) { | ||
throw new Error("User belonging to this token does not exist"); | ||
} | ||
|
||
if (!roles.includes(user.role)) { | ||
throw new Error("You're not allowed to access this resource"); | ||
} | ||
|
||
req.user = user; | ||
next(); | ||
} catch (err: any) { | ||
let message: string; | ||
if ( | ||
err.name === "JsonWebTokenError" || | ||
err.name === "TokenExpiredError" | ||
) { | ||
message = "Invalid token. Log in again to get a new one"; | ||
} else { | ||
message = err.message; | ||
} | ||
res.status(401).json({ ok: false, status: "fail", message: message }); | ||
} | ||
}; | ||
}; |