|
1 | 1 | import type { NodeSavedSession, NodeSavedSessionStore } from '@atproto/oauth-client-node' |
2 | | -import type { H3Event } from 'h3' |
3 | | - |
4 | | -/** |
5 | | - * Storage key prefix for oauth session storage. |
6 | | - */ |
7 | | -export const OAUTH_SESSION_CACHE_STORAGE_BASE = 'oauth-atproto-session' |
| 2 | +import type { UserServerSession } from '#shared/types/userSession' |
| 3 | +import type { SessionManager } from 'h3' |
8 | 4 |
|
9 | 5 | export class OAuthSessionStore implements NodeSavedSessionStore { |
10 | | - // TODO: not sure if we will support multi accounts, but if we do in the future will need to change this around |
11 | | - private readonly cookieKey = 'oauth:atproto:session' |
12 | | - private readonly storage = useStorage(OAUTH_SESSION_CACHE_STORAGE_BASE) |
| 6 | + private readonly session: SessionManager<UserServerSession> |
13 | 7 |
|
14 | | - constructor(private event: H3Event) {} |
| 8 | + constructor(session: SessionManager<UserServerSession>) { |
| 9 | + this.session = session |
| 10 | + } |
15 | 11 |
|
16 | 12 | async get(): Promise<NodeSavedSession | undefined> { |
17 | | - const sessionKey = getCookie(this.event, this.cookieKey) |
18 | | - if (!sessionKey) return |
19 | | - const result = await this.storage.getItem<NodeSavedSession>(sessionKey) |
20 | | - if (!result) return |
21 | | - return result |
| 13 | + const sessionData = this.session.data |
| 14 | + if (!sessionData) return undefined |
| 15 | + return sessionData.oauthSession |
22 | 16 | } |
23 | 17 |
|
24 | | - async set(key: string, val: NodeSavedSession) { |
25 | | - setCookie(this.event, this.cookieKey, key, { |
26 | | - httpOnly: true, |
27 | | - secure: !import.meta.dev, |
28 | | - sameSite: 'lax', |
| 18 | + async set(_key: string, val: NodeSavedSession) { |
| 19 | + // We are ignoring the key since the mapping is already done in the session |
| 20 | + await this.session.update({ |
| 21 | + oauthSession: val, |
29 | 22 | }) |
30 | | - await this.storage.setItem<NodeSavedSession>(key, val) |
31 | 23 | } |
32 | 24 |
|
33 | 25 | async del() { |
34 | | - const sessionKey = getCookie(this.event, this.cookieKey) |
35 | | - if (sessionKey) { |
36 | | - await this.storage.del(sessionKey) |
37 | | - } |
38 | | - deleteCookie(this.event, this.cookieKey) |
| 26 | + await this.session.update({ |
| 27 | + oauthSession: undefined, |
| 28 | + }) |
39 | 29 | } |
40 | 30 | } |
0 commit comments