mirror of
https://github.com/aleksilassila/reiverr.git
synced 2026-04-25 10:15:11 +02:00
feat: Editing, creating and deleting server accounts, user.controller improvements
This commit is contained in:
@@ -7,7 +7,7 @@ import {
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { SignInDto, UserDto } from '../user/user.dto';
|
||||
import { SignInDto, UserDto } from '../users/user.dto';
|
||||
import { ApiOkResponse, ApiProperty } from '@nestjs/swagger';
|
||||
import { ApiException } from '@nanogiants/nestjs-swagger-api-exception-decorator';
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ import {
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { JWT_SECRET } from '../consts';
|
||||
import { AccessTokenPayload } from './auth.service';
|
||||
import { User } from '../user/user.entity';
|
||||
import { UserService } from '../user/user.service';
|
||||
import { User } from '../users/user.entity';
|
||||
import { UsersService } from '../users/users.service';
|
||||
|
||||
export const GetUser = createParamDecorator(
|
||||
(data: unknown, ctx: ExecutionContext): User => {
|
||||
@@ -28,7 +28,7 @@ function extractTokenFromHeader(request: Request): string | undefined {
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private jwtService: JwtService,
|
||||
private userService: UserService,
|
||||
private userService: UsersService,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
@@ -47,6 +47,10 @@ export class AuthGuard implements CanActivate {
|
||||
if (payload.sub) {
|
||||
request['user'] = await this.userService.findOne(payload.sub);
|
||||
}
|
||||
|
||||
if (!request['user']) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
} catch {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
@@ -58,7 +62,7 @@ export class AuthGuard implements CanActivate {
|
||||
export class OptionalAuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private jwtService: JwtService,
|
||||
private userService: UserService,
|
||||
private userService: UsersService,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { UserModule } from '../user/user.module';
|
||||
import { UsersModule } from '../users/users.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { JWT_SECRET } from '../consts';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
UserModule,
|
||||
UsersModule,
|
||||
JwtModule.register({
|
||||
global: true,
|
||||
secret: JWT_SECRET,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { UserService } from '../user/user.service';
|
||||
import { UsersService } from '../users/users.service';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { User } from '../user/user.entity';
|
||||
import { User } from '../users/user.entity';
|
||||
|
||||
export interface AccessTokenPayload {
|
||||
sub: string;
|
||||
@@ -10,7 +10,7 @@ export interface AccessTokenPayload {
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private userService: UserService,
|
||||
private userService: UsersService,
|
||||
private jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
@@ -23,7 +23,11 @@ export class AuthService {
|
||||
}> {
|
||||
let user = await this.userService.findOneByName(name);
|
||||
if (!user && (await this.userService.noPreviousAdmins()))
|
||||
user = await this.userService.create(name, password, true);
|
||||
user = await this.userService.create({
|
||||
name,
|
||||
password,
|
||||
isAdmin: true,
|
||||
});
|
||||
|
||||
if (!(user && user.password === password)) {
|
||||
throw new UnauthorizedException();
|
||||
|
||||
Reference in New Issue
Block a user