-
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 a27339a
Showing
7 changed files
with
203 additions
and
109 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.findSessionByUserId( | ||
decoded.id | ||
); | ||
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, | ||
}); | ||
} | ||
}; | ||
}; |
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,31 +1,46 @@ | ||
/* eslint-disable comma-dangle */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import Users from "../../../databases/models/users" | ||
import Session from "../../../databases/models/session" | ||
|
||
const createUser = async (body:any) =>{ | ||
return await Users.create(body) | ||
} | ||
|
||
|
||
const findUserByAttributes = async (key:string, value:any) =>{ | ||
return await Users.findOne({ where: { [key]: value} }) | ||
} | ||
|
||
const UpdateUserByAttributes = async (updatedKey:string, updatedValue:any, whereKey:string, whereValue:any) =>{ | ||
await Users.update({ [updatedKey]: updatedValue }, { where: { [whereKey]: whereValue} }); | ||
return await findUserByAttributes(whereKey, whereValue) | ||
} | ||
import Users from "../../../databases/models/users"; | ||
import Session from "../../../databases/models/session"; | ||
|
||
const createUser = async (body: any) => { | ||
return await Users.create(body); | ||
}; | ||
|
||
const findUserByAttributes = async (key: string, value: any) => { | ||
return await Users.findOne({ where: { [key]: value } }); | ||
}; | ||
|
||
const UpdateUserByAttributes = async ( | ||
updatedKey: string, | ||
updatedValue: any, | ||
whereKey: string, | ||
whereValue: any | ||
) => { | ||
await Users.update( | ||
{ [updatedKey]: updatedValue }, | ||
{ where: { [whereKey]: whereValue } } | ||
); | ||
return await findUserByAttributes(whereKey, whereValue); | ||
}; | ||
|
||
const createSession = async (body: any) => { | ||
return await Session.create(body); | ||
} | ||
|
||
const findSessionByUserId = async( userId:number ) => { | ||
return await Session.findOne({ where: { userId } }); | ||
} | ||
|
||
const destroySession = async (userId: number, token:string) =>{ | ||
return await Session.destroy({ where: {userId, token } }); | ||
} | ||
|
||
export default { createUser, createSession, findUserByAttributes, destroySession, UpdateUserByAttributes, findSessionByUserId } | ||
return await Session.create(body); | ||
}; | ||
|
||
const findSessionByUserId = async (userId: number) => { | ||
return await Session.findOne({ where: { userId } }); | ||
}; | ||
|
||
const destroySession = async (userId: number, token: string) => { | ||
return await Session.destroy({ where: { userId, token } }); | ||
}; | ||
|
||
export default { | ||
createUser, | ||
createSession, | ||
findUserByAttributes, | ||
destroySession, | ||
UpdateUserByAttributes, | ||
findSessionByUserId, | ||
}; |