mirror of
https://github.com/aleksilassila/reiverr.git
synced 2026-04-24 17:55:11 +02:00
Initial Nest.js backend
This commit is contained in:
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