File tree Expand file tree Collapse file tree 2 files changed +11
-5
lines changed Expand file tree Collapse file tree 2 files changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -11,14 +11,17 @@ import { UserLoginResponseDto } from './dto/user-login-response.dto';
1111export class UsersController {
1212 constructor ( private readonly usersService : UsersService ) { }
1313
14- @Post ( )
15- @ApiOkResponse ( { type : UserDto } )
16- register ( @Body ( ) createUserDto : CreateUserDto ) : Promise < UserDto > {
14+ @Post ( 'register' )
15+ @ApiOkResponse ( { type : UserLoginResponseDto } )
16+ register (
17+ @Body ( ) createUserDto : CreateUserDto ,
18+ ) : Promise < UserLoginResponseDto > {
1719 return this . usersService . create ( createUserDto ) ;
1820 }
1921
2022 @Post ( 'login' )
2123 @HttpCode ( 200 )
24+ @ApiOkResponse ( { type : UserLoginResponseDto } )
2225 async login (
2326 @Body ( ) userLoginRequestDto : UserLoginRequestDto ,
2427 ) : Promise < UserLoginResponseDto > {
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ export class UsersService {
2828 } ) ;
2929 }
3030
31- async create ( createUserDto : CreateUserDto ) : Promise < UserDto > {
31+ async create ( createUserDto : CreateUserDto ) : Promise < UserLoginResponseDto > {
3232 try {
3333 const user = new User ( ) ;
3434 user . email = createUserDto . email . trim ( ) . toLowerCase ( ) ;
@@ -41,7 +41,10 @@ export class UsersService {
4141 user . password = await hash ( createUserDto . password , salt ) ;
4242
4343 const userData = await user . save ( ) ;
44- return new UserDto ( userData ) ;
44+
45+ // when registering then log user in automatically by returning a token
46+ const token = await this . authService . signToken ( userData ) ;
47+ return new UserLoginResponseDto ( userData , token ) ;
4548 } catch ( err ) {
4649 if ( err . original . constraint === 'user_email_key' ) {
4750 throw new HttpException (
You can’t perform that action at this time.
0 commit comments