eventnaija / src /auth /auth.controller.ts
Auspicious14's picture
feat: initialize complete EventNaija backend project
07441a7
Raw
History Blame Contribute Delete
1.69 kB
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';
@Controller('auth')
export class AuthController {
constructor(
private authService: AuthService,
private usersService: UsersService,
private configService: ConfigService,
) {}
@Post('login')
@UsePipes(new ValidationPipe())
async login(@Body() loginDto: LoginDto) {
const user = await this.authService.validateUser(
loginDto.username,
loginDto.pass,
);
if (!user) {
throw new UnauthorizedException('Invalid credentials');
}
return this.authService.login(user);
}
@Post('register')
@UsePipes(new ValidationPipe())
async register(@Body() createUserDto: CreateUserDto) {
return this.usersService.create(createUserDto);
}
@Get('twitter')
@UseGuards(AuthGuard('twitter'))
async twitterLogin() {
// Initiates the Twitter OAuth flow
}
@Get('twitter/callback')
@UseGuards(AuthGuard('twitter'))
async twitterLoginCallback(@Req() req, @Res() 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}`);
}
}