getSession

API for the getSession function The getSession function is a server side function that will be used to fetch the current active session from a web Request. It takes 2 arguments:

  • request: The web request to fetch the session from
  • options: The auth config we created in the previous step (API Route)

Usage

Basic

import { getSession } from '@solid-mediakit/auth'
import { authOpts } from '~/routes/api/auth/[...solidauth]'

async function getMySession(request: Request) {
  return await getSession(request, authOpts)
} // Promise<Session | null>

Actual

import { getSession } from '@solid-mediakit/auth'
import { cache, createAsync, redirect } from '@solidjs/router'
import { authOptions } from './server/auth'
import { getRequestEvent } from 'solid-js/web'

const getUser = cache(async () => {
  'use server'
  const event = getRequestEvent()!
  const session = await getSession(event, authOptions)
  if (!session) {
    throw redirect('/')
  }
  return session
}, 'user')

const user = createAsync(() => getUser())

This is being used to fetch the session from the server side, if you want to fetch it client-side, check out the createSession function.