-
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
5e7953f
commit e5bfafe
Showing
7 changed files
with
205 additions
and
104 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,64 @@ | ||
/* eslint-disable comma-dangle */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { UsersAttributes } from "../databases/models/users"; | ||
import authRepository from "../modules/auth/repository/authRepositories"; | ||
import httpStatus from "http-status"; | ||
import { decodeToken } from "../helpers"; | ||
import Session from "../databases/models/session"; | ||
|
||
interface ExtendedRequest extends Request { | ||
user: UsersAttributes; | ||
session: Session; | ||
} | ||
|
||
export const userAuthorization = 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) { | ||
res | ||
.status(httpStatus.UNAUTHORIZED) | ||
.json({ status: httpStatus.UNAUTHORIZED, message: "Not authorized" }); | ||
} | ||
|
||
const decoded: any = await decodeToken(token); | ||
|
||
const session: Session = await authRepository.findSessionByUserIdAndToken( | ||
decoded.id, token | ||
); | ||
if (!session) { | ||
res | ||
.status(httpStatus.UNAUTHORIZED) | ||
.json({ status: httpStatus.UNAUTHORIZED, message: "Not authorized" }); | ||
} | ||
|
||
const user = await authRepository.findUserByAttributes("id", decoded.id); | ||
|
||
if (!user) { | ||
res | ||
.status(httpStatus.UNAUTHORIZED) | ||
.json({ status: httpStatus.UNAUTHORIZED, message: "Not authorized" }); | ||
} | ||
|
||
if (!roles.includes(user.role)) { | ||
res | ||
.status(httpStatus.UNAUTHORIZED) | ||
.json({ status: httpStatus.UNAUTHORIZED, message: "Not authorized" }); | ||
} | ||
|
||
req.user = user; | ||
req.session = session; | ||
next(); | ||
} catch (error: any) { | ||
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ | ||
status: httpStatus.INTERNAL_SERVER_ERROR, | ||
message: error.message, | ||
}); | ||
} | ||
}; | ||
}; |
Oops, something went wrong.