refactor: created utlility functions for setting cookies and handling redirects
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
import { Body, Controller, Get, HttpStatus, Post, Res } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Post,
|
||||
Res,
|
||||
} from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { signInMagicDto } from './dto/signin-magic.dto';
|
||||
import { verifyMagicDto } from './dto/verify-magic.dto';
|
||||
import { Response } from 'express';
|
||||
import * as E from 'fp-ts/Either';
|
||||
import { authCookieHandler, throwHTTPErr } from 'src/utils';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
@@ -10,25 +20,15 @@ export class AuthController {
|
||||
|
||||
@Post('signin')
|
||||
async signIn(@Body() authData: signInMagicDto) {
|
||||
return this.authService.signIn(authData.email);
|
||||
const data = await this.authService.signIn(authData.email);
|
||||
if (E.isLeft(data)) throwHTTPErr(data.left);
|
||||
return data.right;
|
||||
}
|
||||
|
||||
//TODO: set expiresOn to cookies
|
||||
@Post('verify')
|
||||
async verify(@Body() data: verifyMagicDto, @Res() res: Response) {
|
||||
const authTokens = await this.authService.verify(data);
|
||||
res.cookie('access_token', authTokens.access_token, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
sameSite: 'lax',
|
||||
});
|
||||
res.cookie('refresh_token', authTokens.refresh_token, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
sameSite: 'lax',
|
||||
});
|
||||
res.status(HttpStatus.OK).json({
|
||||
message: 'Successfully logged in',
|
||||
});
|
||||
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
||||
authCookieHandler(res, authTokens.right, false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user