|
1 | | -import type { |
2 | | - NodeSavedSession, |
3 | | - NodeSavedSessionStore, |
4 | | - NodeSavedState, |
5 | | - NodeSavedStateStore, |
6 | | -} from '@atproto/oauth-client-node' |
7 | 1 | import type { H3Event } from 'h3' |
8 | | - |
9 | | -/** |
10 | | - * Storage key prefix for oauth state storage. |
11 | | - */ |
12 | | -export const OAUTH_STATE_CACHE_STORAGE_BASE = 'oauth-atproto-state' |
13 | | - |
14 | | -export class OAuthStateStore implements NodeSavedStateStore { |
15 | | - private readonly cookieKey = 'oauth:atproto:state' |
16 | | - private readonly storage = useStorage(OAUTH_STATE_CACHE_STORAGE_BASE) |
17 | | - |
18 | | - constructor(private event: H3Event) {} |
19 | | - |
20 | | - async get(): Promise<NodeSavedState | undefined> { |
21 | | - const stateKey = getCookie(this.event, this.cookieKey) |
22 | | - if (!stateKey) return |
23 | | - const result = await this.storage.getItem<NodeSavedState>(stateKey) |
24 | | - if (!result) return |
25 | | - return result |
26 | | - } |
27 | | - |
28 | | - async set(key: string, val: NodeSavedState) { |
29 | | - setCookie(this.event, this.cookieKey, key, { |
30 | | - httpOnly: true, |
31 | | - secure: !import.meta.dev, |
32 | | - sameSite: 'lax', |
33 | | - }) |
34 | | - await this.storage.setItem<NodeSavedState>(key, val) |
35 | | - } |
36 | | - |
37 | | - async del() { |
38 | | - const stateKey = getCookie(this.event, this.cookieKey) |
39 | | - deleteCookie(this.event, this.cookieKey) |
40 | | - if (stateKey) { |
41 | | - await this.storage.del(stateKey) |
42 | | - } |
43 | | - } |
44 | | -} |
45 | | - |
46 | | -/** |
47 | | - * Storage key prefix for oauth session storage. |
48 | | - */ |
49 | | -export const OAUTH_SESSION_CACHE_STORAGE_BASE = 'oauth-atproto-session' |
50 | | - |
51 | | -export class OAuthSessionStore implements NodeSavedSessionStore { |
52 | | - // TODO: not sure if we will support multi accounts, but if we do in the future will need to change this around |
53 | | - private readonly cookieKey = 'oauth:atproto:session' |
54 | | - private readonly storage = useStorage(OAUTH_SESSION_CACHE_STORAGE_BASE) |
55 | | - |
56 | | - constructor(private event: H3Event) {} |
57 | | - |
58 | | - async get(): Promise<NodeSavedSession | undefined> { |
59 | | - const sessionKey = getCookie(this.event, this.cookieKey) |
60 | | - if (!sessionKey) return |
61 | | - const result = await this.storage.getItem<NodeSavedSession>(sessionKey) |
62 | | - if (!result) return |
63 | | - return result |
64 | | - } |
65 | | - |
66 | | - async set(key: string, val: NodeSavedSession) { |
67 | | - setCookie(this.event, this.cookieKey, key, { |
68 | | - httpOnly: true, |
69 | | - secure: !import.meta.dev, |
70 | | - sameSite: 'lax', |
71 | | - }) |
72 | | - await this.storage.setItem<NodeSavedSession>(key, val) |
73 | | - } |
74 | | - |
75 | | - async del() { |
76 | | - const sessionKey = getCookie(this.event, this.cookieKey) |
77 | | - if (sessionKey) { |
78 | | - await this.storage.del(sessionKey) |
79 | | - } |
80 | | - deleteCookie(this.event, this.cookieKey) |
81 | | - } |
82 | | -} |
| 2 | +import { OAuthStateStore } from './oauth-state-store' |
| 3 | +import { OAuthSessionStore } from './oauth-session-store' |
83 | 4 |
|
84 | 5 | export const useOAuthStorage = (event: H3Event) => { |
85 | 6 | return { |
|
0 commit comments