2023-01-10 11:30:39 +00:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
HttpException,
|
|
|
|
|
HttpStatus,
|
|
|
|
|
Post,
|
|
|
|
|
Res,
|
|
|
|
|
} from '@nestjs/common';
|
2023-01-09 13:32:14 +00:00
|
|
|
import { AuthService } from './auth.service';
|
|
|
|
|
import { signInMagicDto } from './dto/signin-magic.dto';
|
|
|
|
|
import { verifyMagicDto } from './dto/verify-magic.dto';
|
2023-01-10 10:36:42 +00:00
|
|
|
import { Response } from 'express';
|
2023-01-10 11:30:39 +00:00
|
|
|
import * as E from 'fp-ts/Either';
|
|
|
|
|
import { authCookieHandler, throwHTTPErr } from 'src/utils';
|
2023-01-09 13:32:14 +00:00
|
|
|
|
|
|
|
|
@Controller('auth')
|
|
|
|
|
export class AuthController {
|
|
|
|
|
constructor(private authService: AuthService) {}
|
|
|
|
|
|
|
|
|
|
@Post('signin')
|
|
|
|
|
async signIn(@Body() authData: signInMagicDto) {
|
2023-01-10 11:30:39 +00:00
|
|
|
const data = await this.authService.signIn(authData.email);
|
|
|
|
|
if (E.isLeft(data)) throwHTTPErr(data.left);
|
|
|
|
|
return data.right;
|
2023-01-09 13:32:14 +00:00
|
|
|
}
|
2023-01-10 10:36:42 +00:00
|
|
|
|
|
|
|
|
@Post('verify')
|
|
|
|
|
async verify(@Body() data: verifyMagicDto, @Res() res: Response) {
|
|
|
|
|
const authTokens = await this.authService.verify(data);
|
2023-01-10 11:30:39 +00:00
|
|
|
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
|
|
|
|
authCookieHandler(res, authTokens.right, false);
|
2023-01-10 10:36:42 +00:00
|
|
|
}
|
2023-01-09 13:32:14 +00:00
|
|
|
}
|