-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathcookieAuthStorageAdapter.ts
52 lines (40 loc) · 1.42 KB
/
cookieAuthStorageAdapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { GoTrueClientOptions, Session } from '@supabase/supabase-js';
import { DEFAULT_COOKIE_OPTIONS, parseSupabaseCookie, stringifySupabaseSession } from './utils';
import { CookieOptions } from './types';
export interface StorageAdapter extends Exclude<GoTrueClientOptions['storage'], undefined> {}
export abstract class CookieAuthStorageAdapter implements StorageAdapter {
protected readonly cookieOptions: CookieOptions;
constructor(cookieOptions?: CookieOptions) {
this.cookieOptions = {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions
};
}
protected abstract getCookie(
name: string
): string | undefined | null | Promise<string | undefined | null>;
protected abstract setCookie(name: string, value: string): void;
protected abstract deleteCookie(name: string): void;
async getItem(key: string): Promise<string | null> {
const value = await this.getCookie(key);
if (!value) return null;
// pkce code verifier
if (key.endsWith('-code-verifier')) {
return value;
}
return JSON.stringify(parseSupabaseCookie(value));
}
setItem(key: string, value: string): void | Promise<void> {
// pkce code verifier
if (key.endsWith('-code-verifier')) {
this.setCookie(key, value);
return;
}
let session: Session = JSON.parse(value);
const sessionStr = stringifySupabaseSession(session);
this.setCookie(key, sessionStr);
}
removeItem(key: string): void | Promise<void> {
this.deleteCookie(key);
}
}