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,8 @@
export interface CreateRefreshCommand {
userId: number;
}
export interface DeleteRefreshCommand {
id: number;
userId: number;
}

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

View file

@ -0,0 +1,5 @@
export interface CreateUserCommand {
mail: string;
username: string;
secret: string;
}

View file

@ -0,0 +1,30 @@
import { dataSource } from "../data-source";
import { user } from "../entity/user";
import InternalException from "../exceptions/internalException";
import { CreateUserCommand } from "./userCommand";
export default abstract class UserCommandHandler {
/**
* @description create user
* @param CreateUserCommand
* @returns {Promise<string>}
*/
static async create(createUser: CreateUserCommand): Promise<string> {
return await dataSource
.createQueryBuilder()
.insert()
.into(user)
.values({
username: createUser.username,
mail: createUser.mail,
secret: createUser.secret,
})
.execute()
.then((result) => {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed saving user");
});
}
}