Skip to content

Commit 5df8049

Browse files
Allow setting _osmo_session cookies for local -> prod development (#548)
* Allow setting _osmo_session cookies for local -> prod development * Format
1 parent c0069bc commit 5df8049

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

src/ui/src/lib/dev/dev-auth.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
/**
1818
* Dev Auth Helpers
1919
*
20-
* Console utilities for managing the _osmo_session cookie in local development.
20+
* Console utilities for managing _osmo_session cookies in local development.
2121
* Auth is handled by Envoy + OAuth2 Proxy in production. For local dev against
22-
* a real backend, copy the encrypted session cookie from production.
22+
* a real backend, copy the encrypted session cookies from Chrome DevTools
23+
* (Application > Cookies) since they're HttpOnly and not accessible via JS.
24+
*
25+
* The session is split across chunked cookies (e.g. _osmo_session_0,
26+
* _osmo_session_1) when the encrypted payload exceeds the 4KB cookie limit.
2327
*
2428
* Console API:
25-
* devAuth.status() - Check if session cookie is present
26-
* devAuth.clear() - Clear session cookies
27-
* devAuth.help() - Show setup instructions
29+
* devAuth.set(name, value) - Set a session cookie by name and value
30+
* devAuth.status() - Check if session cookies are present
31+
* devAuth.clear() - Clear all session cookies
32+
* devAuth.help() - Show setup instructions
2833
*/
2934

3035
export function hasSessionCookie(): boolean {
@@ -42,19 +47,39 @@ export function clearSessionCookies(): void {
4247
console.log("Session cookies cleared");
4348
}
4449

50+
export function setSessionCookie(name: string, value: string): void {
51+
if (!name || !value) {
52+
console.error("Usage: devAuth.set('_osmo_session_0', 'value')");
53+
return;
54+
}
55+
document.cookie = `${name}=${value};path=/;max-age=604800`;
56+
console.log(`%c${name} set successfully.`, "color: #22c55e; font-weight: bold;");
57+
}
58+
4559
export function printHelp(): void {
4660
console.log("%c Local Dev Auth", "color: #3b82f6; font-weight: bold; font-size: 14px;");
4761
console.log("");
4862
console.log("%cTo authenticate local dev against production:", "font-weight: bold;");
4963
console.log("");
50-
console.log("%c1. Open production app in browser console and run:", "color: #64748b;");
64+
console.log("%c1. Open the production app in Chrome DevTools → Application → Cookies", "color: #64748b;");
65+
console.log(
66+
"%c2. Find the %c_osmo_session_*%c cookies and copy each name + value",
67+
"color: #64748b;",
68+
"color: #22d3ee; font-family: monospace;",
69+
"color: #64748b;",
70+
);
71+
console.log(
72+
"%c (they're HttpOnly, so they won't appear via document.cookie)",
73+
"color: #94a3b8; font-style: italic;",
74+
);
75+
console.log("%c3. Come back here and run for each cookie:", "color: #64748b;");
5176
console.log("");
5277
console.log(
53-
`%c${COPY_COOKIES_SNIPPET}`,
78+
"%cdevAuth.set('_osmo_session_0', 'value_from_devtools')\ndevAuth.set('_osmo_session_1', 'value_from_devtools')",
5479
"background: #1e293b; color: #22d3ee; padding: 8px; border-radius: 4px; font-family: monospace;",
5580
);
5681
console.log("");
57-
console.log("%c2. Come back here and paste the result into this console.", "color: #64748b;");
82+
console.log("%c4. Reload the page.", "color: #64748b;");
5883
console.log("");
5984
console.log(
6085
"%cAlternatively, use mock mode: %cpnpm dev:mock",
@@ -63,19 +88,18 @@ export function printHelp(): void {
6388
);
6489
}
6590

66-
const COPY_COOKIES_SNIPPET = `copy(document.cookie.split(";").filter(c=>c.trim().startsWith("_osmo_session")).map(c=>{const[k,v]=c.trim().split("=");return\`document.cookie="\${k}=\${v};path=/;max-age=604800";\`}).join("\\n"))`;
67-
6891
export function printStatus(): void {
6992
const hasSession = hasSessionCookie();
7093
console.log(`Session cookie present: ${hasSession}`);
7194
if (!hasSession) {
72-
console.log("No _osmo_session cookie. Run devAuth.help() for setup instructions.");
95+
console.log("No _osmo_session cookies found. Run devAuth.help() for setup instructions.");
7396
}
7497
}
7598

7699
declare global {
77100
interface Window {
78101
devAuth?: {
102+
set: typeof setSessionCookie;
79103
status: typeof printStatus;
80104
clear: typeof clearSessionCookies;
81105
help: typeof printHelp;
@@ -87,6 +111,7 @@ export function initDevAuth(showInstructions: boolean): void {
87111
if (typeof window === "undefined") return;
88112

89113
window.devAuth = {
114+
set: setSessionCookie,
90115
status: printStatus,
91116
clear: clearSessionCookies,
92117
help: printHelp,

0 commit comments

Comments
 (0)