Skip to content

Commit

Permalink
fix(bluesky): use local map for session storing
Browse files Browse the repository at this point in the history
  • Loading branch information
noook committed Feb 5, 2025
1 parent 9b25a6d commit 57a367c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
1 change: 0 additions & 1 deletion playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const providers = computed(() =>
{
label: user.value?.bluesky || 'Bluesky',
click() {
console.log('hello')
const handle = prompt('Enter your Bluesky handle')
if (handle) {
navigateTo({
Expand Down
25 changes: 11 additions & 14 deletions src/runtime/server/lib/atproto/bluesky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function defineOAuthBlueskyEventHandler({ config, onSuccess, onError }: O
const clientMetadata = getAtprotoClientMetadata(event, 'bluesky', config)
const scopes = clientMetadata.scope?.split(' ') ?? []

const sessionStore = new SessionStore(event)
const sessionStore = new SessionStore()
const stateStore = new StateStore(event)

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

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

sessionStore.del()
sessionStore.del(session.did)

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

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

constructor(private event: H3Event) {}

Expand All @@ -131,21 +131,18 @@ export class StateStore implements NodeSavedStateStore {
}

export class SessionStore implements NodeSavedSessionStore {
private readonly sessionKey = 'oauth:bluesky:session'
private store: Record<string, NodeSavedSession> = {}

constructor(private event: H3Event) {}

async get(): Promise<NodeSavedSession | undefined> {
const result = getCookie(this.event, this.sessionKey)
if (!result) return
return JSON.parse(atob(result))
async get(key: string): Promise<NodeSavedSession | undefined> {
return this.store[key]
}

async set(key: string, val: NodeSavedSession) {
setCookie(this.event, this.sessionKey, btoa(JSON.stringify(val)))
this.store[key] = val
}

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

0 comments on commit 57a367c

Please sign in to comment.