diff --git a/frontend/src/index.test.ts b/frontend/src/index.test.ts index b7bd3ce9..953a09a4 100644 --- a/frontend/src/index.test.ts +++ b/frontend/src/index.test.ts @@ -54,12 +54,6 @@ describe("browsertrix-app", () => { expect(el).lightDom.descendants("btrix-home"); }); - it("renders when `AuthService.initSessionStorage` rejects", async () => { - stub(AuthService, "initSessionStorage").returns(Promise.reject()); - const el = await fixture(""); - expect(el).lightDom.descendants("btrix-log-in"); - }); - // TODO move tests to AuthService it("sets auth state from session storage", async () => { stub(AuthService.prototype, "startFreshnessCheck").callsFake(() => {}); @@ -86,7 +80,7 @@ describe("browsertrix-app", () => { Promise.resolve(mockAPIUser), ); stub(AuthService.prototype, "startFreshnessCheck").callsFake(() => {}); - stub(AuthService, "initSessionStorage").callsFake(() => + stub(AuthService, "initSessionStorage").callsFake(async () => Promise.resolve({ headers: { Authorization: "_fake_headers_" }, tokenExpiresAt: 0, @@ -105,7 +99,7 @@ describe("browsertrix-app", () => { Promise.resolve(mockAPIUser), ); stub(AuthService.prototype, "startFreshnessCheck").callsFake(() => {}); - stub(AuthService, "initSessionStorage").callsFake(() => + stub(AuthService, "initSessionStorage").callsFake(async () => Promise.resolve({ headers: { Authorization: "_fake_headers_" }, tokenExpiresAt: 0, @@ -134,7 +128,7 @@ describe("browsertrix-app", () => { }), ); stub(AuthService.prototype, "startFreshnessCheck").callsFake(() => {}); - stub(AuthService, "initSessionStorage").callsFake(() => + stub(AuthService, "initSessionStorage").callsFake(async () => Promise.resolve({ headers: { Authorization: "_fake_headers_" }, tokenExpiresAt: 0, diff --git a/frontend/src/index.ts b/frontend/src/index.ts index 56320bb6..6a4e6c74 100644 --- a/frontend/src/index.ts +++ b/frontend/src/index.ts @@ -82,6 +82,9 @@ export class App extends LiteElement { this.syncViewState(); if (authState) { this.authService.saveLogin(authState); + } + this.syncViewState(); + if (authState) { void this.updateUserInfo(); } super.connectedCallback(); @@ -124,7 +127,7 @@ export class App extends LiteElement { (pathname === "/log-in" || pathname === "/reset-password") ) { // Redirect to logged in home page - this.viewState = this.router.match(ROUTES.home); + this.viewState = this.router.match(this.orgBasePath); window.history.replaceState(this.viewState, "", this.viewState.pathname); } else { this.viewState = this.router.match( @@ -225,7 +228,7 @@ export class App extends LiteElement { if (newViewPath === "/log-in" && this.authService.authState) { // Redirect to logged in home page - this.viewState = this.router.match(ROUTES.home); + this.viewState = this.router.match(this.orgBasePath); } else { this.viewState = this.router.match(newViewPath); } @@ -815,7 +818,7 @@ export class App extends LiteElement { }); if (!detail.api) { - this.navigate(detail.redirectUrl || ROUTES.home); + this.navigate(detail.redirectUrl || this.orgBasePath); } if (detail.firstLogin) { @@ -833,11 +836,13 @@ export class App extends LiteElement { this.navigate(ROUTES.login, { redirectUrl, }); - this.notify({ - message: msg("Please log in to continue."), - variant: "warning", - icon: "exclamation-triangle", - }); + if (redirectUrl && redirectUrl !== "/") { + this.notify({ + message: msg("Please log in to continue."), + variant: "warning", + icon: "exclamation-triangle", + }); + } }; onNavigateTo = (event: CustomEvent) => { diff --git a/frontend/src/pages/home.ts b/frontend/src/pages/admin.ts similarity index 99% rename from frontend/src/pages/home.ts rename to frontend/src/pages/admin.ts index 0c39530e..7beb9d99 100644 --- a/frontend/src/pages/home.ts +++ b/frontend/src/pages/admin.ts @@ -4,6 +4,7 @@ import { serialize } from "@shoelace-style/shoelace/dist/utilities/form.js"; import { type PropertyValues } from "lit"; import { customElement, state } from "lit/decorators.js"; +import needLogin from "@/decorators/needLogin"; import type { InviteSuccessDetail } from "@/features/accounts/invite-form"; import type { APIUser } from "@/index"; import type { APIPaginatedList } from "@/types/api"; @@ -26,6 +27,7 @@ import { formatAPIUser } from "@/utils/user"; */ @localized() @customElement("btrix-home") +@needLogin export class Home extends LiteElement { @state() private orgList?: OrgData[]; @@ -52,6 +54,7 @@ export class Home extends LiteElement { private readonly validateOrgNameMax = maxLengthValidator(40); connectedCallback() { + console.log("saldfhjkass"); if (this.authState) { if (this.slug) { this.navTo(`/orgs/${this.slug}`); diff --git a/frontend/src/pages/index.ts b/frontend/src/pages/index.ts index 46f9d04a..bc4f6380 100644 --- a/frontend/src/pages/index.ts +++ b/frontend/src/pages/index.ts @@ -1,5 +1,4 @@ -import "./home"; - +import(/* webpackChunkName: "admin" */ "./admin"); import(/* webpackChunkName: "sign-up" */ "./sign-up"); import(/* webpackChunkName: "log-in" */ "./log-in"); import(/* webpackChunkName: "org" */ "./org"); diff --git a/frontend/src/pages/invite/accept.ts b/frontend/src/pages/invite/accept.ts index f651b86d..aee71bc0 100644 --- a/frontend/src/pages/invite/accept.ts +++ b/frontend/src/pages/invite/accept.ts @@ -8,7 +8,6 @@ import { renderInviteMessage } from "./ui/inviteMessage"; import { BtrixElement } from "@/classes/BtrixElement"; import type { APIUser } from "@/index"; import type { OrgUpdatedDetail } from "@/pages/invite/ui/org-form"; -import { ROUTES } from "@/routes"; import type { UserOrg, UserOrgInviteInfo } from "@/types/user"; import { isApiError } from "@/utils/api"; import { AppStateService } from "@/utils/state"; @@ -149,7 +148,7 @@ export class AcceptInvite extends BtrixElement { ? nothing : html` diff --git a/frontend/src/pages/invite/join.ts b/frontend/src/pages/invite/join.ts index e12511de..c254162a 100644 --- a/frontend/src/pages/invite/join.ts +++ b/frontend/src/pages/invite/join.ts @@ -6,7 +6,6 @@ import { renderInviteMessage } from "./ui/inviteMessage"; import type { SignUpSuccessDetail } from "@/features/accounts/sign-up-form"; import type { OrgUpdatedDetail } from "@/pages/invite/ui/org-form"; -import { ROUTES } from "@/routes"; import type { UserOrg, UserOrgInviteInfo } from "@/types/user"; import AuthService, { type LoggedInEventDetail } from "@/utils/AuthService"; import LiteElement, { html } from "@/utils/LiteElement"; @@ -97,7 +96,7 @@ export class Join extends LiteElement { html`
${err instanceof Error ? err.message : err}
@@ -174,7 +173,7 @@ export class Join extends LiteElement { if (inviteInfo?.orgSlug) { this.navTo(`/orgs/${inviteInfo.orgSlug}`); } else { - this.navTo(ROUTES.home); + this.navTo(this.orgBasePath); } } } diff --git a/frontend/src/pages/log-in.ts b/frontend/src/pages/log-in.ts index 1e8011e0..8602ac02 100644 --- a/frontend/src/pages/log-in.ts +++ b/frontend/src/pages/log-in.ts @@ -4,7 +4,6 @@ import { assign, createMachine, interpret } from "@xstate/fsm"; import { type PropertyValues } from "lit"; import { customElement, property, state } from "lit/decorators.js"; -import { ROUTES } from "@/routes"; import { isApiError } from "@/utils/api"; import type { ViewState } from "@/utils/APIRouter"; import AuthService from "@/utils/AuthService"; @@ -145,7 +144,7 @@ export class LogInPage extends LiteElement { viewState!: ViewState; @property({ type: String }) - redirectUrl: string = ROUTES.home; + redirectUrl?: string; private readonly formStateService = interpret(machine); diff --git a/frontend/src/utils/APIRouter.ts b/frontend/src/utils/APIRouter.ts index b49e5a57..24828650 100644 --- a/frontend/src/utils/APIRouter.ts +++ b/frontend/src/utils/APIRouter.ts @@ -5,7 +5,7 @@ type Routes = { [key: string]: UrlPattern }; type Paths = { [key: string]: string }; export type ViewState = { - // route name, e.g. "home" + // route name, e.g. "admin" route: string | null; // path name // e.g. "/dashboard" diff --git a/frontend/src/utils/AuthService.ts b/frontend/src/utils/AuthService.ts index 2c14ad57..28465c4d 100644 --- a/frontend/src/utils/AuthService.ts +++ b/frontend/src/utils/AuthService.ts @@ -347,7 +347,7 @@ export default class AuthService { this.logout(); const { pathname, search, hash } = window.location; const redirectUrl = - pathname !== ROUTES.login && pathname !== ROUTES.home + pathname !== ROUTES.login && pathname !== "/" ? `${pathname}${search}${hash}` : ""; window.dispatchEvent(AuthService.createNeedLoginEvent({ redirectUrl }));