Files
persionalBrand/apps/api/src/modules/session/session.controller.ts
T
que01 d0a0eeea8c
CI / Browser smoke (pull_request) Successful in 58s
CI / Static checks (pull_request) Successful in 1m32s
CI / API integration (pull_request) Successful in 1m27s
CI / Production build (pull_request) Successful in 2m7s
feat: 支持按品牌配置微信登录凭据
2026-10-23 22:07:39 +08:00

80 lines
2.5 KiB
TypeScript

import { Body, Controller, Get, Post, Query, Req, Res } from "@nestjs/common";
import type { FastifyReply, FastifyRequest } from "fastify";
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";
import { AdminLoginDto, WechatLoginDto } from "../../admin-auth.dto.js";
import { CustomerSessionService } from "./customer-session.service.js";
import { ChangeAdminPasswordDto } from "../../admin.dto.js";
@Controller("customer/session")
export class CustomerSessionController {
constructor(private readonly sessions: CustomerSessionService) {}
@Post("wechat")
async wechatLogin(
@Body() body: WechatLoginDto,
@Query("brandSlug") brandSlug?: string,
): Promise<{ token: string }> {
return this.sessions.loginWithWechat(body.code, brandSlug);
}
}
@Controller("admin/session")
export class AdminSessionController {
constructor(private readonly auth: AdminAuthService) {}
@AdminPublic()
@Post("login")
async login(
@Body() body: AdminLoginDto,
@Res({ passthrough: true }) reply: FastifyReply,
): Promise<{
displayName: string;
mustChangePassword: boolean;
role: string;
platformTemplateRole: "none" | "template_admin";
}> {
const { session, token } = await this.auth.login(body.username, body.password);
setAdminSessionCookie(reply, token);
return session;
}
@AdminTokenOnly()
@Get("current")
async current(@Req() request: FastifyRequest) {
return this.auth.currentSession(request);
}
@AdminTokenOnly()
@Post("logout")
async logout(
@Req() request: FastifyRequest,
@Res({ passthrough: true }) reply: FastifyReply,
): Promise<{ loggedOut: true }> {
const identity = requireAdminIdentity(request);
await this.auth.logout(identity.subject);
clearAdminSessionCookie(reply);
return { loggedOut: true };
}
@AdminTokenOnly()
@Post("change-password")
async changePassword(
@Req() request: FastifyRequest,
@Body() body: ChangeAdminPasswordDto,
@Res({ passthrough: true }) reply: FastifyReply,
): Promise<{ changed: true }> {
const identity = requireAdminIdentity(request);
const { token } = await this.auth.changePassword(
identity.subject,
identity.brandId,
body.currentPassword,
body.newPassword,
);
setAdminSessionCookie(reply, token);
return { changed: true };
}
}