|
1 | | -import { Controller, Get, Post, Res } from '@nestjs/common' |
| 1 | +import { Controller, Get, HttpStatus, Post, Res, UseGuards } from '@nestjs/common' |
2 | 2 | import { AuthService } from './auth.service' |
3 | 3 | import { AbstractController } from '~/_common/abstracts/abstract.controller' |
4 | 4 | import { Response } from 'express' |
5 | 5 | import { ApiTags } from '@nestjs/swagger' |
| 6 | +import { Public } from '~/_common/decorators/public.decorator' |
| 7 | +import { ModuleRef } from '@nestjs/core' |
| 8 | +import { AuthGuard } from '@nestjs/passport' |
| 9 | +import { IdentityType } from '~/_common/types/identity.type' |
| 10 | +import { ReqIdentity } from '~/_common/decorators/params/req-identity.decorator' |
6 | 11 |
|
| 12 | +@Public() |
7 | 13 | @ApiTags('core') |
8 | 14 | @Controller('auth') |
9 | 15 | export class AuthController extends AbstractController { |
10 | | - constructor(private readonly service: AuthService) { |
| 16 | + constructor( |
| 17 | + protected moduleRef: ModuleRef, |
| 18 | + private readonly service: AuthService, |
| 19 | + ) { |
11 | 20 | super() |
12 | 21 | } |
13 | 22 |
|
14 | | - @Post('login') |
15 | | - public async login(@Res() res: Response): Promise<Response> { |
16 | | - console.log('login') |
17 | | - return res.json({ |
18 | | - token: '123', |
| 23 | + @Post('local') |
| 24 | + @UseGuards(AuthGuard('local')) |
| 25 | + public async authenticateWithLocal(@Res() res: Response, @ReqIdentity() identity: IdentityType): Promise<Response> { |
| 26 | + const tokens = await this.service.createToken(identity) |
| 27 | + return res.status(HttpStatus.CREATED).json({ |
| 28 | + ...tokens, |
| 29 | + token: '1234', |
| 30 | + identity, |
19 | 31 | user: { |
20 | | - id: "1", |
| 32 | + id: 1, |
21 | 33 | } |
22 | 34 | }) |
23 | 35 | } |
24 | 36 |
|
25 | 37 | @Get('session') |
26 | | - public async session(@Res() res: Response): Promise<Response> { |
27 | | - // console.log('session') |
28 | | - return res.json({ |
| 38 | + // @UseGuards(AuthGuard('jwt')) |
| 39 | + public async session(@Res() res: Response, @ReqIdentity() identity: IdentityType): Promise<Response> { |
| 40 | + // const tokens = await this.service.createToken(identity) |
| 41 | + return res.status(HttpStatus.OK).json({ |
| 42 | + // ...tokens, |
29 | 43 | token: '1234', |
| 44 | + identity, |
30 | 45 | user: { |
31 | | - id: "1", |
| 46 | + id: 1, |
32 | 47 | } |
33 | 48 | }) |
34 | 49 | } |
35 | 50 |
|
36 | | - @Get('credentials') |
37 | | - public async credentials(@Res() res: Response): Promise<Response> { |
38 | | - return res.json({ |
39 | | - token: '123', |
40 | | - user: { |
41 | | - id: "1", |
42 | | - } |
43 | | - }) |
| 51 | + @Post('logout') |
| 52 | + public async logout(@Res() res: Response): Promise<Response> { |
| 53 | + return res.status(HttpStatus.NO_CONTENT).send() |
44 | 54 | } |
45 | 55 | } |
0 commit comments