-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remember the last visited community (#912)
* Remember the last visited community * Set cookie client side Co-authored-by: Thomas F. K. Jorna <[email protected]> * Remove now unused server action * Update logout in test to account for new menu --------- Co-authored-by: Thomas F. K. Jorna <[email protected]>
- Loading branch information
Showing
8 changed files
with
74 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
core/app/components/LastVisitedCommunity/SetLastVisited.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use client"; | ||
|
||
import { useEffect } from "react"; | ||
|
||
import { LAST_VISITED_COOKIE, LAST_VISITED_COOKIE_MAX_AGE } from "./constants"; | ||
|
||
export default function SetLastVisited({ communitySlug }: { communitySlug: string }) { | ||
useEffect(() => { | ||
document.cookie = `${LAST_VISITED_COOKIE}=${communitySlug}; path=/; max-age=${LAST_VISITED_COOKIE_MAX_AGE}`; | ||
}, [communitySlug]); | ||
|
||
return <></>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const LAST_VISITED_COOKIE = "lastVisitedCommunitySlug"; | ||
export const LAST_VISITED_COOKIE_MAX_AGE = 60 * 60 * 24 * 30; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import { cookies } from "next/headers"; | ||
import { redirect } from "next/navigation"; | ||
|
||
import { LAST_VISITED_COOKIE } from "~/app/components/LastVisitedCommunity/constants"; | ||
import { getPageLoginData } from "~/lib/authentication/loginData"; | ||
|
||
export default async function Page() { | ||
const { user } = await getPageLoginData(); | ||
|
||
// if user and no commuhnmitiy, redirect to settings | ||
if (!user) { | ||
redirect("/login"); | ||
} | ||
|
||
const memberSlug = user.memberships[0]?.community?.slug; | ||
const cookieStore = await cookies(); | ||
const lastVisited = cookieStore.get(LAST_VISITED_COOKIE); | ||
const communitySlug = lastVisited?.value ?? user.memberships[0]?.community?.slug; | ||
|
||
if (!memberSlug) { | ||
if (!communitySlug) { | ||
redirect("/settings"); | ||
} | ||
|
||
redirect(`/c/${memberSlug}/stages`); | ||
redirect(`/c/${communitySlug}/stages`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import { expect, test } from "@playwright/test"; | ||
|
||
import { LoginPage } from "./fixtures/login-page"; | ||
import { inbucketClient, retryAction } from "./helpers"; | ||
import * as loginFlows from "./login.flows"; | ||
import { inbucketClient } from "./helpers"; | ||
|
||
test.describe("general auth", () => { | ||
test("Login with invalid credentials", async ({ page }) => { | ||
|
@@ -110,3 +109,26 @@ test.describe("Auth with lucia", () => { | |
await page.waitForURL(/\/c\/\w+\/stages/); | ||
}); | ||
}); | ||
|
||
test("Last visited community is remembered", async ({ page }) => { | ||
const unjournalRegex = new RegExp("/c/unjournal/"); | ||
const croccrocRegex = new RegExp("/c/croccroc/"); | ||
const loginPage = new LoginPage(page); | ||
|
||
// Login and visit default community | ||
await loginPage.goto(); | ||
await loginPage.loginAndWaitForNavigation("[email protected]", "pubpub-all"); | ||
await expect(page).toHaveURL(unjournalRegex); | ||
|
||
// Switch communities and logout | ||
await page.getByRole("button", { name: "Select a community" }).click(); | ||
await page.getByRole("menuitem", { name: "CrocCroc" }).click(); | ||
await page.waitForURL(croccrocRegex); | ||
await page.getByRole("button", { name: "User menu" }).click(); | ||
await page.getByRole("button", { name: "Logout" }).click(); | ||
|
||
// Log back in and switched community should be remembered | ||
await page.waitForURL("/login"); | ||
await loginPage.loginAndWaitForNavigation("[email protected]", "pubpub-all"); | ||
await expect(page).toHaveURL(croccrocRegex); | ||
}); |