From fc60847b25edb3b93bf6d4c264e744507581fb1f Mon Sep 17 00:00:00 2001 From: OrJDev Date: Sun, 17 Mar 2024 00:46:33 +0200 Subject: [PATCH] fix: add the getSession server side fn --- .changeset/smooth-poets-add.md | 5 +++++ packages/auth/src/index.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .changeset/smooth-poets-add.md diff --git a/.changeset/smooth-poets-add.md b/.changeset/smooth-poets-add.md new file mode 100644 index 0000000..d1f95f2 --- /dev/null +++ b/.changeset/smooth-poets-add.md @@ -0,0 +1,5 @@ +--- +'@solid-mediakit/auth': patch +--- + +fix: add the getSession server side fn diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.ts index 6679e4d..3ff46c7 100644 --- a/packages/auth/src/index.ts +++ b/packages/auth/src/index.ts @@ -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 @@ -244,3 +245,28 @@ export async function auth( if (status === 200) return data throw new Error(data.message) } + +export type GetSessionResult = Promise + +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) +}