fix: 修复首次登录会话恢复
This commit is contained in:
1 parent
4ef4be2071
commit
8e29932bc6
6 files changed
+746
-714
No files matched your search
@@ -59,7 +59,18 @@ export function Root() {
|
||||
}}
|
||||
/>
|
||||
);
|
||||
if (session.mustChangePassword) return <ChangePassword onComplete={() => setSession(null)} />;
|
||||
if (session.mustChangePassword)
|
||||
return (
|
||||
<ChangePassword
|
||||
onComplete={async () => {
|
||||
try {
|
||||
setSession(await createApiClient().getAdminSession());
|
||||
} catch {
|
||||
setSession(null);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
);
|
||||
return (
|
||||
<AdminRouter>
|
||||
<Dashboard
|
||||
|
||||
@@ -55,21 +55,30 @@ export class AdminAuthService {
|
||||
}
|
||||
|
||||
async currentSession(request: FastifyRequest): Promise<AdminCurrentSession> {
|
||||
const access = await this.access.requireBrand(request);
|
||||
const user = await this.prisma.adminUser.findUniqueOrThrow({
|
||||
where: { id: access.subject },
|
||||
select: {
|
||||
displayName: true,
|
||||
mustChangePassword: true,
|
||||
platformTemplateRole: true,
|
||||
},
|
||||
});
|
||||
// current 必须能返回首次登录态;不能先执行 requireBrand 把 mustChangePassword 用户挡在登录页。
|
||||
const access = await this.access.requireToken(request);
|
||||
const [user, membership] = await Promise.all([
|
||||
this.prisma.adminUser.findUniqueOrThrow({
|
||||
where: { id: access.subject },
|
||||
select: {
|
||||
displayName: true,
|
||||
mustChangePassword: true,
|
||||
platformTemplateRole: true,
|
||||
},
|
||||
}),
|
||||
this.prisma.brandMembership.findUnique({
|
||||
where: {
|
||||
adminUserId_brandId: { adminUserId: access.subject, brandId: access.brandId },
|
||||
},
|
||||
}),
|
||||
]);
|
||||
if (!membership?.isEnabled) throw new ForbiddenException("管理员没有当前品牌权限");
|
||||
return {
|
||||
authenticated: true,
|
||||
displayName: user.displayName,
|
||||
mustChangePassword: user.mustChangePassword,
|
||||
platformTemplateRole: user.platformTemplateRole,
|
||||
role: access.role,
|
||||
role: membership.role,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Body, Controller, Get, Post, Query, Req, Res } from "@nestjs/common";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { WechatAuthService } from "../../wechat-auth.js";
|
||||
import { AdminBrand, AdminPublic, AdminTokenOnly } from "../../admin-auth.decorators.js";
|
||||
import { AdminPublic, AdminTokenOnly } from "../../admin-auth.decorators.js";
|
||||
import { clearAdminSessionCookie, setAdminSessionCookie } from "../../admin-session-cookie.js";
|
||||
import { AdminAuthService } from "../../admin-auth.service.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
@@ -46,7 +46,7 @@ export class AdminSessionController {
|
||||
return session;
|
||||
}
|
||||
|
||||
@AdminBrand()
|
||||
@AdminTokenOnly()
|
||||
@Get("current")
|
||||
async current(@Req() request: FastifyRequest) {
|
||||
return this.auth.currentSession(request);
|
||||
|
||||
@@ -2285,17 +2285,29 @@ test("manually created member must change the initial password before normal use
|
||||
});
|
||||
assert.equal(login.statusCode, 201);
|
||||
assert.equal(JSON.parse(login.body).mustChangePassword, true);
|
||||
const firstLoginToken = sessionToken(login);
|
||||
const currentBeforeChange = await request("/admin/session/current", {
|
||||
headers: { authorization: `Bearer ${firstLoginToken}` },
|
||||
});
|
||||
assert.equal(currentBeforeChange.statusCode, 200);
|
||||
assert.equal(JSON.parse(currentBeforeChange.body).mustChangePassword, true);
|
||||
assert.equal(JSON.parse(currentBeforeChange.body).role, "editor");
|
||||
const blocked = await request("/admin/brands/members", {
|
||||
headers: { authorization: `Bearer ${sessionToken(login)}` },
|
||||
headers: { authorization: `Bearer ${firstLoginToken}` },
|
||||
});
|
||||
assert.equal(blocked.statusCode, 403);
|
||||
assert.match(errorMessage(blocked.body), /初始密码/);
|
||||
const changed = await request("/admin/session/change-password", {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${sessionToken(login)}` },
|
||||
headers: { authorization: `Bearer ${firstLoginToken}` },
|
||||
payload: { currentPassword: "initial-password-123", newPassword: "updated-password-123" },
|
||||
});
|
||||
assert.equal(changed.statusCode, 201);
|
||||
const currentAfterChange = await request("/admin/session/current", {
|
||||
headers: { authorization: `Bearer ${sessionToken(changed)}` },
|
||||
});
|
||||
assert.equal(currentAfterChange.statusCode, 200);
|
||||
assert.equal(JSON.parse(currentAfterChange.body).mustChangePassword, false);
|
||||
const updatedLogin = await request("/admin/session/login", {
|
||||
method: "POST",
|
||||
payload: { username, password: "updated-password-123" },
|
||||
|
||||
Reference in new issue
Block a user