-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.controller.ts
110 lines (98 loc) · 3.04 KB
/
auth.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import {
Body,
Controller,
Get,
Param,
Post,
Req,
Session,
UseGuards,
Request as NestRequest,
} from '@nestjs/common';
import { Request as ExpressRequest } from 'express';
import { CreateUserDto } from '../users/dto/create-user.dto';
import { AuthService } from './auth.service';
import { AuthGuard } from '@nestjs/passport';
import { JwtAuthGuard } from '../guard/jwt-auth.guard';
import { Public } from "../config/public.decorator";
@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}
// #1
@Get('/chats')
async getChat(@Session() session: any) {
return session.chat;
}
@Get('/chats/:chat')
async setChat(@Param('chat') chat: string, @Session() session: any) {
session.chat = chat;
}
@Get()
findAll(@Req() request: ExpressRequest) {
console.log('1: ');
console.log(request.cookies);
console.log('2: ');
console.log(request.cookies['cookieKey']);
console.log('3: ');
console.log(request.signedCookies);
}
@Post('/v1/signup')
async signup(@Body() body: CreateUserDto, @Session() session: any) {
// # Simple test
// this.userService.create(body.email, body.password);
// # Test with auth
// return this.authService.sugnup(body.email, body.password);
// # Test using cookies
const user = await this.authService.signup(body.email, body.password);
session.userId = user.id;
return user;
}
@Post('/v1/signin')
async signin(@Body() body: CreateUserDto, @Session() session: any) {
// # Test with auth
// return this.authService.signin(body.email, body.password);
// # Test using cookies
const user = await this.authService.signin(body.email, body.password);
session.userId = user.id;
return user;
}
@Post('/v2/signup')
async signupV2(@Body() body: CreateUserDto, @Session() session: any) {
const user = await this.authService.signupV2(body.email, body.password);
session.userId = user.id;
return user;
}
@Post('/v2/signin')
async signinV2(@Body() body: CreateUserDto, @Session() session: any) {
const user = await this.authService.signinV2(body.email, body.password);
session.userId = user.id;
return user;
}
@Post('/v3/signup')
async signupV3(@Body() body: CreateUserDto, @Session() session: any) {
const user = await this.authService.signupV3(body.email, body.password);
session.userId = user.id;
return user;
}
@Post('/v3/signin')
async signinV3(@Body() body: CreateUserDto, @Session() session: any) {
const user = await this.authService.signinV3(body.email, body.password);
session.userId = user.id;
return user;
}
/**
* Ref: https://docs.nestjs.com/security/authentication#login-route
*/
@UseGuards(AuthGuard('local')) //provisioned when we extended the passport-local strategy
@Post('/login')
async login(@NestRequest() req) {
// return req.user;
return this.authService.login(req.user);
}
@Public()
@UseGuards(JwtAuthGuard)
@Get('/profile')
async getProfile(@NestRequest() req) {
return req.user;
}
}