| 
									
										
										
										
											2023-02-16 15:43:38 +08:00
										 |  |  | import { Body, Controller, Get, Post } from '@nestjs/common' | 
					
						
							| 
									
										
										
										
											2023-02-16 01:18:55 +08:00
										 |  |  | import { AuthService } from './auth.service' | 
					
						
							|  |  |  | import { CreateUserDto } from 'src/users/dto/create-user.dto' | 
					
						
							| 
									
										
										
										
											2023-02-16 17:10:03 +08:00
										 |  |  | import { ApiTags } from '@nestjs/swagger' | 
					
						
							| 
									
										
										
										
											2023-02-16 01:18:55 +08:00
										 |  |  | import { LoginInputDto } from './dto/login-input.dto' | 
					
						
							| 
									
										
										
										
											2023-02-17 11:49:16 +08:00
										 |  |  | import { Token } from 'src/common/decorators/token.decorator' | 
					
						
							| 
									
										
										
										
											2023-02-16 17:25:13 +08:00
										 |  |  | import { NeedAuth } from 'src/common/decorators/need-auth.decorator' | 
					
						
							| 
									
										
										
										
											2023-02-17 11:49:16 +08:00
										 |  |  | import { UsersService } from 'src/users/users.service' | 
					
						
							| 
									
										
										
										
											2023-02-16 01:18:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | @ApiTags('auth') | 
					
						
							|  |  |  | @Controller() | 
					
						
							|  |  |  | export class AuthController { | 
					
						
							| 
									
										
										
										
											2023-02-17 11:49:16 +08:00
										 |  |  |   constructor( | 
					
						
							|  |  |  |     private readonly authService: AuthService, | 
					
						
							|  |  |  |     private readonly userService: UsersService, | 
					
						
							|  |  |  |   ) {} | 
					
						
							| 
									
										
										
										
											2023-02-16 01:18:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-16 10:40:03 +08:00
										 |  |  |   @Post('api/register') | 
					
						
							| 
									
										
										
										
											2023-02-16 01:18:55 +08:00
										 |  |  |   async register(@Body() userData: CreateUserDto) { | 
					
						
							| 
									
										
										
										
											2023-02-16 10:40:03 +08:00
										 |  |  |     return this.authService.register(userData) | 
					
						
							| 
									
										
										
										
											2023-02-16 01:18:55 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @Post('api/login') | 
					
						
							|  |  |  |   async login(@Body() user: LoginInputDto) { | 
					
						
							|  |  |  |     return this.authService.login(user.email, user.password) | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-02-16 12:09:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-16 17:10:03 +08:00
										 |  |  |   @NeedAuth() | 
					
						
							| 
									
										
										
										
											2023-02-16 12:09:48 +08:00
										 |  |  |   @Get('api/profile') | 
					
						
							| 
									
										
										
										
											2023-02-17 11:49:16 +08:00
										 |  |  |   async getUserInfo(@Token('userId') userId: string) { | 
					
						
							|  |  |  |     const user = await this.userService.findUser({ id: userId }) | 
					
						
							| 
									
										
										
										
											2023-02-16 15:06:51 +08:00
										 |  |  |     return user | 
					
						
							| 
									
										
										
										
											2023-02-16 12:09:48 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-02-16 01:18:55 +08:00
										 |  |  | } |