从旧 Laravel 5.7 重构为现代技术栈,对接现有 MySQL。 - 技术栈: Next.js 15 App Router + TypeScript + Prisma + Auth.js + Tailwind v4 - 认证: 复用旧库 admins 的 bcrypt 密码,Auth.js Credentials - 客户管理: 列表(搜索/报单区间/分页)、增改删、Excel 导入导出 - 生日提醒: 阴阳历(lunar-typescript),口径对齐旧 overtrue 库 - RBAC: 管理员/角色/权限(菜单树) 完整 CRUD + 分配 - 控制台: 统计卡 + 月度新增报单柱状图 - 个人中心: 改昵称/改密码 - 现代 CMS 界面 + 联动支付品牌(logo/登录/底部/icon) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import NextAuth from "next-auth";
|
||
import Credentials from "next-auth/providers/credentials";
|
||
import bcrypt from "bcryptjs";
|
||
import { prisma } from "@/lib/prisma";
|
||
import { authConfig } from "@/lib/auth.config";
|
||
|
||
/**
|
||
* 读取某个后台账号的角色名与权限码(走现有 spatie 关联表)。
|
||
*/
|
||
async function loadAcl(adminId: number) {
|
||
const modelRoles = await prisma.modelHasRole.findMany({
|
||
where: { modelId: adminId, modelType: "App\\Model\\Admin" },
|
||
select: { roleId: true },
|
||
});
|
||
const roleIds = modelRoles.map((r) => r.roleId);
|
||
if (roleIds.length === 0) return { roles: [], permissions: [] };
|
||
|
||
const [roles, rolePerms] = await Promise.all([
|
||
prisma.role.findMany({ where: { id: { in: roleIds } }, select: { name: true } }),
|
||
prisma.roleHasPermission.findMany({
|
||
where: { roleId: { in: roleIds } },
|
||
select: { permissionId: true },
|
||
}),
|
||
]);
|
||
|
||
const permIds = [...new Set(rolePerms.map((p) => p.permissionId))];
|
||
const perms = permIds.length
|
||
? await prisma.permission.findMany({
|
||
where: { id: { in: permIds } },
|
||
select: { name: true },
|
||
})
|
||
: [];
|
||
|
||
return {
|
||
roles: roles.map((r) => r.name),
|
||
permissions: perms.map((p) => p.name),
|
||
};
|
||
}
|
||
|
||
export const { handlers, auth, signIn, signOut } = NextAuth({
|
||
...authConfig,
|
||
providers: [
|
||
Credentials({
|
||
credentials: {
|
||
username: { label: "用户名" },
|
||
password: { label: "密码", type: "password" },
|
||
},
|
||
authorize: async (credentials) => {
|
||
const username = String(credentials?.username ?? "").trim();
|
||
const password = String(credentials?.password ?? "");
|
||
if (!username || !password) return null;
|
||
|
||
const admin = await prisma.admin.findUnique({ where: { username } });
|
||
// 软删除账号不允许登录
|
||
if (!admin || admin.deletedAt) return null;
|
||
|
||
// 复用 Laravel bcrypt($2y$)哈希,老账号密码原样可登录
|
||
const ok = await bcrypt.compare(password, admin.password);
|
||
if (!ok) return null;
|
||
|
||
const acl = await loadAcl(admin.id);
|
||
return {
|
||
id: String(admin.id),
|
||
name: admin.nickname || admin.username,
|
||
username: admin.username,
|
||
image: admin.face ?? null,
|
||
roles: acl.roles,
|
||
permissions: acl.permissions,
|
||
};
|
||
},
|
||
}),
|
||
],
|
||
});
|