diff --git a/src/common/layouts/BaseLayoutElement/BaseLayoutElement.ts b/src/common/layouts/BaseLayoutElement/BaseLayoutElement.ts index 2144a1d..a64c1db 100644 --- a/src/common/layouts/BaseLayoutElement/BaseLayoutElement.ts +++ b/src/common/layouts/BaseLayoutElement/BaseLayoutElement.ts @@ -3,6 +3,7 @@ import { target } from "@github/catalyst"; class BaseLayoutElement extends HTMLElement { @target slotted: HTMLElement; public isLayout: boolean = true; + public _slotted: string; constructor() { super(); } @@ -19,7 +20,9 @@ class BaseLayoutElement extends HTMLElement { }; setElement = (newTag: string) => { - this.slotted.innerHTML = `<${newTag}>${newTag}>`; + const _slotted = `<${newTag}>${newTag}>`; + this._slotted = _slotted; + this.slotted.innerHTML = _slotted; }; } diff --git a/src/components/app-link/AppLinkElement.ts b/src/components/app-link/AppLinkElement.ts index 8c3b134..8f88deb 100644 --- a/src/components/app-link/AppLinkElement.ts +++ b/src/components/app-link/AppLinkElement.ts @@ -21,13 +21,15 @@ class AppLinkElement extends HTMLElement { this.routerService = this.appMain?.routerService; this.update(); if (isTrue(this.goBack)) { - window.addEventListener("routechanged", () => { - this.update(); - }); + window.addEventListener("routechanged", this.update); } } - public disconnectedCallback(): void {} + public disconnectedCallback(): void { + if (isTrue(this.goBack)) { + window.removeEventListener("routechanged", this.update); + } + } goTo = () => { if (!isTrue(this.goBack) && this.to) { @@ -55,7 +57,7 @@ class AppLinkElement extends HTMLElement { >`}`; } - update() { + update = () => { render(this.render(), this); - } + }; } diff --git a/src/components/app-main/AppMainElement.ts b/src/components/app-main/AppMainElement.ts index 84f9d40..4a470da 100644 --- a/src/components/app-main/AppMainElement.ts +++ b/src/components/app-main/AppMainElement.ts @@ -27,11 +27,12 @@ class AppMainElement extends HTMLElement { path: "/", component: "home-page", layout: "menu-layout", - middleware: this.middleAuth, }, { path: "/home", component: "home-page", + layout: "menu-layout", + middleware: this.middleAuth, }, { path: "/register", @@ -46,10 +47,16 @@ class AppMainElement extends HTMLElement { { path: "/unauthorized", component: "login-page", + layout: "menu-layout", }, { - path: "token-expired", + path: "/token-expired", component: "login-page", + layout: "menu-layout", + }, + { + path: "/logout", + component: "logout-page", }, ]); this.routerService.init(); diff --git a/src/core/services/router-service/RouterService.ts b/src/core/services/router-service/RouterService.ts index 98503ca..324da51 100644 --- a/src/core/services/router-service/RouterService.ts +++ b/src/core/services/router-service/RouterService.ts @@ -43,7 +43,6 @@ class RouterService { update() { if (!this._routes) return; - window.dispatchEvent(this.domEvents.routechanged); const path = window.location.pathname; const _mainRoot = this.mainRoot; const route = this.routerState; @@ -98,12 +97,12 @@ class RouterService { _mainRoot.appendChild(_newElement); } } - return; } else { const newRoute = this.findByPath(); this.historyStack.push(newRoute); this.update(); } + window.dispatchEvent(this.domEvents.routechanged); } goTo(path: string) { diff --git a/src/core/store/AuthStore.ts b/src/core/store/AuthStore.ts index 5a6ed18..7622e02 100644 --- a/src/core/store/AuthStore.ts +++ b/src/core/store/AuthStore.ts @@ -5,6 +5,9 @@ class AuthStore { private _token; private _userDetails; private authService: AuthService; + private domEvents: any = { + tokenchange: new Event("tokenchange"), + }; constructor(private appService: AppService) { this.token = localStorage.getItem("token"); this.authService = new AuthService(this.appService); @@ -19,6 +22,7 @@ class AuthStore { set token(token) { this._token = token; localStorage.setItem("token", token); + window.dispatchEvent(this.domEvents.tokenchange); } get user() { @@ -52,6 +56,11 @@ class AuthStore { throw err; } }; + + userLogout = () => { + this.token = null; + localStorage.removeItem("token"); + }; } export default AuthStore; diff --git a/src/layouts/menu-layout/MenuLayoutElement.ts b/src/layouts/menu-layout/MenuLayoutElement.ts index 4fdccd4..34e7c7a 100644 --- a/src/layouts/menu-layout/MenuLayoutElement.ts +++ b/src/layouts/menu-layout/MenuLayoutElement.ts @@ -1,30 +1,51 @@ -import { attr, targets, controller, target } from "@github/catalyst"; -import { closest, index, update, isTrue } from "core/utils"; -import { html, render, until } from "@github/jtml"; -import { PingService } from "services/"; +import { controller } from "@github/catalyst"; +import { closest } from "core/utils"; +import { html, render } from "@github/jtml"; import { BaseLayoutElement } from "common/layouts"; +import { AppMainElement } from "components/"; @controller class MenuLayoutElement extends BaseLayoutElement { - @closest appMain; + @closest appMain: AppMainElement; constructor() { super(); } - @update - connectedCallback() {} + connectedCallback() { + this.update(); + window.addEventListener("tokenchange", this.updateAuth); + } + + disconnectedCallback(): void { + window.removeEventListener("tokenchange", this.updateAuth); + } + + get isAuth() { + const _hasToken = this.appMain?.isAuth; + const _hasData = this.appMain?.authStore?.user; + return _hasData && _hasToken; + } + + updateAuth = () => { + this.update(); + }; render() { return html` -