mirror of
https://github.com/immich-app/immich
synced 2026-08-29 13:15:45 +00:00
feat: jwt auth for maintenance
This commit is contained in:
parent
5f4a72735c
commit
014669f4a4
9 changed files with 482 additions and 48 deletions
|
|
@ -1,21 +1,42 @@
|
|||
import { BadRequestException, Controller, Post, Res } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { Response } from 'express';
|
||||
import { MaintenanceModeResponseDto } from 'src/dtos/maintenance.dto';
|
||||
import { AuthDto } from 'src/dtos/auth.dto';
|
||||
import { MaintenanceAuthDto, MaintenanceModeResponseDto } from 'src/dtos/maintenance.dto';
|
||||
import { ImmichCookie, Permission } from 'src/enum';
|
||||
import { Authenticated } from 'src/middleware/auth.guard';
|
||||
import { Auth, Authenticated } from 'src/middleware/auth.guard';
|
||||
import { MaintenanceService } from 'src/services/maintenance.service';
|
||||
|
||||
@ApiTags('Maintenance (admin)')
|
||||
@Controller('admin/maintenance')
|
||||
export class MaintenanceController {
|
||||
constructor(private service: MaintenanceService) {}
|
||||
constructor(
|
||||
private service: MaintenanceService,
|
||||
private jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
@Post('start')
|
||||
@Authenticated({ permission: Permission.Maintenance, admin: true })
|
||||
async startMaintenance(@Res({ passthrough: true }) response: Response): Promise<MaintenanceModeResponseDto> {
|
||||
await this.service.startMaintenance();
|
||||
response.cookie(ImmichCookie.MaintenanceToken, 'my-token');
|
||||
async startMaintenance(
|
||||
@Auth() auth: AuthDto,
|
||||
@Res({ passthrough: true }) response: Response,
|
||||
): Promise<MaintenanceModeResponseDto> {
|
||||
const { token } = await this.service.startMaintenance();
|
||||
|
||||
const jwt = await this.jwtService.signAsync(
|
||||
{
|
||||
data: {
|
||||
username: auth.user.name,
|
||||
} as MaintenanceAuthDto,
|
||||
},
|
||||
{
|
||||
secret: token,
|
||||
},
|
||||
);
|
||||
|
||||
response.cookie(ImmichCookie.MaintenanceToken, jwt);
|
||||
|
||||
return { isMaintenanceMode: true };
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue