Skip to content

Commit 0a19142

Browse files
committed
[TOOL-3120] Redirect to last visited team page if authenticated (#6028)
1 parent feb4fd0 commit 0a19142

File tree

6 files changed

+54
-28
lines changed

6 files changed

+54
-28
lines changed

apps/dashboard/src/app/team/[team_slug]/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getTeamBySlug } from "@/api/team";
22
import { AppFooter } from "@/components/blocks/app-footer";
33
import { redirect } from "next/navigation";
44
import { TWAutoConnect } from "../../components/autoconnect";
5+
import { SaveLastVisitedTeamPage } from "../components/last-visited-page/SaveLastVisitedPage";
56

67
export default async function RootTeamLayout(props: {
78
children: React.ReactNode;
@@ -19,6 +20,7 @@ export default async function RootTeamLayout(props: {
1920
<div className="flex grow flex-col">{props.children}</div>
2021
<TWAutoConnect />
2122
<AppFooter />
23+
<SaveLastVisitedTeamPage />
2224
</div>
2325
);
2426
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use client";
2+
3+
import { usePathname } from "next/navigation";
4+
import { useEffect } from "react";
5+
import { setCookie } from "../../../../lib/cookie";
6+
import { LAST_VISITED_TEAM_PAGE_PATH } from "./consts";
7+
8+
export function SaveLastVisitedTeamPage() {
9+
const pathname = usePathname();
10+
11+
// eslint-disable-next-line no-restricted-syntax
12+
useEffect(() => {
13+
setCookie(LAST_VISITED_TEAM_PAGE_PATH, pathname);
14+
}, [pathname]);
15+
16+
return null;
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Only keep constants here - this file is imported in middleware too
2+
3+
export const LAST_VISITED_TEAM_PAGE_PATH = "last-visited-team-page-path";

apps/dashboard/src/lib/cookie.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use client";
2+
3+
export function getCookie(name: string) {
4+
try {
5+
const value = document.cookie;
6+
const cookies = value.split(";");
7+
8+
for (const c of cookies) {
9+
const [_name, _val] = c.trim().split("=");
10+
if (_name === name && _val) {
11+
return decodeURIComponent(_val);
12+
}
13+
}
14+
} catch {
15+
// ignore
16+
}
17+
18+
return undefined;
19+
}
20+
21+
export function setCookie(name: string, value: string, days = 365) {
22+
const date = new Date();
23+
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
24+
document.cookie = `${name}=${value};path=/;expires=${date.toUTCString()}`;
25+
}

apps/dashboard/src/middleware.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { type NextRequest, NextResponse } from "next/server";
55
import { getAddress } from "thirdweb";
66
import { getChainMetadata } from "thirdweb/chains";
77
import { isValidENSName } from "thirdweb/utils";
8+
import { LAST_VISITED_TEAM_PAGE_PATH } from "./app/team/components/last-visited-page/consts";
89
import { defineDashboardChain } from "./lib/defineDashboardChain";
910

1011
// ignore assets, api - only intercept page routes
@@ -54,7 +55,6 @@ export async function middleware(request: NextRequest) {
5455
: null;
5556

5657
// utm collection
57-
// NOTE: this is not working for pages with rewrites in next.config.js - (framer pages)
5858
// if user is already signed in - don't bother capturing utm params
5959
if (!authCookie) {
6060
const searchParamsEntries = request.nextUrl.searchParams.entries();
@@ -96,13 +96,15 @@ export async function middleware(request: NextRequest) {
9696
}
9797
}
9898

99-
// remove '/' in front and then split by '/'
100-
10199
// if it's the homepage and we have an auth cookie, redirect to the dashboard
102100
if (paths.length === 1 && paths[0] === "" && authCookie) {
101+
const lastVisitedTeamPagePath = request.cookies.get(
102+
LAST_VISITED_TEAM_PAGE_PATH,
103+
)?.value;
104+
103105
return redirect(
104106
request,
105-
"/team",
107+
lastVisitedTeamPagePath || "/team",
106108
cookiesToSet
107109
? {
108110
cookiesToSet,

apps/dashboard/src/stores/SyncStoreToCookies.tsx

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type Store, useStore } from "@/lib/reactive";
22
import { useEffect } from "react";
3+
import { getCookie, setCookie } from "../lib/cookie";
34

45
const loadedStoreKeys = new Set<string>();
56

@@ -51,27 +52,3 @@ export function SyncStoreToCookies<T>(props: {
5152

5253
return null;
5354
}
54-
55-
function getCookie(name: string) {
56-
try {
57-
const value = document.cookie;
58-
const cookies = value.split(";");
59-
60-
for (const c of cookies) {
61-
const [_name, _val] = c.trim().split("=");
62-
if (_name === name && _val) {
63-
return decodeURIComponent(_val);
64-
}
65-
}
66-
} catch {
67-
// ignore
68-
}
69-
70-
return undefined;
71-
}
72-
73-
function setCookie(name: string, value: string, days = 365) {
74-
const date = new Date();
75-
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
76-
document.cookie = `${name}=${value};path=/;expires=${date.toUTCString()}`;
77-
}

0 commit comments

Comments
 (0)