Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"homepage": "https://github.com/supabase/ssr#readme",
"devDependencies": {
"@eslint/js": "^9.3.0",
"@supabase/supabase-js": "^2.43.4",
"@supabase/supabase-js": "^2.56.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be bumped further once this is live: supabase/supabase-js#1545

"@vitest/coverage-v8": "^1.6.0",
"eslint": "^8.57.0",
"prettier": "^3.2.5",
Expand Down
7 changes: 7 additions & 0 deletions src/createBrowserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@
detectSessionInUrl: isBrowser(),
persistSession: true,
storage,
...(options?.cookies &&
"encode" in options.cookies &&
options.cookies.encode === "tokens-only"
? {
userStorage: options?.auth?.userStorage ?? window.localStorage,

Check failure on line 148 in src/createBrowserClient.ts

View workflow job for this annotation

GitHub Actions / Build and test

Property 'userStorage' does not exist on type '{ autoRefreshToken?: boolean | undefined; storageKey?: string | undefined; persistSession?: boolean | undefined; detectSessionInUrl?: boolean | undefined; storage?: SupportedStorage | undefined; flowType?: AuthFlowType | undefined; debug?: boolean | ... 1 more ... | undefined; lock?: LockFunc | undefined; }'.
}
: null),
},
});

Expand Down
9 changes: 9 additions & 0 deletions src/createServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CookieMethodsServer,
CookieMethodsServerDeprecated,
} from "./types";
import { memoryLocalStorageAdapter } from "./utils/helpers";

/**
* @deprecated Please specify `getAll` and `setAll` cookie methods instead of
Expand Down Expand Up @@ -170,6 +171,14 @@
detectSessionInUrl: false,
persistSession: true,
storage,
...(options?.cookies &&
"encode" in options.cookies &&
options.cookies.encode === "tokens-only"
? {
userStorage:
options?.auth?.userStorage ?? memoryLocalStorageAdapter(),

Check failure on line 179 in src/createServerClient.ts

View workflow job for this annotation

GitHub Actions / Build and test

Property 'userStorage' does not exist on type '{ autoRefreshToken?: boolean | undefined; storageKey?: string | undefined; persistSession?: boolean | undefined; detectSessionInUrl?: boolean | undefined; storage?: SupportedStorage | undefined; flowType?: AuthFlowType | undefined; debug?: boolean | ... 1 more ... | undefined; lock?: LockFunc | undefined; }'.
}
: null),
},
});

Expand Down
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export type CookieMethodsBrowserDeprecated = {
};

export type CookieMethodsBrowser = {
/**
* If set to true, only the user's session (access and refresh tokens) will be encoded in cookies. The user object will be encoded in local storage if the `userStorage` option is not provided when creating the client.
*
* You should keep this option the same between `createBrowserClient()` and `createServerClient()`. When set to `tokens-only` accessing the `user` property on the data returned from `getSession()` will only be possible if the user has already been stored in the separate storage. It's best to use `getClaims()` instead to avoid surprizes.
*
* @expermental
*/
encode?: "user-and-tokens" | "tokens-only";

getAll: GetAllCookies;
setAll: SetAllCookies;
};
Expand All @@ -44,6 +53,15 @@ export type CookieMethodsServerDeprecated = {
};

export type CookieMethodsServer = {
/**
* If set to `tokens-only`, only the user's access and refresh tokens will be encoded in cookies. The user object will be encoded in memory if the `userStorage` option is not provided when creating the client. Unset value defaults to `user-and-tokens`.
*
* You should keep this option the same between `createBrowserClient()` and `createServerClient()`. When set to `tokens-only` accessing the `user` property on the data returned from `getSession()` will not be possible. Use `getUser()` or preferably `getClaims()` instead.
*
* @experimental
*/
encode?: "user-and-tokens" | "tokens-only";

getAll: GetAllCookies;
setAll?: SetAllCookies;
};
22 changes: 22 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,25 @@ export function isBrowser() {
typeof window !== "undefined" && typeof window.document !== "undefined"
);
}

/**
* Returns a localStorage-like object that stores the key-value pairs in
* memory.
*/
export function memoryLocalStorageAdapter(
store: { [key: string]: string } = {},
) {
return {
getItem: (key: string) => {
return store[key] || null;
},

setItem: (key: string, value: string) => {
store[key] = value;
},

removeItem: (key: string) => {
delete store[key];
},
};
}
Loading