2025-05-09 16:00:58 -05:00
|
|
|
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Post, Put, Req, Res } from '@nestjs/common';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
|
|
|
import { Request, Response } from 'express';
|
2023-01-23 23:13:42 -05:00
|
|
|
import {
|
2023-12-09 23:34:12 -05:00
|
|
|
AuthDto,
|
2025-05-09 16:00:58 -05:00
|
|
|
AuthStatusResponseDto,
|
2023-01-23 23:13:42 -05:00
|
|
|
ChangePasswordDto,
|
|
|
|
|
LoginCredentialDto,
|
|
|
|
|
LoginResponseDto,
|
|
|
|
|
LogoutResponseDto,
|
2025-05-09 16:00:58 -05:00
|
|
|
PinCodeChangeDto,
|
|
|
|
|
PinCodeSetupDto,
|
2023-01-23 23:13:42 -05:00
|
|
|
SignUpDto,
|
|
|
|
|
ValidateAccessTokenResponseDto,
|
2024-03-20 23:53:07 +01:00
|
|
|
} from 'src/dtos/auth.dto';
|
2024-05-26 18:15:52 -04:00
|
|
|
import { UserAdminResponseDto } from 'src/dtos/user.dto';
|
2024-10-17 13:17:32 -04:00
|
|
|
import { AuthType, ImmichCookie } from 'src/enum';
|
2024-05-09 13:58:44 -04:00
|
|
|
import { Auth, Authenticated, GetLoginDetails } from 'src/middleware/auth.guard';
|
2024-03-21 00:07:30 +01:00
|
|
|
import { AuthService, LoginDetails } from 'src/services/auth.service';
|
2024-04-19 11:19:23 -04:00
|
|
|
import { respondWithCookie, respondWithoutCookie } from 'src/utils/response';
|
2023-01-23 23:13:42 -05:00
|
|
|
|
|
|
|
|
@ApiTags('Authentication')
|
|
|
|
|
@Controller('auth')
|
|
|
|
|
export class AuthController {
|
2023-06-16 15:36:07 -04:00
|
|
|
constructor(private service: AuthService) {}
|
2023-01-23 23:13:42 -05:00
|
|
|
|
|
|
|
|
@Post('login')
|
|
|
|
|
async login(
|
|
|
|
|
@Res({ passthrough: true }) res: Response,
|
2025-05-05 18:57:32 -04:00
|
|
|
@Body() loginCredential: LoginCredentialDto,
|
2023-04-25 22:19:23 -04:00
|
|
|
@GetLoginDetails() loginDetails: LoginDetails,
|
2023-01-23 23:13:42 -05:00
|
|
|
): Promise<LoginResponseDto> {
|
2024-04-19 11:19:23 -04:00
|
|
|
const body = await this.service.login(loginCredential, loginDetails);
|
|
|
|
|
return respondWithCookie(res, body, {
|
|
|
|
|
isSecure: loginDetails.isSecure,
|
|
|
|
|
values: [
|
|
|
|
|
{ key: ImmichCookie.ACCESS_TOKEN, value: body.accessToken },
|
|
|
|
|
{ key: ImmichCookie.AUTH_TYPE, value: AuthType.PASSWORD },
|
|
|
|
|
{ key: ImmichCookie.IS_AUTHENTICATED, value: 'true' },
|
|
|
|
|
],
|
|
|
|
|
});
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('admin-sign-up')
|
2024-05-26 18:15:52 -04:00
|
|
|
signUpAdmin(@Body() dto: SignUpDto): Promise<UserAdminResponseDto> {
|
2023-11-09 10:14:15 -05:00
|
|
|
return this.service.adminSignUp(dto);
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('validateToken')
|
2023-08-01 11:49:50 -04:00
|
|
|
@HttpCode(HttpStatus.OK)
|
2024-05-09 13:58:44 -04:00
|
|
|
@Authenticated()
|
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)
|
2024-05-09 13:58:44 -04:00
|
|
|
@Authenticated()
|
2024-05-26 18:15:52 -04:00
|
|
|
changePassword(@Auth() auth: AuthDto, @Body() dto: ChangePasswordDto): Promise<UserAdminResponseDto> {
|
|
|
|
|
return this.service.changePassword(auth, dto);
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('logout')
|
2023-08-01 11:49:50 -04:00
|
|
|
@HttpCode(HttpStatus.OK)
|
2024-05-09 13:58:44 -04:00
|
|
|
@Authenticated()
|
2024-04-19 11:19:23 -04:00
|
|
|
async logout(
|
2024-02-02 04:18:00 +01:00
|
|
|
@Req() request: Request,
|
2023-02-05 23:31:16 -06:00
|
|
|
@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> {
|
2024-04-19 11:19:23 -04:00
|
|
|
const authType = (request.cookies || {})[ImmichCookie.AUTH_TYPE];
|
2023-01-23 23:13:42 -05:00
|
|
|
|
2024-04-19 11:19:23 -04:00
|
|
|
const body = await this.service.logout(auth, authType);
|
|
|
|
|
return respondWithoutCookie(res, body, [
|
|
|
|
|
ImmichCookie.ACCESS_TOKEN,
|
|
|
|
|
ImmichCookie.AUTH_TYPE,
|
|
|
|
|
ImmichCookie.IS_AUTHENTICATED,
|
|
|
|
|
]);
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|
2025-05-09 16:00:58 -05:00
|
|
|
|
|
|
|
|
@Get('status')
|
|
|
|
|
@Authenticated()
|
|
|
|
|
getAuthStatus(@Auth() auth: AuthDto): Promise<AuthStatusResponseDto> {
|
|
|
|
|
return this.service.getAuthStatus(auth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('pin-code')
|
|
|
|
|
@Authenticated()
|
|
|
|
|
setupPinCode(@Auth() auth: AuthDto, @Body() dto: PinCodeSetupDto): Promise<void> {
|
|
|
|
|
return this.service.setupPinCode(auth, dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Put('pin-code')
|
|
|
|
|
@Authenticated()
|
|
|
|
|
async changePinCode(@Auth() auth: AuthDto, @Body() dto: PinCodeChangeDto): Promise<void> {
|
|
|
|
|
return this.service.changePinCode(auth, dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete('pin-code')
|
|
|
|
|
@Authenticated()
|
|
|
|
|
async resetPinCode(@Auth() auth: AuthDto, @Body() dto: PinCodeChangeDto): Promise<void> {
|
|
|
|
|
return this.service.resetPinCode(auth, dto);
|
|
|
|
|
}
|
2025-05-15 09:35:21 -06:00
|
|
|
|
|
|
|
|
@Post('pin-code/verify')
|
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
|
|
|
@Authenticated()
|
|
|
|
|
async verifyPinCode(@Auth() auth: AuthDto, @Body() dto: PinCodeSetupDto): Promise<void> {
|
|
|
|
|
return this.service.verifyPinCode(auth, dto);
|
|
|
|
|
}
|
2023-01-23 23:13:42 -05:00
|
|
|
}
|