File size: 3,693 Bytes
f3abb0d
 
 
 
 
db3d93b
 
 
 
 
f3abb0d
 
 
d201b18
f3abb0d
 
 
 
 
 
d201b18
f3abb0d
 
 
db3d93b
f3abb0d
 
db3d93b
 
 
f3abb0d
 
db3d93b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3abb0d
 
db3d93b
f3abb0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Controller, Post, Body, Req, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { UserAuthService } from './auth.service';
import { UserRegisterDto } from './dto/register.dto';
import { UserLoginDto } from './dto/login.dto';
import { VerifyEmailOtpDto } from './dto/verify-email-otp.dto';
import { ResendEmailOtpDto } from './dto/resend-email-otp.dto';
import { RegisterPhoneDto } from './dto/register-phone.dto';
import { VerifyPhoneOtpDto } from './dto/verify-phone-otp.dto';
import { CompleteProfileDto } from './dto/complete-profile.dto';
import { UserRefreshTokenDto } from './dto/refresh-token.dto';
import { Public } from 'src/shared/decorators/public.decorator';
import { GetUser } from 'src/shared/decorators/get-user.decorator';
import { LogToDiscord } from 'src/shared/decorators/log-to-discord.decorator';
import type { UserSessionType } from 'src/shared/types/user-session.type';
import { UserAuthGuard } from './guards/user-auth.guard';

@ApiTags('User: Authentication')
@Controller('user/auth')
@UseGuards(UserAuthGuard)
@LogToDiscord()
export class UserAuthController {
  constructor(private readonly userAuthService: UserAuthService) {}

  // PHASE 1: Initial Registration
  @Public()
  @Post('register')
  @ApiOperation({ summary: 'Register a new user (Step 1/5)' })
  async register(@Body() registerDto: UserRegisterDto) {
    return this.userAuthService.register(registerDto);
  }

  // PHASE 2: Email Verification
  @Public()
  @Post('verify-email')
  @ApiOperation({ summary: 'Verify email with OTP (Step 2/5)' })
  async verifyEmail(@Body() verifyDto: VerifyEmailOtpDto) {
    return this.userAuthService.verifyEmailOtp(verifyDto);
  }

  @Public()
  @Post('resend-email-otp')
  @ApiOperation({ summary: 'Resend email OTP' })
  async resendEmailOtp(@Body() resendDto: ResendEmailOtpDto) {
    return this.userAuthService.resendEmailOtp(resendDto);
  }

  // PHASE 3: Phone Registration
  @Public()
  @Post('register-phone')
  @ApiOperation({ summary: 'Register phone number (Step 3/5)' })
  async registerPhone(@Body() registerPhoneDto: RegisterPhoneDto) {
    return this.userAuthService.registerPhone(registerPhoneDto);
  }

  // PHASE 4: Phone Verification
  @Public()
  @Post('verify-phone')
  @ApiOperation({ summary: 'Verify phone with OTP (Step 4/5)' })
  async verifyPhone(@Body() verifyDto: VerifyPhoneOtpDto) {
    return this.userAuthService.verifyPhoneOtp(verifyDto);
  }

  // PHASE 5: Complete Profile
  @Public()
  @Post('complete-profile')
  @ApiOperation({ summary: 'Complete profile and activate account (Step 5/5)' })
  async completeProfile(
    @Body() completeDto: CompleteProfileDto,
    @Req() request: any,
  ) {
    return this.userAuthService.completeProfile(completeDto, request);
  }

  // Login (Only for verified users)
  @Public()
  @Post('login')
  @ApiOperation({ summary: 'Login user (Only verified accounts)' })
  async login(@Body() loginDto: UserLoginDto, @Req() request: any) {
    return this.userAuthService.login(loginDto, request);
  }

  @ApiBearerAuth()
  @Post('logout')
  @ApiOperation({ summary: 'Logout user' })
  async logout(@GetUser() sessionData: UserSessionType) {
    return this.userAuthService.logout(sessionData);
  }

  @Public()
  @Post('refresh-token')
  @ApiOperation({ summary: 'Refresh access token' })
  async refreshToken(@Body() refreshTokenDto: UserRefreshTokenDto) {
    return this.userAuthService.refreshToken(refreshTokenDto);
  }

  @ApiBearerAuth()
  @Post('profile')
  @ApiOperation({ summary: 'Get user profile' })
  async getProfile(@GetUser() sessionData: UserSessionType) {
    return { user: sessionData.user };
  }
}