File size: 1,321 Bytes
abc493d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Body, Controller, Get, Patch, Post, Request, SetMetadata, UseGuards } from '@nestjs/common';
import { IS_PUBLIC_KEY, JwtAuthGuard } from './jwt-auth.guard';
import { AuthService } from './auth.service';

@Controller('auth')
export class AuthController {
  constructor(private auth: AuthService) {}

  @SetMetadata(IS_PUBLIC_KEY, true)
  @Post('register')
  register(@Body() body: { email: string; name: string; password: string }) {
    return this.auth.register(body.email, body.name, body.password);
  }

  @SetMetadata(IS_PUBLIC_KEY, true)
  @Post('login')
  login(@Body() body: { email: string; password: string }) {
    return this.auth.login(body.email, body.password);
  }

  @UseGuards(JwtAuthGuard)
  @Get('me')
  me(@Request() req: { user: { id: number } }) {
    return this.auth.me(req.user.id);
  }

  @UseGuards(JwtAuthGuard)
  @Patch('profile')
  updateProfile(
    @Request() req: { user: { id: number } },
    @Body() body: { name: string },
  ) {
    return this.auth.updateProfile(req.user.id, body.name);
  }

  @UseGuards(JwtAuthGuard)
  @Patch('password')
  changePassword(
    @Request() req: { user: { id: number } },
    @Body() body: { currentPassword: string; newPassword: string },
  ) {
    return this.auth.changePassword(req.user.id, body.currentPassword, body.newPassword);
  }
}