Skip to content

Commit

Permalink
добавил редирект, если пользователь зарегистрирован
Browse files Browse the repository at this point in the history
  • Loading branch information
veglem committed Mar 4, 2024
1 parent 9d02110 commit 7be5c51
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
10 changes: 10 additions & 0 deletions public/src/modules/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ class AuthRequests {
throw Error(body.message)
}
}

CheckUser = async () => {
const {status, body} = await baseRequest(methods.GET, this.#baseUrl + '/check_user')

if (status === 200) {
return body;
} else {
throw Error('not authorized');
}
}
}

class NoteRequests {
Expand Down
3 changes: 2 additions & 1 deletion public/src/modules/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import NotFoundPage from "../pages/notFound/not-found.js";
import NotesPage from "../pages/notes/notes.js";
import {AppUserStore, UserActions} from "../stores/user/userStore.js";
import {AppEventMaker} from "./eventMaker.js";
import {AppDispatcher} from "./dispathcer.js";

class Router {
#currentUrl;
Expand Down Expand Up @@ -33,7 +34,7 @@ class Router {
const notFoundPage = new NotFoundPage(root, config.notFoundPage)
this.registerPage(notFoundPage)

this.redirect(this.#currentUrl)
AppDispatcher.dispatch(UserActions.CHECK_USER);
}

registerPage(page) {
Expand Down
17 changes: 16 additions & 1 deletion public/src/stores/user/userStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class UserStore {
case UserActions.REGISTER:
await this.register(action.payload);
break;
case UserActions.CHANGE_PAGE:
await this.checkUser();
break;
}
})
}
Expand Down Expand Up @@ -77,6 +80,17 @@ class UserStore {
console.log(err);
}
}

async checkUser(){
try {
const res = await AppAuthRequests.CheckUser();
this.#state.isAuth = true;
this.#state.username = res.username;
router.redirect("/notes")
} catch (err) {
router.redirect("/")
}
}
}

export const AppUserStore = new UserStore();
Expand All @@ -85,5 +99,6 @@ export const UserActions = {
LOGIN: "LOGIN",
REGISTER: "REGISTER",
LOGOUT: "LOGOUT",
CHANGE_PAGE: "CHANGE_PAGE"
CHANGE_PAGE: "CHANGE_PAGE",
CHECK_USER: "CHECK_USER"
}

0 comments on commit 7be5c51

Please sign in to comment.