diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index 2eb4b84..e5faf5f 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -9,6 +9,8 @@ import RegisterView from "./views/accounts/RegisterView.js"; import LogoutView from "./views/accounts/LogoutView.js"; import { Client } from "./api/client.js"; +import AbstractProtectedView from "./views/AbstractRedirectView.js"; +import AbstractRedirectView from "./views/AbstractRedirectView.js"; let client = new Client(location.protocol + "//" + location.host) @@ -64,6 +66,10 @@ const router = async (uri = "") => { await lastView.leavePage(); const view = new match.route.view(getParams(match)); + + if (view instanceof AbstractRedirectView && await view.redirect()) + return 1; + lastView = view; let content = await view.getHtml(); diff --git a/frontend/static/js/views/AbstractAuthentificateView.js b/frontend/static/js/views/AbstractAuthentificateView.js new file mode 100644 index 0000000..705630f --- /dev/null +++ b/frontend/static/js/views/AbstractAuthentificateView.js @@ -0,0 +1,18 @@ +import { client, navigateTo } from "../index.js"; +import AbstractRedirectView from "./AbstractRedirectView.js"; + +export default class extends AbstractRedirectView{ + constructor(params) { + super(params, "/login"); + } + + async redirect() + { + if (await client.isAuthentificate() === false) + { + navigateTo(this.redirect_url); + return 1; + } + return 0; + } +} diff --git a/frontend/static/js/views/AbstractRedirectView.js b/frontend/static/js/views/AbstractRedirectView.js new file mode 100644 index 0000000..7f139cd --- /dev/null +++ b/frontend/static/js/views/AbstractRedirectView.js @@ -0,0 +1,15 @@ +import { navigateTo } from "../index.js"; +import AbstractView from "./AbstractView.js"; + +export default class extends AbstractView{ + constructor(params, url) + { + super(params); + this.redirect_url = url; + } + + async redirect() + { + navigateTo(url); + } +} diff --git a/frontend/static/js/views/AbstractUnAuthentificateView.js b/frontend/static/js/views/AbstractUnAuthentificateView.js new file mode 100644 index 0000000..c8b4eb2 --- /dev/null +++ b/frontend/static/js/views/AbstractUnAuthentificateView.js @@ -0,0 +1,16 @@ +import { client, navigateTo } from "../index.js"; +import AbstractRedirectView from "./AbstractRedirectView.js"; + +export default class extends AbstractRedirectView{ + constructor(params, url) { + super(params, url); + } + + async redirect() + { + if (await client.isAuthentificate() === false) + return 0; + navigateTo(this.redirect_url); + return 1; + } +} diff --git a/frontend/static/js/views/HomeView.js b/frontend/static/js/views/HomeView.js index 79880c3..e18f727 100644 --- a/frontend/static/js/views/HomeView.js +++ b/frontend/static/js/views/HomeView.js @@ -1,18 +1,13 @@ -import AbstractView from "./AbstractView.js"; -import { client, navigateTo } from "../index.js"; +import AbstractAuthentificateView from "./AbstractAuthentificateView.js"; -export default class extends AbstractView { +export default class extends AbstractAuthentificateView { constructor(params) { super(params); this.setTitle("Home"); + this.redirect_url = "/login" } async getHtml() { - if (await client.isAuthentificate() === false) - { - navigateTo("/login"); - return; - } return `

HOME

Logout diff --git a/frontend/static/js/views/accounts/LoginView.js b/frontend/static/js/views/accounts/LoginView.js index f92c5de..92835e1 100644 --- a/frontend/static/js/views/accounts/LoginView.js +++ b/frontend/static/js/views/accounts/LoginView.js @@ -1,5 +1,5 @@ -import AbstractView from "../AbstractView.js"; import { client, navigateTo } from "../../index.js"; +import AbstractUnAuthentificateView from "../AbstractUnAuthentificateView.js"; async function login() { @@ -27,9 +27,9 @@ async function login() }); } -export default class extends AbstractView { +export default class extends AbstractUnAuthentificateView { constructor(params) { - super(params); + super(params, "/home"); } async postInit() @@ -39,11 +39,6 @@ export default class extends AbstractView { } async getHtml() { - if (await client.isAuthentificate()) - { - navigateTo("/home") - return; - } return `
diff --git a/frontend/static/js/views/accounts/LogoutView.js b/frontend/static/js/views/accounts/LogoutView.js index 0df2227..82e4f8e 100644 --- a/frontend/static/js/views/accounts/LogoutView.js +++ b/frontend/static/js/views/accounts/LogoutView.js @@ -1,13 +1,12 @@ import { client, navigateTo } from "../../index.js"; -import AbstractView from "../AbstractView.js"; +import AbstractAuthentificateView from "../AbstractAuthentificateView.js"; -export default class extends AbstractView +export default class extends AbstractAuthentificateView { constructor(params) { super(params); this.setTitle("Logout"); - if (client.logged) - client.logout(); + client.logout(); navigateTo("/login") } } \ No newline at end of file diff --git a/frontend/static/js/views/accounts/RegisterView.js b/frontend/static/js/views/accounts/RegisterView.js index d026719..c6d784f 100644 --- a/frontend/static/js/views/accounts/RegisterView.js +++ b/frontend/static/js/views/accounts/RegisterView.js @@ -1,5 +1,5 @@ -import AbstractView from "../AbstractView.js"; import { client, navigateTo } from "../../index.js"; +import AbstractUnAuthentificateView from "../AbstractUnAuthentificateView.js"; async function register() { @@ -22,9 +22,9 @@ async function register() }); } -export default class extends AbstractView { +export default class extends AbstractUnAuthentificateView { constructor(params) { - super(params); + super(params, "/home"); } async postInit() @@ -34,11 +34,6 @@ export default class extends AbstractView { } async getHtml() { - if (await client.isAuthentificate()) - { - navigateTo("/home") - return; - } return `