-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.controller.ts
More file actions
64 lines (60 loc) · 1.79 KB
/
Copy pathauth.controller.ts
File metadata and controls
64 lines (60 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {
Body,
Controller,
Get,
Post,
Request,
UseGuards,
Res,
} from '@nestjs/common';
import { AuthGuard } from './auth.guard';
import { AuthService } from './auth.service';
import { ApiBearerAuth, ApiCookieAuth, ApiTags } from '@nestjs/swagger';
import { SingInDto as LoginDto } from './dto/login.dto';
import { RegisterDto } from './dto/register.dto';
import { RegisterResponseDto } from './dto/register-response.dto';
import { Public } from './public.decorator';
import { UserPermsOutDto } from 'src/users/dto/user-perms-out.dto';
import { Response } from 'express';
import { AuthorizedRequest } from './entities/authorized-request.entity';
import { Throttle } from '@nestjs/throttler';
import { appConfig } from 'src/config';
@Throttle({ default: { limit: 2, ttl: 60000 } })
@ApiTags('auth')
@Controller('auth')
export class AuthController {
constructor(private service: AuthService) {}
@Public()
@Post('login')
async login(
@Body() loginDto: LoginDto,
@Res({ passthrough: true }) res: Response,
) {
const token = await this.service.login(loginDto.email, loginDto.password);
res.cookie('accessToken', token.access_token, {
sameSite: 'none',
domain: appConfig.frontendDomain,
});
return token;
}
@Public()
@Post('register')
async register(
@Body() registerDto: RegisterDto,
@Res({ passthrough: true }) res: Response,
): Promise<RegisterResponseDto> {
const token = await this.service.register(registerDto);
res.cookie('accessToken', token.access_token, {
sameSite: 'none',
domain: appConfig.frontendDomain,
});
return token;
}
@ApiCookieAuth()
@ApiBearerAuth()
@UseGuards(AuthGuard)
@Get('perms')
async getPerms(@Request() req: AuthorizedRequest): Promise<UserPermsOutDto> {
return req.perms;
}
}