add token expiry date to login response
This commit is contained in:
parent
5dd79374c1
commit
fac606cf97
3 changed files with 18 additions and 4 deletions
|
|
@ -32,13 +32,14 @@ export class AuthController {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create JWT
|
// Create JWT
|
||||||
const token = encrypt.generateToken({
|
const tokenInfo = encrypt.generateToken({
|
||||||
id: user.id,
|
id: user.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
const responseDto = new LoginResponseDto();
|
const responseDto = new LoginResponseDto();
|
||||||
responseDto.userData = this.mapper.toDto(user);
|
responseDto.userData = this.mapper.toDto(user);
|
||||||
responseDto.token = token;
|
responseDto.token = tokenInfo.token;
|
||||||
|
responseDto.expiryDate = tokenInfo.expiryDate;
|
||||||
return responseDto;
|
return responseDto;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,5 @@ import { UserDto } from "./UserDto.js";
|
||||||
export class LoginResponseDto {
|
export class LoginResponseDto {
|
||||||
userData?: UserDto;
|
userData?: UserDto;
|
||||||
token?: string;
|
token?: string;
|
||||||
|
expiryDate? : Date;
|
||||||
}
|
}
|
||||||
|
|
@ -4,6 +4,16 @@ import dotenv from "dotenv";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
const { JWT_SECRET = "" } = process.env;
|
const { JWT_SECRET = "" } = process.env;
|
||||||
|
|
||||||
|
export class tokenInfo{
|
||||||
|
constructor(token: string, expiryDate: Date){
|
||||||
|
this.token = token;
|
||||||
|
this.expiryDate = expiryDate;
|
||||||
|
}
|
||||||
|
token: string;
|
||||||
|
expiryDate: Date;
|
||||||
|
}
|
||||||
|
|
||||||
export class encrypt {
|
export class encrypt {
|
||||||
static async encryptpass(password: string) {
|
static async encryptpass(password: string) {
|
||||||
return bcrypt.hashSync(password, 12);
|
return bcrypt.hashSync(password, 12);
|
||||||
|
|
@ -13,8 +23,10 @@ export class encrypt {
|
||||||
}
|
}
|
||||||
|
|
||||||
static generateToken(payload: object) {
|
static generateToken(payload: object) {
|
||||||
// @todo currently, we have an error here!
|
let expiryDate = new Date();
|
||||||
return jwt.sign(payload, JWT_SECRET, { expiresIn: "1d" });
|
expiryDate.setDate(expiryDate.getDate() + 1); // 1 day
|
||||||
|
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: "1d" });
|
||||||
|
return new tokenInfo(token, expiryDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
static verifyToken(token: string) {
|
static verifyToken(token: string) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue