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

Conversation

ndom91
Copy link
Member

@ndom91 ndom91 commented Dec 29, 2023

☕️ Reasoning

PRed from work done by @benjaminknox (see: #8034 (comment))

When getSession() is called it returns only the body from the backend and the header to set the authentication cookie is lost. Ben was able to create a workaround by creating another function (getSessionWithSetCookies) to return the set-cookie headers and then set them myself in +layout.server.ts

Usage in +layout.server.ts

// src/routes/+layout.server.ts
import type { LayoutServerLoad } from "./$types"

export const load: LayoutServerLoad = async (event) => {
  const sessionWithCookies = await event.locals.getSessionWithSetCookies();

  for(const cookieName in sessionWithCookies?.cookies) {
    const { value, options } = sessionWithCookies.cookies[cookieName]
    event.cookies.set(cookieName, value, options)
  }

  return {
    session: sessionWithCookies?.session
  }
}

Todo

  • Having issues with the cookies types still 🤔

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

Fixes: #8034

📌 Resources

Copy link

vercel bot commented Dec 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 16, 2024 4:54pm
2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
auth-docs-nextra ⬜️ Ignored (Inspect) Visit Preview Jan 16, 2024 4:54pm
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Jan 16, 2024 4:54pm

if (status === 200) return data
if (status === 200) return {
session: data,
cookies: parseCookies(Array.from(response.headers).filter(([headerName]) => headerName === 'set-cookie'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use the getSetCookie here? 😁

Copy link
Member Author

@ndom91 ndom91 Jan 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good call, modified that a bit. Also added cookie and @types/cookie as peer deps to @auth/svelte since their dependencies in @auth/core already anyway and that'll always be installed together. Does that make sense? Or should I just add them as straight-up dependencies for @auth/sveltekit too?

packages/frameworks-sveltekit/src/lib/index.ts Outdated Show resolved Hide resolved
@ndom91
Copy link
Member Author

ndom91 commented Jan 1, 2024

Hmm so the build step is failing for the @auth/drizzle-adapter apparently, but I can't find any similar code issues in my locally checked out copy of htis branch and the build script runs for me locally without issue as well 🤔

image

Is it working for yuo locally?

Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the added work for the user.

A better API might be to wrap load with getSession instead (maybe rename it to auth() to align with next-auth too) so it can set the cookies after the user logic has run, merging the results.

This is similar to what we do here:

// Preserve cookies from the session response
for (const cookie of sessionResponse.headers.getSetCookie())
finalResponse.headers.append("set-cookie", cookie)
return finalResponse

For the user, ideally, the +layout.server.ts file could just be export const load = auth(). 🤔

@ndom91
Copy link
Member Author

ndom91 commented Jan 3, 2024

Hmm yeah i can test that, i have a sveltekit project with auth.js currently WIP.

But the wrapping load strategy feels a bit un-sveltekit like, based on the little experience i have at least. I'd be curious what a more seasoned svelte person would say about this 🤔

@balazsorban44
Copy link
Member

balazsorban44 commented Jan 5, 2024

Let's merge with a unstable_ prefix then. 👍 but IMO it needs to go at the end. it should "just work"

(build needs to pass first)

@ndom91
Copy link
Member Author

ndom91 commented Jan 5, 2024

Let's merge with a unstable_ prefix then. 👍 but IMO it needs to go at the end. it should "just work"

(build needs to pass first)

Okay so we had amended the pre-existing getSession method, you're saying add it as a new method then? So we have getSession() and unstable_getSessionWithCookies() or something like that?

@stalkerg
Copy link

Just in case, I don't think "updated from client to server" is a real issue. A decorator can be better.
btw composition can be more flexible, decorators (wrapper) usually is less flexible. Current getSession should be keep as is I believe.

@ndom91
Copy link
Member Author

ndom91 commented Jan 20, 2024

Heads up anyone following this PR - feature's been merged via #9694 🙏

@aakash14goplani
Copy link

@ndom91 - can you please update documentation to specific how to use new method and how it is different then existing get-session method. Thanks!

@ndom91
Copy link
Member Author

ndom91 commented Jan 21, 2024

@aakash14goplani Yeah we're working on a new version of the docs and it'll definitely be in there.

For now, the old method is still available, although marked deprecated. The usage of the auth() method is the same as the getSession one, the difference is that it sets the cookies from the auth response before returning the session body, keeping expiry updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Are token cookies updated when getting the session in @auth/sveltekit?
6 participants