This commit is contained in:
2026-06-16 11:32:07 +03:30
parent fdda306e85
commit b5bfd13ea2
17 changed files with 626 additions and 358 deletions

View File

@@ -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();