Skip to content

Commit

Permalink
fix: add the getSession server side fn
Browse files Browse the repository at this point in the history
  • Loading branch information
OrJDev committed Mar 16, 2024
1 parent 370577b commit fc60847
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/smooth-poets-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solid-mediakit/auth': patch
---

fix: add the getSession server side fn
28 changes: 27 additions & 1 deletion packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ import { parse } from 'set-cookie-parser'

import { createActionURL, raw, skipCSRFCheck } from '@auth/core'
import { sendRedirect } from 'vinxi/http'
import { setEnvDefaults } from './utils'
import { getBasePath, setEnvDefaults } from './utils'
import { Session } from '@auth/core/types'

type SignInParams = Parameters<App.Locals['signIn']>

Expand Down Expand Up @@ -244,3 +245,28 @@ export async function auth(
if (status === 200) return data
throw new Error(data.message)
}

export type GetSessionResult = Promise<Session | null>

export async function getSession(
req: Request,
options: SolidAuthConfig
): GetSessionResult {
options.secret ??= process.env.AUTH_SECRET
options.trustHost ??= true

const basePath = getBasePath()
const url = new URL(`${basePath}/session`, req.url)
const response = await Auth(
new Request(url, { headers: req.headers }),
options
)

const { status = 200 } = response

const data = await response.json()

if (!data || !Object.keys(data).length) return null
if (status === 200) return data
throw new Error(data.message)
}

0 comments on commit fc60847

Please sign in to comment.