AdonisJS API
    Preparing search index...

    Class SessionCollection

    SessionCollection provides APIs for programmatic session management. It allows reading, destroying, and tagging sessions without an HTTP context.

    import app from '@adonisjs/core/services/app'
    import { SessionCollection } from '@adonisjs/session'

    const sessionCollection = await app.container.make(SessionCollection)

    // List all sessions for a user
    const sessions = await sessionCollection.tagged(String(user.id))

    // Destroy a specific session
    await sessionCollection.destroy(sessionId)
    Index
    • Checks if the current store supports session tagging. Only Memory, Redis, and Database stores support tagging.

      Returns boolean

      const collection = await app.container.make(SessionCollection)
      if (collection.supportsTagging()) {
      await collection.tag(sessionId, userId)
      }
    • Returns the session data for the given session ID, or null if the session does not exist

      Parameters

      • sessionId: string

        Session identifier

      Returns Promise<SessionData | null>

      const data = await sessionCollection.get('sess_abc123')
      
    • Destroys a session by its ID

      Parameters

      • sessionId: string

        Session identifier

      Returns Promise<void>

      await sessionCollection.destroy('sess_abc123')
      
    • Tag a session with a user ID. Only supported by Memory, Redis and Database stores.

      Parameters

      • sessionId: string

        Session identifier

      • userId: string

        User identifier to tag the session with

      Returns Promise<void>

      await sessionCollection.tag('sess_abc123', 'user_456')
      
    • Get all sessions for a given user ID (tag). Only supported by Memory, Redis and Database stores.

      Parameters

      • userId: string

        User identifier to get sessions for

      Returns Promise<TaggedSession[]>

      const sessions = await sessionCollection.tagged('user_456')