-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.controller.ts
More file actions
53 lines (49 loc) · 1.51 KB
/
auth.controller.ts
File metadata and controls
53 lines (49 loc) · 1.51 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
import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiConflictResponse,
ApiCreatedResponse,
ApiInternalServerErrorResponse,
ApiOkResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { AuthService } from './auth.service.js';
import { LoginDto } from './dto/login.dto.js';
import { RegisterDto } from './dto/register.dto.js';
import { UserTokenDto } from './entities/user-token.entity.js';
@Controller('auth')
@ApiTags('Authentication')
@ApiInternalServerErrorResponse({
description: 'This service is temporary unavailable.',
})
@ApiBadRequestResponse({
description: 'The body of the request is invalid.',
})
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('register')
@HttpCode(HttpStatus.CREATED)
@ApiCreatedResponse({
type: UserTokenDto,
description: 'The Bearer token (JWT) for the new user.',
})
@ApiConflictResponse({
description: 'A user is already registered.',
})
async register(@Body() body: RegisterDto): Promise<UserTokenDto> {
return await this.authService.register(body);
}
@Post('login')
@HttpCode(HttpStatus.OK)
@ApiOkResponse({
type: UserTokenDto,
description: 'The user token used to communicate with the API.',
})
@ApiUnauthorizedResponse({
description: 'Either the email or the password is invalid.',
})
async login(@Body() body: LoginDto): Promise<UserTokenDto> {
return await this.authService.login(body);
}
}