atproto icon indicating copy to clipboard operation
atproto copied to clipboard

I'm getting errors after manipulating the API: "The session was deleted by another process"

Open pirmax opened this issue 10 months ago • 1 comments

Hello ATP community,

I've been using NodeJS ATProto's SDK for a few months, users can log in via OAuth but the session seems to be deleted by the session storage implemented, and this for some reason.

So, I get a lot of errors from the worker where a feature is implemented to retrieve profile information.

Error: The session was deleted by another process

The SessionStore and StateStore :

import type {
  NodeSavedSession,
  NodeSavedSessionStore,
  NodeSavedState,
  NodeSavedStateStore,
} from '@atproto/oauth-client-node'
import { PrismaClient } from '@prisma/client'

export class StateStore implements NodeSavedStateStore {
  constructor(private prisma: PrismaClient) {}

  async get(key: string): Promise<NodeSavedState | undefined> {
    const authState = await this.prisma.authState.findFirst({
      where: {
        key,
      },
    })

    if (!authState) {
      return undefined
    }

    return JSON.parse(authState.state) as NodeSavedState
  }

  async set(key: string, val: NodeSavedState) {
    const state = JSON.stringify(val)

    await this.prisma.authState.upsert({
      where: {
        key,
      },
      update: {
        state,
      },
      create: {
        key,
        state,
      },
    })
  }

  async del(key: string) {
    await this.prisma.authState.delete({
      where: {
        key,
      },
    })
  }
}

export class SessionStore implements NodeSavedSessionStore {
  constructor(private prisma: PrismaClient) {}

  async get(key: string): Promise<NodeSavedSession | undefined> {
    const authSession = await this.prisma.authSession.findFirst({
      where: {
        key,
      },
    })

    if (!authSession) {
      return undefined
    }

    return JSON.parse(authSession.session) as NodeSavedSession
  }

  async set(key: string, val: NodeSavedSession) {
    const session = JSON.stringify(val)

    await this.prisma.authSession.upsert({
      where: {
        key,
      },
      update: {
        session,
      },
      create: {
        key,
        session,
      },
    })
  }

  async del(key: string) {
    await this.prisma.authSession.delete({
      where: {
        key,
      },
    })
  }
}

I can of course comment on session deletion, but I don't think this is the right way to do it. I'm missing something and not knowing Bluesky's OAuth well enough, I'm having trouble :)

Thank you for enlightening me.

pirmax avatar Dec 10 '24 13:12 pirmax