2023-01-23 23:13:42 -05:00
|
|
|
import {
|
2023-04-25 22:19:23 -04:00
|
|
|
AuthDeviceResponseDto,
|
2023-12-09 23:34:12 -05:00
|
|
|
AuthDto,
|
2023-01-23 23:13:42 -05:00
|
|
|
AuthService,
|
|
|
|
|
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,
|
2023-11-14 04:10:35 +01:00
|
|
|
mapUser,
|
2023-01-23 23:13:42 -05:00
|
|
|
} from '@app/domain';
|
2023-08-01 11:49:50 -04:00
|
|
|
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Post, Req, Res } from '@nestjs/common';
|
2023-11-09 10:14:15 -05:00
|
|
|
import { ApiTags } from '@nestjs/swagger';
|
2023-01-23 23:13:42 -05:00
|
|
|
import { Request, Response } from 'express';
|
2023-12-09 23:34:12 -05:00
|
|
|
import { Auth, Authenticated, GetLoginDetails, PublicRoute } from '../app.guard';
|
2023-07-01 14:27:34 -04:00
|
|
|
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')
|
2023-11-09 10:14:15 -05:00
|
|
|
signUpAdmin(@Body() dto: SignUpDto): Promise<UserResponseDto> {
|
|
|
|
|
return this.service.adminSignUp(dto);
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-25 22:19:23 -04:00
|
|
|
@Get('devices')
|
2023-12-09 23:34:12 -05:00
|
|
|
getAuthDevices(@Auth() auth: AuthDto): Promise<AuthDeviceResponseDto[]> {
|
|
|
|
|
return this.service.getDevices(auth);
|
2023-04-25 22:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
2023-05-09 15:34:17 -04:00
|
|
|
@Delete('devices')
|
2023-08-01 11:49:50 -04:00
|
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
2023-12-09 23:34:12 -05:00
|
|
|
logoutAuthDevices(@Auth() auth: AuthDto): Promise<void> {
|
|
|
|
|
return this.service.logoutDevices(auth);
|
2023-05-09 15:34:17 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-25 22:19:23 -04:00
|
|
|
@Delete('devices/:id')
|
2023-08-01 11:49:50 -04:00
|
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
2023-12-09 23:34:12 -05:00
|
|
|
logoutAuthDevice(@Auth() auth: AuthDto, @Param() { id }: UUIDParamDto): Promise<void> {
|
|
|
|
|
return this.service.logoutDevice(auth, id);
|
2023-04-25 22:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-23 23:13:42 -05:00
|
|
|
@Post('validateToken')
|
2023-08-01 11:49:50 -04:00
|
|
|
@HttpCode(HttpStatus.OK)
|
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-08-01 11:49:50 -04:00
|
|
|
@HttpCode(HttpStatus.OK)
|
2023-12-09 23:34:12 -05:00
|
|
|
changePassword(@Auth() auth: AuthDto, @Body() dto: ChangePasswordDto): Promise<UserResponseDto> {
|
|
|
|
|
return this.service.changePassword(auth, dto).then(mapUser);
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('logout')
|
2023-08-01 11:49:50 -04:00
|
|
|
@HttpCode(HttpStatus.OK)
|
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-12-09 23:34:12 -05:00
|
|
|
@Auth() auth: AuthDto,
|
2023-02-05 23:31:16 -06:00
|
|
|
): Promise<LogoutResponseDto> {
|
2023-01-23 23:13:42 -05:00
|
|
|
res.clearCookie(IMMICH_ACCESS_COOKIE);
|
|
|
|
|
res.clearCookie(IMMICH_AUTH_TYPE_COOKIE);
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
return this.service.logout(auth, (req.cookies || {})[IMMICH_AUTH_TYPE_COOKIE]);
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
|
|
|
|
}
|