feat: Backend typing and openapi schema & codegen

This commit is contained in:
Aleksi Lassila
2024-03-26 23:01:11 +02:00
parent b29907c0e2
commit 7318a0fa99
22 changed files with 376 additions and 179 deletions

View File

@@ -1,18 +0,0 @@
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

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

View File

@@ -1,16 +1,26 @@
import {
CanActivate,
createParamDecorator,
ExecutionContext,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { JWT_SECRET } from '../consts';
import { AuthUser } from './auth.service';
import { AccessTokenPayload } from './auth.service';
import { User } from '../user/user.entity';
import { UserService } from '../user/user.service';
export const GetUser = createParamDecorator((data, req): User => {
return req.user;
});
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private jwtService: JwtService) {}
constructor(
private jwtService: JwtService,
private userService: UserService,
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
@@ -19,12 +29,15 @@ export class AuthGuard implements CanActivate {
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;
const payload: AccessTokenPayload = await this.jwtService.verifyAsync(
token,
{
secret: JWT_SECRET,
},
);
if (payload.sub) {
request['user'] = await this.userService.findOne(payload.sub);
}
} catch {
throw new UnauthorizedException();
}

View File

@@ -1,18 +0,0 @@
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

@@ -2,10 +2,8 @@ import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UserService } from '../user/user.service';
import { JwtService } from '@nestjs/jwt';
interface AccessTokenPayload {
export interface AccessTokenPayload {
sub: string;
name: string;
isAdmin: boolean;
}
@Injectable()
@@ -29,8 +27,6 @@ export class AuthService {
const payload: AccessTokenPayload = {
sub: user.id,
name: user.name,
isAdmin: user.isAdmin,
};
return {
@@ -38,8 +34,3 @@ export class AuthService {
};
}
}
export type AuthUser = {
id: string;
name: string;
isAdmin: boolean;
};