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
8
src/command/refreshCommand.ts
Normal file
8
src/command/refreshCommand.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
export interface CreateRefreshCommand {
|
||||
userId: number;
|
||||
}
|
||||
|
||||
export interface DeleteRefreshCommand {
|
||||
id: number;
|
||||
userId: number;
|
||||
}
|
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");
|
||||
});
|
||||
}
|
||||
}
|
5
src/command/userCommand.ts
Normal file
5
src/command/userCommand.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
export interface CreateUserCommand {
|
||||
mail: string;
|
||||
username: string;
|
||||
secret: string;
|
||||
}
|
30
src/command/userCommandHandler.ts
Normal file
30
src/command/userCommandHandler.ts
Normal 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");
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue