-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
user.controller.ts
60 lines (53 loc) · 1.35 KB
/
user.controller.ts
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
import {
Body,
Controller,
Get,
Post,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { AuthService } from './services/auth/auth.service';
import { LoginDto } from './dto/login.dto';
import { UserService } from './services/user/user.service';
import { JwtAuthGuard } from './guards/jwt-auth/jwt-auth.guard';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CacheInterceptor } from '@nestjs/cache-manager';
@ApiTags('user')
@Controller('user')
export class UserController {
constructor(
private readonly authService: AuthService,
private readonly userService: UserService,
) {}
@Post('register')
async register(@Body() user: CreateUserDto) {
const newUser = await this.authService.register(user);
return {
message: 'User created',
user: {
id: newUser.id,
token: newUser.token,
},
};
}
@Post('login')
async login(@Body() login: LoginDto) {
const token = await this.authService.login(login);
return {
message: 'Login successful',
token,
};
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@UseInterceptors(CacheInterceptor)
@Get()
async getUsers() {
const users = await this.userService.getAll();
return {
message: 'Users retrieved successfully',
users,
};
}
}