mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-16 08:15:50 +01:00
add authenticated decorator
This commit is contained in:
10
backend/src/decorators/authenticated.ts
Normal file
10
backend/src/decorators/authenticated.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { CanActivate, UseGuards } from '@nestjs/common';
|
||||||
|
import { AdminGuard } from '../managers/auth/guards/admin.guard';
|
||||||
|
import { JwtAuthGuard } from '../managers/auth/guards/jwt.guard';
|
||||||
|
|
||||||
|
export const Authenticated = (adminOnly: boolean = false) => {
|
||||||
|
const guards: (Function | CanActivate)[] = [JwtAuthGuard];
|
||||||
|
if (adminOnly) guards.push(AdminGuard);
|
||||||
|
|
||||||
|
return UseGuards(...guards);
|
||||||
|
};
|
||||||
@@ -1,5 +1,11 @@
|
|||||||
import {
|
import {
|
||||||
Body, Controller, Get, InternalServerErrorException, Post, Request, UseGuards
|
Body,
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
InternalServerErrorException,
|
||||||
|
Post,
|
||||||
|
Request,
|
||||||
|
UseGuards
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
AuthDeleteRequest,
|
AuthDeleteRequest,
|
||||||
@@ -8,9 +14,8 @@ import {
|
|||||||
AuthRegisterRequest
|
AuthRegisterRequest
|
||||||
} from 'picsur-shared/dist/dto/auth.dto';
|
} from 'picsur-shared/dist/dto/auth.dto';
|
||||||
import { HasFailed } from 'picsur-shared/dist/types';
|
import { HasFailed } from 'picsur-shared/dist/types';
|
||||||
|
import { Authenticated } from '../../../decorators/authenticated';
|
||||||
import { AuthManagerService } from '../../../managers/auth/auth.service';
|
import { AuthManagerService } from '../../../managers/auth/auth.service';
|
||||||
import { AdminGuard } from '../../../managers/auth/guards/admin.guard';
|
|
||||||
import { JwtAuthGuard } from '../../../managers/auth/guards/jwt.guard';
|
|
||||||
import { LocalAuthGuard } from '../../../managers/auth/guards/localauth.guard';
|
import { LocalAuthGuard } from '../../../managers/auth/guards/localauth.guard';
|
||||||
import AuthFasityRequest from '../../../models/dto/authrequest.dto';
|
import AuthFasityRequest from '../../../models/dto/authrequest.dto';
|
||||||
|
|
||||||
@@ -18,8 +23,8 @@ import AuthFasityRequest from '../../../models/dto/authrequest.dto';
|
|||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private authService: AuthManagerService) {}
|
constructor(private authService: AuthManagerService) {}
|
||||||
|
|
||||||
@UseGuards(LocalAuthGuard)
|
|
||||||
@Post('login')
|
@Post('login')
|
||||||
|
@UseGuards(LocalAuthGuard)
|
||||||
async login(@Request() req: AuthFasityRequest) {
|
async login(@Request() req: AuthFasityRequest) {
|
||||||
const response: AuthLoginResponse = {
|
const response: AuthLoginResponse = {
|
||||||
jwt_token: await this.authService.createToken(req.user),
|
jwt_token: await this.authService.createToken(req.user),
|
||||||
@@ -28,8 +33,8 @@ export class AuthController {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard, AdminGuard)
|
|
||||||
@Post('create')
|
@Post('create')
|
||||||
|
@Authenticated(true)
|
||||||
async register(
|
async register(
|
||||||
@Request() req: AuthFasityRequest,
|
@Request() req: AuthFasityRequest,
|
||||||
@Body() register: AuthRegisterRequest,
|
@Body() register: AuthRegisterRequest,
|
||||||
@@ -50,8 +55,8 @@ export class AuthController {
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard, AdminGuard)
|
|
||||||
@Post('delete')
|
@Post('delete')
|
||||||
|
@Authenticated(true)
|
||||||
async delete(
|
async delete(
|
||||||
@Request() req: AuthFasityRequest,
|
@Request() req: AuthFasityRequest,
|
||||||
@Body() deleteData: AuthDeleteRequest,
|
@Body() deleteData: AuthDeleteRequest,
|
||||||
@@ -65,8 +70,8 @@ export class AuthController {
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard, AdminGuard)
|
|
||||||
@Get('list')
|
@Get('list')
|
||||||
|
@Authenticated(true)
|
||||||
async listUsers(@Request() req: AuthFasityRequest) {
|
async listUsers(@Request() req: AuthFasityRequest) {
|
||||||
const users = this.authService.listUsers();
|
const users = this.authService.listUsers();
|
||||||
if (HasFailed(users)) {
|
if (HasFailed(users)) {
|
||||||
@@ -77,8 +82,8 @@ export class AuthController {
|
|||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
@Get('me')
|
@Get('me')
|
||||||
|
@Authenticated()
|
||||||
async me(@Request() req: AuthFasityRequest) {
|
async me(@Request() req: AuthFasityRequest) {
|
||||||
const meResponse: AuthMeResponse = new AuthMeResponse();
|
const meResponse: AuthMeResponse = new AuthMeResponse();
|
||||||
meResponse.user = req.user;
|
meResponse.user = req.user;
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ import {
|
|||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Get,
|
Get,
|
||||||
InternalServerErrorException, Param,
|
InternalServerErrorException,
|
||||||
Post,
|
Param,
|
||||||
UseGuards
|
Post
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
SysPreferences,
|
SysPreferences,
|
||||||
@@ -12,11 +12,10 @@ import {
|
|||||||
} from 'picsur-shared/dist/dto/syspreferences.dto';
|
} from 'picsur-shared/dist/dto/syspreferences.dto';
|
||||||
import { HasFailed } from 'picsur-shared/dist/types';
|
import { HasFailed } from 'picsur-shared/dist/types';
|
||||||
import { SysPreferenceService } from '../../../collections/syspreferencesdb/syspreferencedb.service';
|
import { SysPreferenceService } from '../../../collections/syspreferencesdb/syspreferencedb.service';
|
||||||
import { AdminGuard } from '../../../managers/auth/guards/admin.guard';
|
import { Authenticated } from '../../../decorators/authenticated';
|
||||||
import { JwtAuthGuard } from '../../../managers/auth/guards/jwt.guard';
|
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard, AdminGuard)
|
|
||||||
@Controller('api/pref')
|
@Controller('api/pref')
|
||||||
|
@Authenticated(true)
|
||||||
export class PrefController {
|
export class PrefController {
|
||||||
constructor(private prefService: SysPreferenceService) {}
|
constructor(private prefService: SysPreferenceService) {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user