Files
mp-pilates/packages/server/src/auth/auth.controller.ts
2026-04-19 22:23:23 +08:00

51 lines
1.2 KiB
TypeScript

import {
Body,
Controller,
HttpCode,
HttpStatus,
Post,
Request,
UseGuards,
} from '@nestjs/common'
import type { UserProfileResponse } from '@mp-pilates/shared'
import { AuthService } from './auth.service'
import { LoginDto } from './dto/login.dto'
import { BindPhoneDto } from './dto/bind-phone.dto'
import { JwtAuthGuard } from './jwt-auth.guard'
import { AuthenticatedUser } from './jwt.strategy'
import { User } from '@prisma/client'
interface AuthenticatedRequest {
user: AuthenticatedUser
}
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('login')
@HttpCode(HttpStatus.OK)
async login(@Body() loginDto: LoginDto): Promise<{ token: string; user: UserProfileResponse; isNewUser: boolean }> {
return this.authService.login(
loginDto.code,
loginDto.nickname,
loginDto.avatarUrl,
loginDto.inviterId,
)
}
@Post('phone')
@UseGuards(JwtAuthGuard)
@HttpCode(HttpStatus.OK)
async bindPhone(
@Request() req: AuthenticatedRequest,
@Body() bindPhoneDto: BindPhoneDto,
): Promise<User> {
return this.authService.bindPhone(
req.user.sub,
bindPhoneDto.encryptedData,
bindPhoneDto.iv,
)
}
}