Skip to content

Commit d8534ef

Browse files
noookatinux
andauthored
fix(bluesky): use local map for session storing (#340)
Co-authored-by: Sébastien Chopin <[email protected]>
1 parent 43d7d11 commit d8534ef

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

playground/app.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ const providers = computed(() =>
223223
prefetch: false,
224224
external: true,
225225
to: inPopup.value ? '#' : p.to,
226-
click: inPopup.value ? () => openInPopup(p.to) : void 0,
226+
click: inPopup.value ? () => openInPopup(p.to) : p.click,
227227
})),
228228
)
229229
</script>

src/runtime/server/lib/atproto/bluesky.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function defineOAuthBlueskyEventHandler({ config, onSuccess, onError }: O
3636
const clientMetadata = getAtprotoClientMetadata(event, 'bluesky', config)
3737
const scopes = clientMetadata.scope?.split(' ') ?? []
3838

39-
const sessionStore = new SessionStore(event)
39+
const sessionStore = new SessionStore()
4040
const stateStore = new StateStore(event)
4141

4242
const client = new NodeOAuthClient({
@@ -86,12 +86,12 @@ export function defineOAuthBlueskyEventHandler({ config, onSuccess, onError }: O
8686

8787
try {
8888
const { session } = await client.callback(new URLSearchParams(query as Record<string, string>))
89-
const sessionInfo = await sessionStore.get()
89+
const sessionInfo = await sessionStore.get(session.did)
9090
const profile = scopes.includes('transition:generic')
9191
? (await new Agent(session).getProfile({ actor: session.did })).data
9292
: null
9393

94-
sessionStore.del()
94+
sessionStore.del(session.did)
9595

9696
return onSuccess(event, {
9797
user: profile ?? { did: session.did },
@@ -111,7 +111,7 @@ export function defineOAuthBlueskyEventHandler({ config, onSuccess, onError }: O
111111
}
112112

113113
export class StateStore implements NodeSavedStateStore {
114-
private readonly stateKey = 'oauth:bluesky:stat'
114+
private readonly stateKey = 'oauth-bluesky-state'
115115

116116
constructor(private event: H3Event) {}
117117

@@ -122,7 +122,12 @@ export class StateStore implements NodeSavedStateStore {
122122
}
123123

124124
async set(key: string, val: NodeSavedState) {
125-
setCookie(this.event, this.stateKey, btoa(JSON.stringify(val)))
125+
setCookie(this.event, this.stateKey, btoa(JSON.stringify(val)), {
126+
path: '/',
127+
httpOnly: true,
128+
secure: true,
129+
sameSite: 'lax',
130+
})
126131
}
127132

128133
async del() {
@@ -131,21 +136,18 @@ export class StateStore implements NodeSavedStateStore {
131136
}
132137

133138
export class SessionStore implements NodeSavedSessionStore {
134-
private readonly sessionKey = 'oauth:bluesky:session'
135-
136-
constructor(private event: H3Event) {}
139+
private store: Record<string, NodeSavedSession> = {}
137140

138-
async get(): Promise<NodeSavedSession | undefined> {
139-
const result = getCookie(this.event, this.sessionKey)
140-
if (!result) return
141-
return JSON.parse(atob(result))
141+
async get(key: string): Promise<NodeSavedSession | undefined> {
142+
return this.store[key]
142143
}
143144

144145
async set(key: string, val: NodeSavedSession) {
145-
setCookie(this.event, this.sessionKey, btoa(JSON.stringify(val)))
146+
this.store[key] = val
146147
}
147148

148-
async del() {
149-
deleteCookie(this.event, this.sessionKey)
149+
async del(key: string) {
150+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
151+
delete this.store[key]
150152
}
151153
}

0 commit comments

Comments
 (0)