mirror of
https://github.com/aleksilassila/reiverr.git
synced 2026-04-25 10:15:11 +02:00
Initial Nest.js backend
This commit is contained in:
18
backend/src/auth/auth.controller.spec.ts
Normal file
18
backend/src/auth/auth.controller.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AuthController } from './auth.controller';
|
||||
|
||||
describe('AuthController', () => {
|
||||
let controller: AuthController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AuthController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<AuthController>(AuthController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
34
backend/src/auth/auth.controller.ts
Normal file
34
backend/src/auth/auth.controller.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Post,
|
||||
UseGuards,
|
||||
Request,
|
||||
} from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { AuthGuard } from './auth.guard';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(private authService: AuthService) {}
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post()
|
||||
async signIn(@Body() signInDto: { name: string; password: string }) {
|
||||
const { token } = await this.authService.signIn(
|
||||
signInDto.name,
|
||||
signInDto.password,
|
||||
);
|
||||
return {
|
||||
accessToken: token,
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Get('profile')
|
||||
getProfile(@Request() req) {
|
||||
return req.user;
|
||||
}
|
||||
}
|
||||
7
backend/src/auth/auth.guard.spec.ts
Normal file
7
backend/src/auth/auth.guard.spec.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { AuthGuard } from './auth.guard';
|
||||
|
||||
describe('AuthGuard', () => {
|
||||
it('should be defined', () => {
|
||||
expect(new AuthGuard()).toBeDefined();
|
||||
});
|
||||
});
|
||||
39
backend/src/auth/auth.guard.ts
Normal file
39
backend/src/auth/auth.guard.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { JWT_SECRET } from '../consts';
|
||||
import { AuthUser } from './auth.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(private jwtService: JwtService) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const token = this.extractTokenFromHeader(request);
|
||||
if (!token) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
try {
|
||||
const payload = await this.jwtService.verifyAsync(token, {
|
||||
secret: JWT_SECRET,
|
||||
});
|
||||
// 💡 We're assigning the payload to the request object here
|
||||
// so that we can access it in our route handlers
|
||||
request['user'] = payload as AuthUser;
|
||||
} catch {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private extractTokenFromHeader(request: Request): string | undefined {
|
||||
const [type, token] =
|
||||
(request.headers as any).authorization?.split(' ') ?? [];
|
||||
return type === 'Bearer' ? token : undefined;
|
||||
}
|
||||
}
|
||||
20
backend/src/auth/auth.module.ts
Normal file
20
backend/src/auth/auth.module.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { UserModule } from '../user/user.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { JWT_SECRET } from 'src/consts';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
UserModule,
|
||||
JwtModule.register({
|
||||
global: true,
|
||||
secret: JWT_SECRET,
|
||||
signOptions: { expiresIn: '1d' },
|
||||
}),
|
||||
],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService],
|
||||
})
|
||||
export class AuthModule {}
|
||||
18
backend/src/auth/auth.service.spec.ts
Normal file
18
backend/src/auth/auth.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
describe('AuthService', () => {
|
||||
let service: AuthService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [AuthService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<AuthService>(AuthService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
45
backend/src/auth/auth.service.ts
Normal file
45
backend/src/auth/auth.service.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { UserService } from '../user/user.service';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
|
||||
interface AccessTokenPayload {
|
||||
sub: string;
|
||||
name: string;
|
||||
isAdmin: boolean;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private userService: UserService,
|
||||
private jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
async signIn(
|
||||
name: string,
|
||||
password: string,
|
||||
): Promise<{
|
||||
token: string;
|
||||
}> {
|
||||
const user = await this.userService.findOneByName(name);
|
||||
|
||||
if (!(user && user.password === password)) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
|
||||
const payload: AccessTokenPayload = {
|
||||
sub: user.id,
|
||||
name: user.name,
|
||||
isAdmin: user.isAdmin,
|
||||
};
|
||||
|
||||
return {
|
||||
token: await this.jwtService.signAsync(payload),
|
||||
};
|
||||
}
|
||||
}
|
||||
export type AuthUser = {
|
||||
id: string;
|
||||
name: string;
|
||||
isAdmin: boolean;
|
||||
};
|
||||
Reference in New Issue
Block a user