armisa-models
Version:
models of armisa!
68 lines (58 loc) • 2.63 kB
text/typescript
import { AxiosError } from "axios";
import { ElementsOfFormFactory } from "../../../Page/ElementsOfFormFactory";
import { AuthFactory, ILoginResponse } from "../../AuthFactory";
import { useAxios, useUrls } from "../../Models/StorageManager/TokenInfo";
export class LoginByUserPass {
public static createNew(authFactory: AuthFactory): LoginByUserPass | null {
const loginByUserPass = new LoginByUserPass(authFactory);
return loginByUserPass;
}
#urls = useUrls('BaseCode', 'auth');
#apiAxios = useAxios(this.#urls);
private constructor(public authFactory: AuthFactory) {
}
get #elementsOfForm(): ElementsOfFormFactory {
return this.authFactory.elementsOfForm;
}
login = () => {
this.#elementsOfForm.showWaitingFormSpinner();
const user = this.authFactory.getUser().value || '';
const pass = this.authFactory.getPass().value || '';
const rememberMe = this.authFactory.getRememberMe().value || false;
this.loginAxios(user, pass, rememberMe).then(response => {
if (response.isSuccess) {
this.#elementsOfForm.closeWaitingFormSpinner();
} else {
this.#elementsOfForm.closeWaitingFormSpinner();
this.#elementsOfForm.showInvalidArgumentMessageBox(response.message);
}
}).catch(error => {
this.#elementsOfForm.closeWaitingFormSpinner();
this.#elementsOfForm.showErrorMessageBox(error);
});
}
loginAxios = (user: string, pass: string, rememberMe: boolean): Promise<ILoginResponse> => {
return new Promise<ILoginResponse>((resolve, reject) => {
const data = { userName: user, passworrd: pass };
this.#apiAxios.post<ILoginResponse>(this.#urls.login, data).then((res) => {
if (res.status === 200) {
this.authFactory.logedIn(res.data, rememberMe, this.routerPath);
resolve(res.data);
} else {
reject(`requeset to ${this.controllerPath}/${this.actionPath} reject by status : ${res.status} ${res.statusText}`);
}
}).catch((error: AxiosError) => {
reject(`exception error on login user error : ${error}`);
});
});
}
public get controllerPath(): string {
return this.#urls.controller;
}
public get actionPath(): string {
return this.#urls.login;
}
public get routerPath(): string {
return '/home';
}
}