profile.controller.ts
import { Body, Controller, Param, Post, UseGuards } from '@nestjs/common';
import { Profile } from '@/profile/profile.service';
import { JwtAuthGuard } from '@/auth/guards/jwt.guard';
@Controller('api')
export class ProfileController {
constructor(private profile: Profile) {
}
@UseGuards(JwtAuthGuard)
@Post('user/:userId/profile')
async postProfile(@Param() params, @Body() body) {
await this.profile.add(params.userId, body);
}
}
auth.controller.ts
import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common';
import { OAuthGuard } from '@/auth/guards/oauth.guard';
import { Response } from 'express';
import { DbService } from '@/db/db.service';
import { eq } from 'drizzle-orm';
import { users } from '@/db/schema';
import { JwtAuthService } from '@/auth/jwt-auth.service';
@Controller('auth')
export class AuthController {
constructor(private readonly dbService: DbService, private jwtAuth: JwtAuthService) {
}
@Get('login')
@UseGuards(OAuthGuard)
async login() {
// This route will redirect the user to the OAuth provider
// The guard will handle the redirection
}
@Get('callback')
@UseGuards(OAuthGuard)
async callback(@Req() req, @Res() res: Response) {
// Access the user information from the request
const user = await this.dbService.database.query.users.findFirst({ where: eq(users.providerId, req.user.id) });
const { accessToken } = this.jwtAuth.login(user.id, user.username);
res.cookie('jwt', accessToken, {
httpOnly: true,
});
if (user.isActive) {
res.redirect(`${process.env.FRONTEND_URL}/login-success?id=${user.id}`);
} else {
res.redirect(`${process.env.FRONTEND_URL}/profile?id=${user.id}`);
}
}
}