2023-01-23 23:13:42 -05:00
|
|
|
import {
|
|
|
|
|
AdminSignupResponseDto,
|
2023-04-25 22:19:23 -04:00
|
|
|
AuthDeviceResponseDto,
|
2023-01-23 23:13:42 -05:00
|
|
|
AuthService,
|
|
|
|
|
AuthType,
|
|
|
|
|
AuthUserDto,
|
|
|
|
|
ChangePasswordDto,
|
|
|
|
|
IMMICH_ACCESS_COOKIE,
|
|
|
|
|
IMMICH_AUTH_TYPE_COOKIE,
|
|
|
|
|
LoginCredentialDto,
|
2023-04-25 22:19:23 -04:00
|
|
|
LoginDetails,
|
2023-01-23 23:13:42 -05:00
|
|
|
LoginResponseDto,
|
|
|
|
|
LogoutResponseDto,
|
|
|
|
|
SignUpDto,
|
|
|
|
|
UserResponseDto,
|
|
|
|
|
ValidateAccessTokenResponseDto,
|
|
|
|
|
} from '@app/domain';
|
2023-04-25 22:19:23 -04:00
|
|
|
import { Body, Controller, Delete, Get, Param, Post, Req, Res } from '@nestjs/common';
|
2023-02-24 17:01:10 +01:00
|
|
|
import { ApiBadRequestResponse, ApiTags } from '@nestjs/swagger';
|
2023-01-23 23:13:42 -05:00
|
|
|
import { Request, Response } from 'express';
|
2023-07-01 14:27:34 -04:00
|
|
|
import { Authenticated, AuthUser, GetLoginDetails, PublicRoute } from '../app.guard';
|
|
|
|
|
import { UseValidation } from '../app.utils';
|
2023-04-25 22:19:23 -04:00
|
|
|
import { UUIDParamDto } from './dto/uuid-param.dto';
|
2023-01-23 23:13:42 -05:00
|
|
|
|
|
|
|
|
@ApiTags('Authentication')
|
|
|
|
|
@Controller('auth')
|
2023-05-28 12:30:01 -04:00
|
|
|
@Authenticated()
|
2023-04-03 06:24:18 +02:00
|
|
|
@UseValidation()
|
2023-01-23 23:13:42 -05:00
|
|
|
export class AuthController {
|
2023-06-16 15:36:07 -04:00
|
|
|
constructor(private service: AuthService) {}
|
2023-01-23 23:13:42 -05:00
|
|
|
|
2023-05-28 12:30:01 -04:00
|
|
|
@PublicRoute()
|
2023-01-23 23:13:42 -05:00
|
|
|
@Post('login')
|
|
|
|
|
async login(
|
2023-03-31 17:14:01 +02:00
|
|
|
@Body() loginCredential: LoginCredentialDto,
|
2023-01-23 23:13:42 -05:00
|
|
|
@Res({ passthrough: true }) res: Response,
|
2023-04-25 22:19:23 -04:00
|
|
|
@GetLoginDetails() loginDetails: LoginDetails,
|
2023-01-23 23:13:42 -05:00
|
|
|
): Promise<LoginResponseDto> {
|
2023-04-25 22:19:23 -04:00
|
|
|
const { response, cookie } = await this.service.login(loginCredential, loginDetails);
|
2023-03-24 00:53:56 -04:00
|
|
|
res.header('Set-Cookie', cookie);
|
2023-01-23 23:13:42 -05:00
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 12:30:01 -04:00
|
|
|
@PublicRoute()
|
2023-01-23 23:13:42 -05:00
|
|
|
@Post('admin-sign-up')
|
|
|
|
|
@ApiBadRequestResponse({ description: 'The server already has an admin' })
|
2023-03-31 17:14:01 +02:00
|
|
|
adminSignUp(@Body() signUpCredential: SignUpDto): Promise<AdminSignupResponseDto> {
|
2023-03-24 00:53:56 -04:00
|
|
|
return this.service.adminSignUp(signUpCredential);
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-25 22:19:23 -04:00
|
|
|
@Get('devices')
|
2023-06-16 15:39:53 -04:00
|
|
|
getAuthDevices(@AuthUser() authUser: AuthUserDto): Promise<AuthDeviceResponseDto[]> {
|
2023-04-25 22:19:23 -04:00
|
|
|
return this.service.getDevices(authUser);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 15:34:17 -04:00
|
|
|
@Delete('devices')
|
2023-06-16 15:39:53 -04:00
|
|
|
logoutAuthDevices(@AuthUser() authUser: AuthUserDto): Promise<void> {
|
2023-05-09 15:34:17 -04:00
|
|
|
return this.service.logoutDevices(authUser);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 22:19:23 -04:00
|
|
|
@Delete('devices/:id')
|
2023-06-16 15:39:53 -04:00
|
|
|
logoutAuthDevice(@AuthUser() authUser: AuthUserDto, @Param() { id }: UUIDParamDto): Promise<void> {
|
2023-04-25 22:19:23 -04:00
|
|
|
return this.service.logoutDevice(authUser, id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 23:13:42 -05:00
|
|
|
@Post('validateToken')
|
2023-03-24 00:53:56 -04:00
|
|
|
validateAccessToken(): ValidateAccessTokenResponseDto {
|
2023-01-23 23:13:42 -05:00
|
|
|
return { authStatus: true };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('change-password')
|
2023-06-16 15:39:53 -04:00
|
|
|
changePassword(@AuthUser() authUser: AuthUserDto, @Body() dto: ChangePasswordDto): Promise<UserResponseDto> {
|
2023-03-24 00:53:56 -04:00
|
|
|
return this.service.changePassword(authUser, dto);
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('logout')
|
2023-03-24 00:53:56 -04:00
|
|
|
logout(
|
2023-02-05 23:31:16 -06:00
|
|
|
@Req() req: Request,
|
|
|
|
|
@Res({ passthrough: true }) res: Response,
|
2023-06-16 15:39:53 -04:00
|
|
|
@AuthUser() authUser: AuthUserDto,
|
2023-02-05 23:31:16 -06:00
|
|
|
): Promise<LogoutResponseDto> {
|
2023-01-23 23:13:42 -05:00
|
|
|
const authType: AuthType = req.cookies[IMMICH_AUTH_TYPE_COOKIE];
|
|
|
|
|
|
|
|
|
|
res.clearCookie(IMMICH_ACCESS_COOKIE);
|
|
|
|
|
res.clearCookie(IMMICH_AUTH_TYPE_COOKIE);
|
|
|
|
|
|
2023-03-24 00:53:56 -04:00
|
|
|
return this.service.logout(authUser, authType);
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
|
|
|
|
}
|