Spaces:
Paused
Paused
| import { | |
| Controller, | |
| Post, | |
| Body, | |
| UsePipes, | |
| ValidationPipe, | |
| UnauthorizedException, | |
| Get, | |
| Req, | |
| Res, | |
| UseGuards, | |
| } from '@nestjs/common'; | |
| import { AuthService } from './auth.service'; | |
| import { CreateUserDto } from '../users/dto/create-user.dto'; | |
| import { UsersService } from '../users/users.service'; | |
| import { LoginDto } from './dto/login.dto'; | |
| import { AuthGuard } from '@nestjs/passport'; | |
| import { ConfigService } from '@nestjs/config'; | |
| ('auth') | |
| export class AuthController { | |
| constructor( | |
| private authService: AuthService, | |
| private usersService: UsersService, | |
| private configService: ConfigService, | |
| ) {} | |
| ('login') | |
| (new ValidationPipe()) | |
| async login(() loginDto: LoginDto) { | |
| const user = await this.authService.validateUser( | |
| loginDto.username, | |
| loginDto.pass, | |
| ); | |
| if (!user) { | |
| throw new UnauthorizedException('Invalid credentials'); | |
| } | |
| return this.authService.login(user); | |
| } | |
| ('register') | |
| (new ValidationPipe()) | |
| async register(() createUserDto: CreateUserDto) { | |
| return this.usersService.create(createUserDto); | |
| } | |
| ('twitter') | |
| (AuthGuard('twitter')) | |
| async twitterLogin() { | |
| // Initiates the Twitter OAuth flow | |
| } | |
| ('twitter/callback') | |
| (AuthGuard('twitter')) | |
| async twitterLoginCallback(() req, () res) { | |
| const jwt = await this.authService.login(req.user); | |
| const frontendUrl = | |
| this.configService.get<string>('FRONTEND_URL') || 'http://localhost:3000'; | |
| res.redirect(`${frontendUrl}/auth/success?token=${jwt.access_token}`); | |
| } | |
| } | |