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