chore(server): auth request type (#2502)
This commit is contained in:
parent
02b8b2c125
commit
3d426b55d3
7 changed files with 19 additions and 25 deletions
|
@ -1,5 +1,6 @@
|
|||
import { Request } from 'express';
|
||||
import * as fs from 'fs';
|
||||
import { AuthRequest } from '../decorators/auth-user.decorator';
|
||||
import { multerUtils } from './asset-upload.config';
|
||||
|
||||
const { fileFilter, destination, filename } = multerUtils;
|
||||
|
@ -14,7 +15,7 @@ const mock = {
|
|||
deviceId: 'test-device',
|
||||
fileExtension: '.jpg',
|
||||
},
|
||||
} as Request,
|
||||
} as AuthRequest,
|
||||
file: { originalname: 'test.jpg' } as Express.Multer.File,
|
||||
};
|
||||
|
||||
|
|
|
@ -2,12 +2,11 @@ import { StorageCore, StorageFolder } from '@app/domain/storage';
|
|||
import { BadRequestException, Logger, UnauthorizedException } from '@nestjs/common';
|
||||
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import { Request } from 'express';
|
||||
import { existsSync, mkdirSync } from 'fs';
|
||||
import { diskStorage, StorageEngine } from 'multer';
|
||||
import { extname } from 'path';
|
||||
import sanitize from 'sanitize-filename';
|
||||
import { AuthUserDto } from '../decorators/auth-user.decorator';
|
||||
import { AuthRequest, AuthUserDto } from '../decorators/auth-user.decorator';
|
||||
import { patchFormData } from '../utils/path-form-data.util';
|
||||
|
||||
export interface ImmichFile extends Express.Multer.File {
|
||||
|
@ -50,7 +49,7 @@ export const multerUtils = { fileFilter, filename, destination };
|
|||
|
||||
const logger = new Logger('AssetUploadConfig');
|
||||
|
||||
function fileFilter(req: Request, file: any, cb: any) {
|
||||
function fileFilter(req: AuthRequest, file: any, cb: any) {
|
||||
if (!req.user || (req.user.isPublicUser && !req.user.isAllowUpload)) {
|
||||
return cb(new UnauthorizedException());
|
||||
}
|
||||
|
@ -66,7 +65,7 @@ function fileFilter(req: Request, file: any, cb: any) {
|
|||
}
|
||||
}
|
||||
|
||||
function destination(req: Request, file: Express.Multer.File, cb: any) {
|
||||
function destination(req: AuthRequest, file: Express.Multer.File, cb: any) {
|
||||
if (!req.user || (req.user.isPublicUser && !req.user.isAllowUpload)) {
|
||||
return cb(new UnauthorizedException());
|
||||
}
|
||||
|
@ -82,7 +81,7 @@ function destination(req: Request, file: Express.Multer.File, cb: any) {
|
|||
cb(null, uploadFolder);
|
||||
}
|
||||
|
||||
function filename(req: Request, file: Express.Multer.File, cb: any) {
|
||||
function filename(req: AuthRequest, file: Express.Multer.File, cb: any) {
|
||||
if (!req.user || (req.user.isPublicUser && !req.user.isAllowUpload)) {
|
||||
return cb(new UnauthorizedException());
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { Request } from 'express';
|
||||
import * as fs from 'fs';
|
||||
import { AuthRequest } from '../decorators/auth-user.decorator';
|
||||
import { multerUtils } from './profile-image-upload.config';
|
||||
|
||||
const { fileFilter, destination, filename } = multerUtils;
|
||||
|
@ -10,7 +11,7 @@ const mock = {
|
|||
user: {
|
||||
id: 'test-user',
|
||||
},
|
||||
} as Request,
|
||||
} as AuthRequest,
|
||||
file: { originalname: 'test.jpg' } as Express.Multer.File,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
import { StorageCore, StorageFolder } from '@app/domain/storage';
|
||||
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
|
||||
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
|
||||
import { Request } from 'express';
|
||||
import { existsSync, mkdirSync } from 'fs';
|
||||
import { diskStorage } from 'multer';
|
||||
import { extname } from 'path';
|
||||
import sanitize from 'sanitize-filename';
|
||||
import { AuthUserDto } from '../decorators/auth-user.decorator';
|
||||
import { AuthRequest, AuthUserDto } from '../decorators/auth-user.decorator';
|
||||
import { patchFormData } from '../utils/path-form-data.util';
|
||||
|
||||
export const profileImageUploadOption: MulterOptions = {
|
||||
|
@ -21,7 +20,7 @@ export const multerUtils = { fileFilter, filename, destination };
|
|||
|
||||
const storageCore = new StorageCore();
|
||||
|
||||
function fileFilter(req: Request, file: any, cb: any) {
|
||||
function fileFilter(req: AuthRequest, file: any, cb: any) {
|
||||
if (!req.user) {
|
||||
return cb(new UnauthorizedException());
|
||||
}
|
||||
|
@ -33,7 +32,7 @@ function fileFilter(req: Request, file: any, cb: any) {
|
|||
}
|
||||
}
|
||||
|
||||
function destination(req: Request, file: Express.Multer.File, cb: any) {
|
||||
function destination(req: AuthRequest, file: Express.Multer.File, cb: any) {
|
||||
if (!req.user) {
|
||||
return cb(new UnauthorizedException());
|
||||
}
|
||||
|
@ -48,7 +47,7 @@ function destination(req: Request, file: Express.Multer.File, cb: any) {
|
|||
cb(null, profileImageLocation);
|
||||
}
|
||||
|
||||
function filename(req: Request, file: Express.Multer.File, cb: any) {
|
||||
function filename(req: AuthRequest, file: Express.Multer.File, cb: any) {
|
||||
if (!req.user) {
|
||||
return cb(new UnauthorizedException());
|
||||
}
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
export { AuthUserDto } from '@app/domain';
|
||||
import { AuthUserDto, LoginDetails } from '@app/domain';
|
||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
||||
import { Request } from 'express';
|
||||
import { UAParser } from 'ua-parser-js';
|
||||
|
||||
export interface AuthRequest extends Request {
|
||||
user?: AuthUserDto;
|
||||
}
|
||||
|
||||
export const GetAuthUser = createParamDecorator((data, ctx: ExecutionContext): AuthUserDto => {
|
||||
return ctx.switchToHttp().getRequest<{ user: AuthUserDto }>().user;
|
||||
});
|
||||
|
|
11
server/apps/immich/src/global.d.ts
vendored
11
server/apps/immich/src/global.d.ts
vendored
|
@ -1,11 +0,0 @@
|
|||
import { AuthUserDto } from './decorators/auth-user.decorator';
|
||||
|
||||
declare global {
|
||||
namespace Express {
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
interface User extends AuthUserDto {}
|
||||
export interface Request {
|
||||
user: AuthUserDto;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import { AuthService } from '@app/domain';
|
||||
import { CanActivate, ExecutionContext, Injectable, Logger } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { Request } from 'express';
|
||||
import { AuthRequest } from '../decorators/auth-user.decorator';
|
||||
import { Metadata } from '../decorators/authenticated.decorator';
|
||||
|
||||
@Injectable()
|
||||
|
@ -21,7 +21,7 @@ export class AuthGuard implements CanActivate {
|
|||
return true;
|
||||
}
|
||||
|
||||
const req = context.switchToHttp().getRequest<Request>();
|
||||
const req = context.switchToHttp().getRequest<AuthRequest>();
|
||||
|
||||
const authDto = await this.authService.validate(req.headers, req.query as Record<string, string>);
|
||||
if (!authDto) {
|
||||
|
|
Loading…
Reference in a new issue