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