Added user login & registration. Added SQL file for postgres database.
This commit is contained in:
37
backend/nestjs-seshat-api/src/auth/auth.access.service.ts
Normal file
37
backend/nestjs-seshat-api/src/auth/auth.access.service.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import * as moment from 'moment';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { UserEntity } from 'src/users/users.entity';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class AuthAccessService {
|
||||
constructor(
|
||||
private jwts: JwtService,
|
||||
private config: ConfigService,
|
||||
) { }
|
||||
|
||||
async generate(user: UserEntity) {
|
||||
const now = new Date();
|
||||
const limit = parseInt(this.config.getOrThrow('AUTH_JWT_ACCESS_TOKEN_EXPIRATION_MS'));
|
||||
const expiration = moment(now).add(limit, 'ms').toDate();
|
||||
|
||||
const token = await this.jwts.signAsync(
|
||||
{
|
||||
username: user.userLogin,
|
||||
sub: user.userId,
|
||||
iat: now.getTime(),
|
||||
nbf: now.getTime(),
|
||||
exp: expiration.getTime(),
|
||||
},
|
||||
{
|
||||
secret: this.config.getOrThrow('AUTH_JWT_ACCESS_TOKEN_SECRET'),
|
||||
}
|
||||
);
|
||||
|
||||
return {
|
||||
access_token: token,
|
||||
exp: expiration.getTime(),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user