Skip to content

Commit fcfac39

Browse files
committed
If no Ory session, fall back to cookie
1 parent 2ca8516 commit fcfac39

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/components/PlaceholderReplacer.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { useEffect } from "react";
22
import { useOrySession } from "@/lib/OrySessionContext";
3+
import { getCookie } from "@/lib/utils";
34

45
export function PlaceholderReplacer() {
56
const { email, loading } = useOrySession();
67

78
useEffect(() => {
8-
// Determine the replacement value
9+
// Determine the replacement value with fallback priority:
10+
// 1. If loading, keep original placeholder
11+
// 2. If Ory email is available, use that (highest priority)
12+
// 3. If no Ory email but cookie exists, use cookie value
13+
// 4. Otherwise, use original placeholder
14+
const getCookieEmail = () => getCookie("last_arcadedev_account_email");
15+
916
const replacement = loading
1017
? "{arcade_user_id}" // Keep original while loading
11-
: email || "{arcade_user_id}";
18+
: email || getCookieEmail() || "{arcade_user_id}";
1219

1320
// Function to replace text in a text node
1421
const replaceInTextNode = (node: Text) => {

src/lib/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,17 @@ import { twMerge } from "tailwind-merge";
44
export function cn(...inputs: ClassValue[]) {
55
return twMerge(clsx(inputs));
66
}
7+
8+
/**
9+
* Read a cookie value by name from document.cookie
10+
* @param name - The name of the cookie to read
11+
* @returns The cookie value or null if not found
12+
*/
13+
export function getCookie(name: string): string | null {
14+
const value = `; ${document.cookie}`;
15+
const parts = value.split(`; ${name}=`);
16+
if (parts.length === 2) {
17+
return parts.pop()?.split(";").shift() || null;
18+
}
19+
return null;
20+
}

0 commit comments

Comments
 (0)