Skip to content

Commit

Permalink
fix: correct authentification with new backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Regikon committed Nov 5, 2024
1 parent 9eab052 commit 85e200a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Api } from '../../modules/Api/Api.js';
import state from '../../modules/AppState/AppState.js';
import { ComponentModel } from '../../modules/Components/Component.js';
import USER_TYPE from '../../modules/UserSession/UserType.js';

export class ApplicantRegistrationFormModel extends ComponentModel {
validate(formData) {
Expand All @@ -17,6 +18,6 @@ export class ApplicantRegistrationFormModel extends ComponentModel {
}

async register(formData) {
return Api.registerApplicant(formData);
return state.userSession.register(USER_TYPE.APPLICANT, formData);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Api } from '../../modules/Api/Api.js';
import { ComponentModel } from '../../modules/Components/Component.js';
import state from '../../modules/AppState/AppState.js';
import USER_TYPE from '../../modules/UserSession/UserType.js';

export class EmployerRegistrationFormModel extends ComponentModel {
validate(formData) {
Expand All @@ -17,6 +18,6 @@ export class EmployerRegistrationFormModel extends ComponentModel {
}

async register(formData) {
return Api.registerEmployer(formData);
return state.userSession.register(USER_TYPE.EMPLOYER, formData);
}
}
8 changes: 4 additions & 4 deletions src/modules/Api/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ const fetchCorsJson = (url, { method = 'GET', credentials = 'same-origin', body
};
export class Api {
static isAuthenticated = async () => {
return fetchCorsJson(backendApi.get('authenticated'), {
const response = await fetchCorsJson(backendApi.get('authenticated'), {
method: 'POST',
credentials: 'include',
}).then((res) => {
return res.json();
});
return unpackStandardApiCall(response);
};

static login = async ({ userType, email, password }) => {
Expand Down Expand Up @@ -263,9 +262,10 @@ export class Api {
};

static logout = async () => {
return fetchCorsJson(backendApi.get('logout'), {
const response = await fetchCorsJson(backendApi.get('logout'), {
method: 'POST',
credentials: 'include',
});
return unpackStandardApiCall(response);
};
}
12 changes: 6 additions & 6 deletions src/modules/Router/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,27 @@ export class Router {
}

const newPage = this.#routes.has(url.pathname)
? new (this.#routes.get(url.pathname))({ url })
: new NotFoundPage({ url });
this._replacePage(newPage);
? this.#routes.get(url.pathname)
: NotFoundPage;
this._replacePage(newPage, url);
} catch (err) {
if (err instanceof ForbiddenPage) {
this.navigate(err.redirectUrl, true, true);
return;
}
if (err instanceof NotFoundError) {
this._replacePage(new NotFoundPage({ url }));
this._replacePage(NotFoundPage, url);
return;
}
throw err;
}
}

_replacePage(newPage) {
_replacePage(newPageClass, newPageUrl) {
if (this.#currentPage) {
this.#currentPage.cleanup();
}
this.#currentPage = newPage;
this.#currentPage = new newPageClass({ url: newPageUrl });
const app = document.getElementById(APP_ID);
app.innerHTML = '';
app.appendChild(this.#currentPage.render());
Expand Down
90 changes: 66 additions & 24 deletions src/modules/UserSession/UserSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,89 @@ export const RUSSIAN_USER_TYPE = {};
RUSSIAN_USER_TYPE[USER_TYPE.EMPLOYER] = 'Работодатель';
RUSSIAN_USER_TYPE[USER_TYPE.APPLICANT] = 'Соискатель';

class User {
constructor(backendUser) {
this.id = backendUser.id;
this.firstName = backendUser.firstName;
this.secondName = backendUser.lastName;
this.city = backendUser.cityName;
this.birthDate = backendUser.birthDate;
this.avatar = backendUser.pathToProfileAvatar;
this.contacts = backendUser.contacts;
this.education = backendUser.education;
this.email = backendUser.email;
}
}

export class UserSession {
#isLoggedIn;
#userType;
#user;

constructor() {
this.#isLoggedIn = false;
this.#userType = undefined;
}

async checkAuthorization() {
return Api.isAuthenticated().then(
(val) => {
if (val.user) {
this.#isLoggedIn = true;
this.#userType = val.user.usertype;
return true;
}
this.#isLoggedIn = false;
return false;
},
() => {
this.#isLoggedIn = false;
return false;
},
);
try {
const authResponse = await Api.isAuthenticated();
this.#isLoggedIn = true;
this.#userType = authResponse.userType;
this.#user = new User(authResponse);
} catch (err) {
console.log(err);
this.#isLoggedIn = false;
this.#userType = undefined;
this.#user = undefined;
return err;
}
}

async login(body) {
const loginResponse = await Api.login(body);
if (!loginResponse) {
try {
const loginResponse = await Api.login(body);
this.#isLoggedIn = true;
this.#userType = body.userType;
this.#user = new User(loginResponse);
return '';
} catch (err) {
console.log(err);
this.#isLoggedIn = false;
this.#userType = undefined;
this.#user = undefined;
throw err;
}
this.#isLoggedIn = true;
this.#userType = loginResponse.userType;
return true;
}

async logout() {
this.#isLoggedIn = false;
Api.logout().then(() => router.navigate(new URL(location.href), true, false));
try {
await Api.logout();
this.#isLoggedIn = false;
this.#userType = undefined;
this.#user = undefined;
router.navigate(new URL(location.href), true, false);
} catch (err) {
console.log(err);
}
}

async register(userType, body) {
try {
const response =
userType === USER_TYPE.APPLICANT
? await Api.registerApplicant(body)
: await Api.registerEmployer(body);
this.#isLoggedIn = true;
this.#userType = userType;
this.#user = new User(response);
} catch (err) {
console.log(err);
this.#isLoggedIn = false;
this.#userType = undefined;
this.#user = undefined;
throw err;
}
}

get isLoggedIn() {
Expand All @@ -64,11 +106,11 @@ export class UserSession {
}

get userId() {
return '123123';
return this.#user.id;
}

getUserFullName() {
return 'Имя Фамилия';
return this.#user ? `${this.#user.firstName} ${this.#user.secondName}` : undefined;
}

goToHomePageIfLoggedIn() {
Expand Down

0 comments on commit 85e200a

Please sign in to comment.