fix: 修复首次登录会话恢复

This commit is contained in:
que01 committed 2026-10-19 10:58:26 +08:00
1 parent 4ef4be2071
commit 8e29932bc6
6 files changed
+746 -714

No files matched your search

@@ -13,7 +13,7 @@
"artifacts": [ "artifacts": [
{ {
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log", "path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "75d40d84242c8be3218ddc00b520f44bea3e5accb75a7b170e2cc35959aba426" "sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
}, },
{ {
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/admin-editor.log", "path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/admin-editor.log",
@@ -30,7 +30,7 @@
"artifacts": [ "artifacts": [
{ {
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log", "path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "75d40d84242c8be3218ddc00b520f44bea3e5accb75a7b170e2cc35959aba426" "sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
} }
], ],
"exitCode": 0 "exitCode": 0
@@ -105,7 +105,7 @@
"artifacts": [ "artifacts": [
{ {
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log", "path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "75d40d84242c8be3218ddc00b520f44bea3e5accb75a7b170e2cc35959aba426" "sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
}, },
{ {
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/admin-editor.log", "path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/admin-editor.log",
@@ -122,7 +122,7 @@
"artifacts": [ "artifacts": [
{ {
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log", "path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "75d40d84242c8be3218ddc00b520f44bea3e5accb75a7b170e2cc35959aba426" "sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
} }
], ],
"exitCode": 0 "exitCode": 0
@@ -197,7 +197,7 @@
"artifacts": [ "artifacts": [
{ {
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log", "path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "75d40d84242c8be3218ddc00b520f44bea3e5accb75a7b170e2cc35959aba426" "sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
}, },
{ {
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/admin-editor.log", "path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/admin-editor.log",
@@ -214,7 +214,7 @@
"artifacts": [ "artifacts": [
{ {
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log", "path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "75d40d84242c8be3218ddc00b520f44bea3e5accb75a7b170e2cc35959aba426" "sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
} }
], ],
"exitCode": 0 "exitCode": 0
@@ -292,5 +292,5 @@
} }
} }
}, },
"sourceTreeHash": "616999880b203df896694cddf36c14ed8ed51eb24ee320850f80898b57503ad3" "sourceTreeHash": "2479cbced319d41ec0ab2a23d913c5104b162eb8f904cd5807b61628dc55e71e"
} }
File diff suppressed because it is too large. Load diff
+12 -1
View File
@@ -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 ( return (
<AdminRouter> <AdminRouter>
<Dashboard <Dashboard
+19 -10
View File
@@ -55,21 +55,30 @@ export class AdminAuthService {
} }
async currentSession(request: FastifyRequest): Promise<AdminCurrentSession> { async currentSession(request: FastifyRequest): Promise<AdminCurrentSession> {
const access = await this.access.requireBrand(request); // current 必须能返回首次登录态;不能先执行 requireBrand 把 mustChangePassword 用户挡在登录页。
const user = await this.prisma.adminUser.findUniqueOrThrow({ const access = await this.access.requireToken(request);
where: { id: access.subject }, const [user, membership] = await Promise.all([
select: { this.prisma.adminUser.findUniqueOrThrow({
displayName: true, where: { id: access.subject },
mustChangePassword: true, select: {
platformTemplateRole: true, 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 { return {
authenticated: true, authenticated: true,
displayName: user.displayName, displayName: user.displayName,
mustChangePassword: user.mustChangePassword, mustChangePassword: user.mustChangePassword,
platformTemplateRole: user.platformTemplateRole, 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 { Body, Controller, Get, Post, Query, Req, Res } from "@nestjs/common";
import type { FastifyReply, FastifyRequest } from "fastify"; import type { FastifyReply, FastifyRequest } from "fastify";
import { WechatAuthService } from "../../wechat-auth.js"; 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 { clearAdminSessionCookie, setAdminSessionCookie } from "../../admin-session-cookie.js";
import { AdminAuthService } from "../../admin-auth.service.js"; import { AdminAuthService } from "../../admin-auth.service.js";
import { requireAdminIdentity } from "../../admin-access.identity.js"; import { requireAdminIdentity } from "../../admin-access.identity.js";
@@ -46,7 +46,7 @@ export class AdminSessionController {
return session; return session;
} }
@AdminBrand() @AdminTokenOnly()
@Get("current") @Get("current")
async current(@Req() request: FastifyRequest) { async current(@Req() request: FastifyRequest) {
return this.auth.currentSession(request); return this.auth.currentSession(request);
+14 -2
View File
@@ -2285,17 +2285,29 @@ test("manually created member must change the initial password before normal use
}); });
assert.equal(login.statusCode, 201); assert.equal(login.statusCode, 201);
assert.equal(JSON.parse(login.body).mustChangePassword, true); 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", { const blocked = await request("/admin/brands/members", {
headers: { authorization: `Bearer ${sessionToken(login)}` }, headers: { authorization: `Bearer ${firstLoginToken}` },
}); });
assert.equal(blocked.statusCode, 403); assert.equal(blocked.statusCode, 403);
assert.match(errorMessage(blocked.body), /初始密码/); assert.match(errorMessage(blocked.body), /初始密码/);
const changed = await request("/admin/session/change-password", { const changed = await request("/admin/session/change-password", {
method: "POST", method: "POST",
headers: { authorization: `Bearer ${sessionToken(login)}` }, headers: { authorization: `Bearer ${firstLoginToken}` },
payload: { currentPassword: "initial-password-123", newPassword: "updated-password-123" }, payload: { currentPassword: "initial-password-123", newPassword: "updated-password-123" },
}); });
assert.equal(changed.statusCode, 201); 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", { const updatedLogin = await request("/admin/session/login", {
method: "POST", method: "POST",
payload: { username, password: "updated-password-123" }, payload: { username, password: "updated-password-123" },