refactor: User and session management

This commit is contained in:
Aleksi Lassila
2024-06-12 18:32:39 +03:00
parent a73f9d6cca
commit 5c1a4d4206
28 changed files with 364 additions and 388 deletions

View File

@@ -7,13 +7,15 @@ import {
UnauthorizedException,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { SignInDto } from '../user/user.dto';
import { SignInDto, UserDto } from '../user/user.dto';
import { ApiOkResponse, ApiProperty } from '@nestjs/swagger';
import { ApiException } from '@nanogiants/nestjs-swagger-api-exception-decorator';
export class SignInResponse {
@ApiProperty()
accessToken: string;
@ApiProperty()
user: UserDto;
}
@Controller('auth')
@@ -24,12 +26,14 @@ export class AuthController {
@ApiOkResponse({ description: 'User found', type: SignInResponse })
@ApiException(() => UnauthorizedException)
async signIn(@Body() signInDto: SignInDto) {
const { token } = await this.authService.signIn(
const { token, user } = await this.authService.signIn(
signInDto.name,
signInDto.password,
);
return {
accessToken: token,
user: UserDto.fromEntity(user),
};
}
}

View File

@@ -19,6 +19,7 @@ export class AuthService {
password: string,
): Promise<{
token: string;
user: User;
}> {
let user = await this.userService.findOneByName(name);
if (!user && (await this.userService.noPreviousAdmins()))
@@ -34,6 +35,7 @@ export class AuthService {
return {
token: await this.jwtService.signAsync(payload),
user,
};
}
}