80 lines
2.5 KiB
TypeScript
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 };
|
|
}
|
|
}
|