Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sveltekit): add getSessionWithSetCookies function for carrying over cookies and enabling token refresh in sveltekit #9497

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3491462
feat: add session handler method for carrying over cookies
ndom91 Dec 29, 2023
e25bf5a
fix: cleanup Cookie type
ndom91 Dec 29, 2023
16b8163
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Dec 30, 2023
d2ff406
fix: use getSetCookie in sveltekit useSession
ndom91 Jan 1, 2024
e5f4fd5
fix: rename getSessionWithSetCookie to default getSession
ndom91 Jan 1, 2024
96dbca8
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Jan 1, 2024
c2743b7
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Jan 2, 2024
fae2892
fix: move cookie deps from peerDeps to deps
ndom91 Jan 3, 2024
2d3f48b
fix: use "cookie" built-in parse method instead of custom parseCookie…
ndom91 Jan 3, 2024
deb0f5f
Merge branch 'ndom91/sveltekit-session-with-cookies-for-refresh-token…
ndom91 Jan 3, 2024
b74330a
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Jan 4, 2024
5d1f725
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Jan 5, 2024
0731280
fix: add unstable_ additional method and fix type errors
ndom91 Jan 5, 2024
5f97358
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Jan 5, 2024
2db07b5
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Jan 6, 2024
b8aa51f
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Jan 7, 2024
2f5c8dd
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Jan 9, 2024
c16e67b
Merge branch 'main' into ndom91/sveltekit-session-with-cookies-for-re…
ndom91 Jan 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/frameworks-sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
"vitest": "^1.0.0"
},
"dependencies": {
"@auth/core": "workspace:*"
"@auth/core": "workspace:*",
"cookie": "0.6.0",
"@types/cookie": "0.6.0"
},
"peerDependencies": {
"svelte": "^3.54.0 || ^4.0.0",
Expand Down
22 changes: 18 additions & 4 deletions packages/frameworks-sveltekit/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ import { base } from "$app/paths"
import { env } from "$env/dynamic/private"

import { Auth } from "@auth/core"
import { parse } from "cookie"
import type { AuthAction, AuthConfig, Session } from "@auth/core/types"

export type {
Expand All @@ -215,10 +216,10 @@ export type {
User,
} from "@auth/core/types"

export async function getSession(
export async function unstable_getSessionWithCookies(
req: Request,
config: SvelteKitAuthConfig
): ReturnType<App.Locals["getSession"]> {
): ReturnType<App.Locals["unstable_getSessionWithCookies"]> {
config.secret ??= env.AUTH_SECRET
config.trustHost ??= true

Expand All @@ -231,10 +232,22 @@ export async function getSession(
const data = await response.json()

if (!data || !Object.keys(data).length) return null
if (status === 200) return data
if (status === 200) return {
session: data,
cookies: parse(response.headers.get('set-cookie') as string)
}
throw new Error(data.message)
}

export async function getSession(
req: Request,
config: SvelteKitAuthConfig
): ReturnType<App.Locals["getSession"]> {
const data = await unstable_getSessionWithCookies(req, config)

return data?.session ?? null
}

/** Configure the {@link SvelteKitAuth} method. */
export interface SvelteKitAuthConfig extends Omit<AuthConfig, "raw"> {
/**
Expand Down Expand Up @@ -265,7 +278,7 @@ type DynamicSvelteKitAuthConfig = (
function AuthHandle(
svelteKitAuthOptions: SvelteKitAuthConfig | DynamicSvelteKitAuthConfig
): Handle {
return async function ({ event, resolve }) {
return async function({ event, resolve }) {
const authOptions =
typeof svelteKitAuthOptions === "object"
? svelteKitAuthOptions
Expand Down Expand Up @@ -306,6 +319,7 @@ declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace App {
interface Locals {
unstable_getSessionWithCookies(): Promise<{ session: Session, cookies: Record<string, string> } | null>
getSession(): Promise<Session | null>
}
interface PageData {
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading