login and authentication
login via totp authentication via access and refresh tokens
This commit is contained in:
parent
6696975bee
commit
e1ec65350d
28 changed files with 3750 additions and 0 deletions
39
src/command/refreshCommandHandler.ts
Normal file
39
src/command/refreshCommandHandler.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import { dataSource } from "../data-source";
|
||||
import { refresh } from "../entity/refresh";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
import { JWTHelper } from "../helpers/jwtHelper";
|
||||
import UserService from "../service/userService";
|
||||
import { JWTRefresh } from "../type/jwtTypes";
|
||||
import { CreateRefreshCommand } from "./refreshCommand";
|
||||
import ms from "ms";
|
||||
|
||||
export default abstract class RefreshCommandHandler {
|
||||
/**
|
||||
* @description create and save refreshToken to user
|
||||
* @param CreateRefreshCommand
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
static async create(createRefresh: CreateRefreshCommand): Promise<string> {
|
||||
let createRefreshToken: JWTRefresh = {
|
||||
userId: createRefresh.userId,
|
||||
};
|
||||
const refreshToken = await JWTHelper.create(createRefreshToken);
|
||||
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(refresh)
|
||||
.values({
|
||||
token: refreshToken,
|
||||
user: await UserService.getById(createRefresh.userId),
|
||||
expiry: ms(process.env.REFRESH_EXPIRATION),
|
||||
})
|
||||
.execute()
|
||||
.then((result) => {
|
||||
return refreshToken;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("Failed saving refresh token");
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue