Initial Nest.js backend

This commit is contained in:
Aleksi Lassila
2024-03-26 00:44:28 +02:00
parent 652894fcc9
commit 8a947d5831
35 changed files with 11307 additions and 1 deletions

View 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();
});
});

View 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;
}
}

View File

@@ -0,0 +1,7 @@
import { AuthGuard } from './auth.guard';
describe('AuthGuard', () => {
it('should be defined', () => {
expect(new AuthGuard()).toBeDefined();
});
});

View 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;
}
}

View 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 {}

View 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();
});
});

View 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;
};