update
This commit is contained in:
@@ -4,6 +4,8 @@ import jwt from "jsonwebtoken";
|
||||
import dotenv from "dotenv";
|
||||
import User from "../../../models/User";
|
||||
import { JWT_SECRET } from "../../../core/constants";
|
||||
import Role from "../../../models/Role";
|
||||
import Permission from "../../../models/Permission";
|
||||
dotenv.config();
|
||||
|
||||
class AuthServiceClass {
|
||||
@@ -39,6 +41,46 @@ class AuthServiceClass {
|
||||
);
|
||||
}
|
||||
}
|
||||
async Me(req:any) {
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!userId) {
|
||||
throw new createHttpError.Unauthorized("کاربر احراز هویت نشده است");
|
||||
}
|
||||
|
||||
const user = await User.findByPk(userId, {
|
||||
attributes: ["id", "fullname", "mobile", "roleId"],
|
||||
include: [
|
||||
{
|
||||
model: Role,
|
||||
as: "role",
|
||||
attributes: ["id", "name"],
|
||||
include: [
|
||||
{
|
||||
model: Permission,
|
||||
as: "permissions",
|
||||
attributes: ["name"],
|
||||
through: { attributes: [] },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new createHttpError.NotFound("کاربر پیدا نشد");
|
||||
}
|
||||
|
||||
const permissions = user.role?.permissions?.map((p: any) => p.name) || [];
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
fullname: user.fullname,
|
||||
mobile: user.mobile,
|
||||
role: user.role?.name,
|
||||
permissions,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const AuthService = new AuthServiceClass();
|
||||
|
||||
Reference in New Issue
Block a user