feat: 增加登录鉴权和固定角色权限
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import { unauthorized } from "../../shared/http-error";
|
||||
import { authRepository } from "./auth.repository";
|
||||
import type {
|
||||
AuthJwtPayload,
|
||||
AuthUser,
|
||||
EmployeeLoginAccount,
|
||||
LoginInput,
|
||||
SuperAdmin,
|
||||
} from "./auth.types";
|
||||
import { verifyPassword } from "./password";
|
||||
|
||||
function toAuthUser(admin: SuperAdmin): AuthUser {
|
||||
return {
|
||||
id: admin.id,
|
||||
username: admin.username,
|
||||
displayName: admin.displayName,
|
||||
accountType: "SUPER_ADMIN",
|
||||
roles: [
|
||||
{
|
||||
id: 0,
|
||||
code: "super_admin",
|
||||
name: "超级管理员",
|
||||
},
|
||||
],
|
||||
permissions: ["*"],
|
||||
canManage: true,
|
||||
};
|
||||
}
|
||||
|
||||
function toEmployeeAuthUser(employee: EmployeeLoginAccount): AuthUser {
|
||||
const canManage = employee.roles.some((role) => role.code === "admin");
|
||||
|
||||
return {
|
||||
id: employee.id,
|
||||
username: employee.username,
|
||||
displayName: employee.displayName,
|
||||
accountType: "EMPLOYEE",
|
||||
storeId: employee.storeId,
|
||||
storeName: employee.storeName,
|
||||
roles: employee.roles,
|
||||
permissions: canManage ? ["*"] : [],
|
||||
canManage,
|
||||
};
|
||||
}
|
||||
|
||||
function toJwtPayload(user: AuthUser): AuthJwtPayload {
|
||||
const subjectPrefix =
|
||||
user.accountType === "SUPER_ADMIN" ? "super_admin" : "employee";
|
||||
|
||||
return {
|
||||
sub: `${subjectPrefix}:${user.id}`,
|
||||
accountType: user.accountType,
|
||||
adminId: user.accountType === "SUPER_ADMIN" ? user.id : undefined,
|
||||
employeeId: user.accountType === "EMPLOYEE" ? user.id : undefined,
|
||||
username: user.username,
|
||||
roleCodes: user.roles.map((role) => role.code),
|
||||
permissions: user.permissions,
|
||||
canManage: user.canManage,
|
||||
};
|
||||
}
|
||||
|
||||
export const authService = {
|
||||
async login(input: LoginInput): Promise<{
|
||||
user: AuthUser;
|
||||
payload: AuthJwtPayload;
|
||||
}> {
|
||||
const admin = await authRepository.findActiveByUsername(input.username);
|
||||
|
||||
if (admin) {
|
||||
const passwordMatched = await verifyPassword(
|
||||
input.password,
|
||||
admin.passwordHash,
|
||||
);
|
||||
|
||||
if (!passwordMatched) {
|
||||
throw unauthorized("用户名或密码错误");
|
||||
}
|
||||
|
||||
await authRepository.updateLastLoginAt(admin.id);
|
||||
|
||||
const user = toAuthUser(admin);
|
||||
|
||||
return {
|
||||
user,
|
||||
payload: toJwtPayload(user),
|
||||
};
|
||||
}
|
||||
|
||||
const employee = await authRepository.findActiveEmployeeByPhone(
|
||||
input.username,
|
||||
);
|
||||
|
||||
if (!employee) {
|
||||
throw unauthorized("用户名或密码错误");
|
||||
}
|
||||
|
||||
const passwordMatched = await verifyPassword(
|
||||
input.password,
|
||||
employee.passwordHash,
|
||||
);
|
||||
|
||||
if (!passwordMatched) {
|
||||
throw unauthorized("用户名或密码错误");
|
||||
}
|
||||
|
||||
await authRepository.updateEmployeeLastLoginAt(employee.id);
|
||||
|
||||
const user = toEmployeeAuthUser(employee);
|
||||
|
||||
return {
|
||||
user,
|
||||
payload: toJwtPayload(user),
|
||||
};
|
||||
},
|
||||
|
||||
async getCurrentUser(payload: AuthJwtPayload): Promise<AuthUser> {
|
||||
if (payload.accountType === "SUPER_ADMIN" && payload.adminId) {
|
||||
const admin = await authRepository.findActiveById(payload.adminId);
|
||||
|
||||
if (!admin) {
|
||||
throw unauthorized("登录已失效,请重新登录");
|
||||
}
|
||||
|
||||
return toAuthUser(admin);
|
||||
}
|
||||
|
||||
if (payload.accountType === "EMPLOYEE" && payload.employeeId) {
|
||||
const employee = await authRepository.findActiveEmployeeById(
|
||||
payload.employeeId,
|
||||
);
|
||||
|
||||
if (!employee) {
|
||||
throw unauthorized("登录已失效,请重新登录");
|
||||
}
|
||||
|
||||
return toEmployeeAuthUser(employee);
|
||||
}
|
||||
|
||||
throw unauthorized("登录已失效,请重新登录");
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user