Merge branch 'dev' of https://git.odit.services/lfk/backend into dev
This commit is contained in:
64
src/models/actions/CreateAddress.ts
Normal file
64
src/models/actions/CreateAddress.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { IsNotEmpty, IsOptional, IsPostalCode, IsString } from 'class-validator';
|
||||
import { Address } from '../entities/Address';
|
||||
|
||||
export class CreateAddress {
|
||||
/**
|
||||
* The address's description.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string;
|
||||
|
||||
/**
|
||||
* The address's first line.
|
||||
* Containing the street and house number.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
address1: string;
|
||||
|
||||
/**
|
||||
* The address's second line.
|
||||
* Containing optional information.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
address2?: string;
|
||||
|
||||
/**
|
||||
* The address's postal code.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@IsPostalCode("DE")
|
||||
postalcode: string;
|
||||
|
||||
/**
|
||||
* The address's city.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
city: string;
|
||||
|
||||
/**
|
||||
* The address's country.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
country: string;
|
||||
|
||||
/**
|
||||
* Creates a Address object based on this.
|
||||
*/
|
||||
public toAddress(): Address {
|
||||
let newAddress: Address = new Address();
|
||||
|
||||
newAddress.address1 = this.address1;
|
||||
newAddress.address2 = this.address2;
|
||||
newAddress.postalcode = this.postalcode;
|
||||
newAddress.city = this.city;
|
||||
newAddress.country = this.country;
|
||||
|
||||
return newAddress;
|
||||
}
|
||||
}
|
||||
58
src/models/actions/CreateAuth.ts
Normal file
58
src/models/actions/CreateAuth.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import * as argon2 from "argon2";
|
||||
import { IsEmail, IsOptional, IsString } from 'class-validator';
|
||||
import * as jsonwebtoken from 'jsonwebtoken';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { config } from '../../config';
|
||||
import { InvalidCredentialsError, PasswordNeededError, UserNotFoundError } from '../../errors/AuthError';
|
||||
import { UsernameOrEmailNeededError } from '../../errors/UserErrors';
|
||||
import { User } from '../entities/User';
|
||||
import { Auth } from '../responses/ResponseAuth';
|
||||
|
||||
export class CreateAuth {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
username?: string;
|
||||
@IsString()
|
||||
password: string;
|
||||
@IsOptional()
|
||||
@IsEmail()
|
||||
@IsString()
|
||||
email?: string;
|
||||
|
||||
public async toAuth(): Promise<Auth> {
|
||||
let newAuth: Auth = new Auth();
|
||||
|
||||
if (this.email === undefined && this.username === undefined) {
|
||||
throw new UsernameOrEmailNeededError();
|
||||
}
|
||||
if (!this.password) {
|
||||
throw new PasswordNeededError()
|
||||
}
|
||||
const found_users = await getConnectionManager().get().getRepository(User).find({ where: [{ username: this.username }, { email: this.email }] });
|
||||
if (found_users.length === 0) {
|
||||
throw new UserNotFoundError()
|
||||
} else {
|
||||
const found_user = found_users[0]
|
||||
if (await argon2.verify(found_user.password, this.password + found_user.uuid)) {
|
||||
const timestamp_accesstoken_expiry = Math.floor(Date.now() / 1000) + 5 * 60
|
||||
delete found_user.password;
|
||||
newAuth.access_token = jsonwebtoken.sign({
|
||||
userdetails: found_user,
|
||||
exp: timestamp_accesstoken_expiry
|
||||
}, config.jwt_secret)
|
||||
newAuth.access_token_expires_at = timestamp_accesstoken_expiry
|
||||
//
|
||||
const timestamp_refresh_expiry = Math.floor(Date.now() / 1000) + 10 * 36000
|
||||
newAuth.refresh_token = jsonwebtoken.sign({
|
||||
refreshtokencount: found_user.refreshTokenCount,
|
||||
userid: found_user.id,
|
||||
exp: timestamp_refresh_expiry
|
||||
}, config.jwt_secret)
|
||||
newAuth.refresh_token_expires_at = timestamp_refresh_expiry
|
||||
} else {
|
||||
throw new InvalidCredentialsError()
|
||||
}
|
||||
}
|
||||
return newAuth;
|
||||
}
|
||||
}
|
||||
83
src/models/actions/CreateGroupContact.ts
Normal file
83
src/models/actions/CreateGroupContact.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
|
||||
import { Address } from '../entities/Address';
|
||||
import { GroupContact } from '../entities/GroupContact';
|
||||
|
||||
export class CreateGroupContact {
|
||||
/**
|
||||
* The contact's first name.
|
||||
*/
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
firstname: string;
|
||||
|
||||
/**
|
||||
* The contact's middle name.
|
||||
* Optional
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
middlename?: string;
|
||||
|
||||
/**
|
||||
* The contact's last name.
|
||||
*/
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
lastname: string;
|
||||
|
||||
/**
|
||||
* The contact's address.
|
||||
* Optional
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
address?: number;
|
||||
|
||||
/**
|
||||
* The contact's phone number.
|
||||
* Optional
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsPhoneNumber("DE")
|
||||
phone?: string;
|
||||
|
||||
/**
|
||||
* The contact's email address.
|
||||
* Optional
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsEmail()
|
||||
email?: string;
|
||||
|
||||
/**
|
||||
* Get's this participant's address from this.address.
|
||||
*/
|
||||
public async getAddress(): Promise<Address> {
|
||||
if (this.address === undefined) {
|
||||
return null;
|
||||
}
|
||||
if (!isNaN(this.address)) {
|
||||
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
|
||||
if (!address) { throw new AddressNotFoundError; }
|
||||
return address;
|
||||
}
|
||||
|
||||
throw new AddressWrongTypeError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Address object based on this.
|
||||
*/
|
||||
public async toGroupContact(): Promise<GroupContact> {
|
||||
let contact: GroupContact = new GroupContact();
|
||||
contact.firstname = this.firstname;
|
||||
contact.middlename = this.middlename;
|
||||
contact.lastname = this.lastname;
|
||||
contact.email = this.email;
|
||||
contact.phone = this.phone;
|
||||
contact.address = await this.getAddress();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
71
src/models/actions/CreateParticipant.ts
Normal file
71
src/models/actions/CreateParticipant.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
|
||||
import { Address } from '../entities/Address';
|
||||
|
||||
export abstract class CreateParticipant {
|
||||
/**
|
||||
* The new participant's first name.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
firstname: string;
|
||||
|
||||
/**
|
||||
* The new participant's middle name.
|
||||
* Optional.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
middlename?: string;
|
||||
|
||||
/**
|
||||
* The new participant's last name.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
lastname: string;
|
||||
|
||||
/**
|
||||
* The new participant's phone number.
|
||||
* Optional.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@IsPhoneNumber("ZZ")
|
||||
phone?: string;
|
||||
|
||||
/**
|
||||
* The new participant's e-mail address.
|
||||
* Optional.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@IsEmail()
|
||||
email?: string;
|
||||
|
||||
/**
|
||||
* The new participant's address.
|
||||
* Must be of type number (address id), createAddress (new address) or address (existing address)
|
||||
* Optional.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
address?: number;
|
||||
|
||||
/**
|
||||
* Get's this participant's address from this.address.
|
||||
*/
|
||||
public async getAddress(): Promise<Address> {
|
||||
if (this.address === undefined) {
|
||||
return null;
|
||||
}
|
||||
if (!isNaN(this.address)) {
|
||||
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
|
||||
if (!address) { throw new AddressNotFoundError; }
|
||||
return address;
|
||||
}
|
||||
|
||||
throw new AddressWrongTypeError;
|
||||
}
|
||||
}
|
||||
51
src/models/actions/CreateRunner.ts
Normal file
51
src/models/actions/CreateRunner.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { IsInt } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { RunnerGroupNotFoundError } from '../../errors/RunnerGroupErrors';
|
||||
import { RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors';
|
||||
import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors';
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||
import { CreateParticipant } from './CreateParticipant';
|
||||
|
||||
export class CreateRunner extends CreateParticipant {
|
||||
|
||||
/**
|
||||
* The new runner's team's id.
|
||||
* Either provide this or his organisation's id.
|
||||
*/
|
||||
@IsInt()
|
||||
group: number;
|
||||
|
||||
/**
|
||||
* Creates a Runner entity from this.
|
||||
*/
|
||||
public async toRunner(): Promise<Runner> {
|
||||
let newRunner: Runner = new Runner();
|
||||
|
||||
newRunner.firstname = this.firstname;
|
||||
newRunner.middlename = this.middlename;
|
||||
newRunner.lastname = this.lastname;
|
||||
newRunner.phone = this.phone;
|
||||
newRunner.email = this.email;
|
||||
newRunner.group = await this.getGroup();
|
||||
newRunner.address = await this.getAddress();
|
||||
|
||||
return newRunner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages all the different ways a group can be provided.
|
||||
*/
|
||||
public async getGroup(): Promise<RunnerGroup> {
|
||||
if (this.group === undefined) {
|
||||
throw new RunnerTeamNeedsParentError();
|
||||
}
|
||||
if (!isNaN(this.group)) {
|
||||
let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group });
|
||||
if (!group) { throw new RunnerGroupNotFoundError; }
|
||||
return group;
|
||||
}
|
||||
|
||||
throw new RunnerOrganisationWrongTypeError;
|
||||
}
|
||||
}
|
||||
37
src/models/actions/CreateRunnerGroup.ts
Normal file
37
src/models/actions/CreateRunnerGroup.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { GroupContactNotFoundError, GroupContactWrongTypeError } from '../../errors/GroupContactErrors';
|
||||
import { GroupContact } from '../entities/GroupContact';
|
||||
|
||||
export abstract class CreateRunnerGroup {
|
||||
/**
|
||||
* The group's name.
|
||||
*/
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The group's contact.
|
||||
* Optional
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
contact?: number;
|
||||
|
||||
/**
|
||||
* Deals with the contact for groups this.
|
||||
*/
|
||||
public async getContact(): Promise<GroupContact> {
|
||||
if (this.contact === undefined) {
|
||||
return null;
|
||||
}
|
||||
if (!isNaN(this.contact)) {
|
||||
let address = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact });
|
||||
if (!address) { throw new GroupContactNotFoundError; }
|
||||
return address;
|
||||
}
|
||||
|
||||
throw new GroupContactWrongTypeError;
|
||||
}
|
||||
}
|
||||
46
src/models/actions/CreateRunnerOrganisation.ts
Normal file
46
src/models/actions/CreateRunnerOrganisation.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { IsInt, IsOptional } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
|
||||
import { Address } from '../entities/Address';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { CreateRunnerGroup } from './CreateRunnerGroup';
|
||||
|
||||
export class CreateRunnerOrganisation extends CreateRunnerGroup {
|
||||
/**
|
||||
* The new organisation's address.
|
||||
* Must be of type number (address id), createAddress (new address) or address (existing address)
|
||||
* Optional.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
address?: number;
|
||||
|
||||
/**
|
||||
* Creates a Participant entity from this.
|
||||
*/
|
||||
public async getAddress(): Promise<Address> {
|
||||
if (this.address === undefined) {
|
||||
return null;
|
||||
}
|
||||
if (!isNaN(this.address)) {
|
||||
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
|
||||
if (!address) { throw new AddressNotFoundError; }
|
||||
return address;
|
||||
}
|
||||
|
||||
throw new AddressWrongTypeError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a RunnerOrganisation entity from this.
|
||||
*/
|
||||
public async toRunnerOrganisation(): Promise<RunnerOrganisation> {
|
||||
let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation();
|
||||
|
||||
newRunnerOrganisation.name = this.name;
|
||||
newRunnerOrganisation.contact = await this.getContact();
|
||||
newRunnerOrganisation.address = await this.getAddress();
|
||||
|
||||
return newRunnerOrganisation;
|
||||
}
|
||||
}
|
||||
43
src/models/actions/CreateRunnerTeam.ts
Normal file
43
src/models/actions/CreateRunnerTeam.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { IsInt, IsNotEmpty } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors';
|
||||
import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||
import { CreateRunnerGroup } from './CreateRunnerGroup';
|
||||
|
||||
export class CreateRunnerTeam extends CreateRunnerGroup {
|
||||
|
||||
/**
|
||||
* The team's parent group (organisation).
|
||||
*/
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
parentGroup: number;
|
||||
|
||||
public async getParent(): Promise<RunnerOrganisation> {
|
||||
if (this.parentGroup === undefined) {
|
||||
throw new RunnerTeamNeedsParentError();
|
||||
}
|
||||
if (!isNaN(this.parentGroup)) {
|
||||
let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup });
|
||||
if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; }
|
||||
return parentGroup;
|
||||
}
|
||||
|
||||
throw new RunnerOrganisationWrongTypeError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a RunnerTeam entity from this.
|
||||
*/
|
||||
public async toRunnerTeam(): Promise<RunnerTeam> {
|
||||
let newRunnerTeam: RunnerTeam = new RunnerTeam();
|
||||
|
||||
newRunnerTeam.name = this.name;
|
||||
newRunnerTeam.parentGroup = await this.getParent();
|
||||
newRunnerTeam.contact = await this.getContact()
|
||||
|
||||
return newRunnerTeam;
|
||||
}
|
||||
}
|
||||
30
src/models/actions/CreateTrack.ts
Normal file
30
src/models/actions/CreateTrack.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
||||
import { Track } from '../entities/Track';
|
||||
|
||||
export class CreateTrack {
|
||||
/**
|
||||
* The track's name.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The track's distance in meters (must be greater 0).
|
||||
*/
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
distance: number;
|
||||
|
||||
/**
|
||||
* Converts a Track object based on this.
|
||||
*/
|
||||
public toTrack(): Track {
|
||||
let newTrack: Track = new Track();
|
||||
|
||||
newTrack.name = this.name;
|
||||
newTrack.distance = this.distance;
|
||||
|
||||
return newTrack;
|
||||
}
|
||||
}
|
||||
119
src/models/actions/CreateUser.ts
Normal file
119
src/models/actions/CreateUser.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import * as argon2 from "argon2";
|
||||
import { IsEmail, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import * as uuid from 'uuid';
|
||||
import { UsernameOrEmailNeededError } from '../../errors/UserErrors';
|
||||
import { UserGroupNotFoundError } from '../../errors/UserGroupErrors';
|
||||
import { User } from '../entities/User';
|
||||
import { UserGroup } from '../entities/UserGroup';
|
||||
|
||||
export class CreateUser {
|
||||
/**
|
||||
* The new user's first name.
|
||||
*/
|
||||
@IsString()
|
||||
firstname: string;
|
||||
|
||||
/**
|
||||
* The new user's middle name.
|
||||
* Optinal.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
middlename?: string;
|
||||
|
||||
/**
|
||||
* The new user's last name.
|
||||
*/
|
||||
@IsString()
|
||||
lastname: string;
|
||||
|
||||
/**
|
||||
* The new user's username.
|
||||
* You have to provide at least one of: {email, username}.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
username?: string;
|
||||
|
||||
/**
|
||||
* The new user's email address.
|
||||
* You have to provide at least one of: {email, username}.
|
||||
*/
|
||||
@IsEmail()
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
email?: string;
|
||||
|
||||
/**
|
||||
* The new user's phone number.
|
||||
* Optional
|
||||
*/
|
||||
@IsPhoneNumber("ZZ")
|
||||
@IsOptional()
|
||||
phone?: string;
|
||||
|
||||
/**
|
||||
* The new user's password.
|
||||
* This will of course not be saved in plaintext :)
|
||||
*/
|
||||
@IsString()
|
||||
password: string;
|
||||
|
||||
/**
|
||||
* The new user's groups' id(s).
|
||||
* You can provide either one groupId or an array of groupIDs.
|
||||
* Optional.
|
||||
*/
|
||||
@IsOptional()
|
||||
groupId?: number[] | number
|
||||
|
||||
//TODO: ProfilePics
|
||||
|
||||
/**
|
||||
* Converts this to a User Entity.
|
||||
*/
|
||||
public async toUser(): Promise<User> {
|
||||
let newUser: User = new User();
|
||||
|
||||
if (this.email === undefined && this.username === undefined) {
|
||||
throw new UsernameOrEmailNeededError();
|
||||
}
|
||||
|
||||
if (this.groupId) {
|
||||
if (!Array.isArray(this.groupId)) {
|
||||
this.groupId = [this.groupId]
|
||||
}
|
||||
const groupIDs: number[] = this.groupId
|
||||
let errors = 0
|
||||
const validateusergroups = async () => {
|
||||
let foundgroups = []
|
||||
for (const g of groupIDs) {
|
||||
const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g });
|
||||
if (found.length === 0) {
|
||||
errors++
|
||||
} else {
|
||||
foundgroups.push(found[0])
|
||||
}
|
||||
}
|
||||
newUser.groups = foundgroups
|
||||
}
|
||||
await validateusergroups()
|
||||
if (errors !== 0) {
|
||||
throw new UserGroupNotFoundError();
|
||||
}
|
||||
}
|
||||
|
||||
newUser.email = this.email
|
||||
newUser.username = this.username
|
||||
newUser.firstname = this.firstname
|
||||
newUser.middlename = this.middlename
|
||||
newUser.lastname = this.lastname
|
||||
newUser.uuid = uuid.v4()
|
||||
newUser.phone = this.phone
|
||||
newUser.password = await argon2.hash(this.password + newUser.uuid);
|
||||
//TODO: ProfilePics
|
||||
|
||||
return newUser;
|
||||
}
|
||||
}
|
||||
30
src/models/actions/CreateUserGroup.ts
Normal file
30
src/models/actions/CreateUserGroup.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { IsOptional, IsString } from 'class-validator';
|
||||
import { UserGroup } from '../entities/UserGroup';
|
||||
|
||||
export class CreateUserGroup {
|
||||
/**
|
||||
* The new group's name.
|
||||
*/
|
||||
@IsString()
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The new group's description.
|
||||
* Optinal.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string;
|
||||
|
||||
/**
|
||||
* Converts this to a UserGroup entity.
|
||||
*/
|
||||
public async toUserGroup(): Promise<UserGroup> {
|
||||
let newUserGroup: UserGroup = new UserGroup();
|
||||
|
||||
newUserGroup.name = this.name;
|
||||
newUserGroup.description = this.description;
|
||||
|
||||
return newUserGroup;
|
||||
}
|
||||
}
|
||||
36
src/models/actions/HandleLogout.ts
Normal file
36
src/models/actions/HandleLogout.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { IsString } from 'class-validator';
|
||||
import * as jsonwebtoken from 'jsonwebtoken';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { config } from '../../config';
|
||||
import { IllegalJWTError, JwtNotProvidedError, RefreshTokenCountInvalidError, UserNotFoundError } from '../../errors/AuthError';
|
||||
import { User } from '../entities/User';
|
||||
import { Logout } from '../responses/ResponseLogout';
|
||||
|
||||
export class HandleLogout {
|
||||
@IsString()
|
||||
token: string;
|
||||
|
||||
public async logout(): Promise<Logout> {
|
||||
let logout: Logout = new Logout();
|
||||
if (!this.token || this.token === undefined) {
|
||||
throw new JwtNotProvidedError()
|
||||
}
|
||||
let decoded;
|
||||
try {
|
||||
decoded = jsonwebtoken.verify(this.token, config.jwt_secret)
|
||||
} catch (error) {
|
||||
throw new IllegalJWTError()
|
||||
}
|
||||
logout.timestamp = Math.floor(Date.now() / 1000)
|
||||
let found_user: User = await getConnectionManager().get().getRepository(User).findOne({ id: decoded["userid"] });
|
||||
if (!found_user) {
|
||||
throw new UserNotFoundError()
|
||||
}
|
||||
if (found_user.refreshTokenCount !== decoded["refreshtokencount"]) {
|
||||
throw new RefreshTokenCountInvalidError()
|
||||
}
|
||||
found_user.refreshTokenCount++;
|
||||
await getConnectionManager().get().getRepository(User).update({ id: found_user.id }, found_user)
|
||||
return logout;
|
||||
}
|
||||
}
|
||||
50
src/models/actions/RefreshAuth.ts
Normal file
50
src/models/actions/RefreshAuth.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { IsString } from 'class-validator';
|
||||
import * as jsonwebtoken from 'jsonwebtoken';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { config } from '../../config';
|
||||
import { IllegalJWTError, JwtNotProvidedError, RefreshTokenCountInvalidError, UserNotFoundError } from '../../errors/AuthError';
|
||||
import { User } from '../entities/User';
|
||||
import { Auth } from '../responses/ResponseAuth';
|
||||
|
||||
export class RefreshAuth {
|
||||
@IsString()
|
||||
token: string;
|
||||
|
||||
public async toAuth(): Promise<Auth> {
|
||||
let newAuth: Auth = new Auth();
|
||||
if (!this.token || this.token === undefined) {
|
||||
throw new JwtNotProvidedError()
|
||||
}
|
||||
let decoded
|
||||
try {
|
||||
decoded = jsonwebtoken.verify(this.token, config.jwt_secret)
|
||||
} catch (error) {
|
||||
throw new IllegalJWTError()
|
||||
}
|
||||
const found_user = await getConnectionManager().get().getRepository(User).findOne({ id: decoded["userid"] });
|
||||
if (!found_user) {
|
||||
throw new UserNotFoundError()
|
||||
}
|
||||
if (found_user.refreshTokenCount !== decoded["refreshtokencount"]) {
|
||||
throw new RefreshTokenCountInvalidError()
|
||||
}
|
||||
delete found_user.password;
|
||||
const timestamp_accesstoken_expiry = Math.floor(Date.now() / 1000) + 5 * 60
|
||||
delete found_user.password;
|
||||
newAuth.access_token = jsonwebtoken.sign({
|
||||
userdetails: found_user,
|
||||
exp: timestamp_accesstoken_expiry
|
||||
}, config.jwt_secret)
|
||||
newAuth.access_token_expires_at = timestamp_accesstoken_expiry
|
||||
//
|
||||
const timestamp_refresh_expiry = Math.floor(Date.now() / 1000) + 10 * 36000
|
||||
newAuth.refresh_token = jsonwebtoken.sign({
|
||||
refreshtokencount: found_user.refreshTokenCount,
|
||||
userid: found_user.id,
|
||||
exp: timestamp_refresh_expiry
|
||||
}, config.jwt_secret)
|
||||
newAuth.refresh_token_expires_at = timestamp_refresh_expiry
|
||||
|
||||
return newAuth;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user