Files
dukang/server/dukang-api/src/modules/iam/auth.controller.ts
T

142 lines
4.2 KiB
TypeScript

import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
import type { Request } from 'express';
import { AuthService } from './auth.service';
import {
BindPhoneDto,
BindWechatPhoneDto,
BootstrapSessionDto,
LoginSmsDto,
LoginWechatDto,
RefreshTokenDto,
SendSmsDto,
} from './dto/auth.dto';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { OptionalJwtAuthGuard } from '../../common/guards/optional-jwt-auth.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { AuthUser } from '../../common/guards/jwt-auth.guard';
import { ClientApp } from '@dukang/shared-types';
@Controller()
export class UserAuthController {
constructor(private readonly authService: AuthService) {}
@Post('auth/session/bootstrap')
bootstrap(@Body() dto: BootstrapSessionDto) {
return this.authService.bootstrapSession(dto.deviceKey, ClientApp.USER_H5);
}
@Post('auth/token/refresh')
refresh(@Body() dto: RefreshTokenDto) {
return this.authService.refreshAccessToken(dto.refreshToken, ClientApp.USER_H5);
}
@Post('auth/sms/send')
sendSms(@Body() dto: SendSmsDto) {
return this.authService.sendSms(dto.phone, dto.scene);
}
@Post('auth/login/sms')
@UseGuards(OptionalJwtAuthGuard)
login(@Req() req: Request, @Body() dto: LoginSmsDto) {
const guest = (req as Request & { user?: AuthUser }).user;
const guestId = guest?.actorType === 'USER' ? guest.actorId : undefined;
return this.authService.loginUser(dto.phone, dto.code, ClientApp.USER_H5, guestId);
}
@Post('auth/phone/bind')
@UseGuards(JwtAuthGuard)
bindPhone(@CurrentUser() user: AuthUser, @Body() dto: BindPhoneDto) {
return this.authService.bindPhone(user.actorId, dto.phone, dto.code, ClientApp.USER_H5);
}
@Post('auth/login/wechat')
@UseGuards(OptionalJwtAuthGuard)
wechatLogin(@Req() req: Request, @Body() dto: LoginWechatDto) {
const guest = (req as Request & { user?: AuthUser }).user;
const guestId = guest?.actorType === 'USER' ? guest.actorId : undefined;
return this.authService.loginUserWechat(
dto.code,
ClientApp.USER_H5,
dto.platform ?? 'h5',
guestId,
);
}
@Post('auth/wechat/bind-phone')
bindPhoneLegacy(@Body() dto: BindWechatPhoneDto) {
return this.authService.bindWechatPhone(
dto.wxSessionKey,
dto.phone,
dto.code,
ClientApp.USER_H5,
);
}
@Get('auth/me')
@UseGuards(JwtAuthGuard)
me(@CurrentUser() user: AuthUser) {
return this.authService.getMe(user.actorType, user.actorId);
}
}
@Controller('shop/auth')
export class ShopAuthController {
constructor(private readonly authService: AuthService) {}
@Post('sms/send')
sendSms(@Body() dto: SendSmsDto) {
return this.authService.sendSms(dto.phone, dto.scene);
}
@Post('login/sms')
login(@Body() dto: LoginSmsDto) {
return this.authService.loginStore(dto.phone, dto.code, ClientApp.SHOP_H5);
}
@Post('login/wechat')
wechatLogin(@Body() dto: LoginWechatDto) {
return this.authService.loginStoreWechat(dto.code, ClientApp.SHOP_H5, dto.platform ?? 'h5');
}
}
@Controller('partner/auth')
export class PartnerAuthController {
constructor(private readonly authService: AuthService) {}
@Post('sms/send')
sendSms(@Body() dto: SendSmsDto) {
return this.authService.sendSms(dto.phone, dto.scene);
}
@Post('login/sms')
login(@Body() dto: LoginSmsDto) {
return this.authService.loginPartner(dto.phone, dto.code, ClientApp.PARTNER_H5);
}
@Post('login/wechat')
@UseGuards(OptionalJwtAuthGuard)
wechatLogin(@Req() req: Request, @Body() dto: LoginWechatDto) {
const user = (req as Request & { user?: AuthUser }).user;
if (user?.actorType === 'PARTNER') {
return this.authService.bindPartnerWechat(
user.actorId,
dto.code,
ClientApp.PARTNER_H5,
dto.platform ?? 'h5',
);
}
return this.authService.loginPartnerWechat(dto.code, ClientApp.PARTNER_H5, dto.platform ?? 'h5');
}
}
@Controller('user')
export class UserProfileController {
constructor(private readonly authService: AuthService) {}
@Get('profile')
@UseGuards(JwtAuthGuard)
profile(@CurrentUser() user: AuthUser) {
return this.authService.getMe(user.actorType, user.actorId);
}
}