Added user login & registration. Added SQL file for postgres database.
This commit is contained in:
42
backend/nestjs-seshat-api/src/auth/auth.service.ts
Normal file
42
backend/nestjs-seshat-api/src/auth/auth.service.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserEntity } from 'src/users/users.entity';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { AuthRefreshService } from './auth.refresh.service';
|
||||
import { AuthAccessService } from './auth.access.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private accessTokens: AuthAccessService,
|
||||
private refreshTokens: AuthRefreshService,
|
||||
private users: UsersService,
|
||||
) { }
|
||||
|
||||
|
||||
async login(user: UserEntity) {
|
||||
return this.renew(user, null);
|
||||
}
|
||||
|
||||
async validate(
|
||||
username: string,
|
||||
password: string,
|
||||
): Promise<UserEntity | null> {
|
||||
return await this.users.findOne({ username, password });
|
||||
}
|
||||
|
||||
async renew(
|
||||
user: UserEntity,
|
||||
refresh_token: string
|
||||
): Promise<{ access_token: string, exp: number, refresh_token: string, refresh_exp: number }> {
|
||||
const new_refresh_data = await this.refreshTokens.generate(user, refresh_token);
|
||||
const new_refresh_token = new_refresh_data.refresh_token;
|
||||
const new_refresh_exp = new_refresh_data.exp;
|
||||
const access_token = await this.accessTokens.generate(user);
|
||||
|
||||
return {
|
||||
...access_token,
|
||||
refresh_token: new_refresh_token,
|
||||
refresh_exp: new_refresh_exp,
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user