login and authentication

login via totp
authentication via access and refresh tokens
This commit is contained in:
Julian Krauser 2024-08-22 11:40:31 +02:00
parent 6696975bee
commit e1ec65350d
28 changed files with 3750 additions and 0 deletions

View 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");
});
}
}