Skip to content

changes to work with SvelteKit v1.0.0-next.278#2

Merged
JannikWempe merged 1 commit intoJannikWempe:masterfrom
krivdat:sveltekit-update
Feb 21, 2022
Merged

changes to work with SvelteKit v1.0.0-next.278#2
JannikWempe merged 1 commit intoJannikWempe:masterfrom
krivdat:sveltekit-update

Conversation

@krivdat
Copy link
Contributor

@krivdat krivdat commented Feb 19, 2022

While following your tutorial on logrocket.com and trying to replicate the app I had to make some adjustments due to breaking changes introduced in sveltejs/kit (sveltejs/kit#3384)

Btw great tutorial, very helpful and exactly what I needed for my app.

@shinokada
Copy link

Great!
When you click sign-out, it returns the following error:

event.headers has been replaced by event.request.headers. See https://github.com/sveltejs/kit/pull/3384 for details
Error: event.headers has been replaced by event.request.headers. See https://github.com/sveltejs/kit/pull/3384 for details
    at Object.get (file:///Users/shinichiokada/Svelte/sveltekit-auth-app/.svelte-kit/runtime/server/index.js:2539:10)
    at get (/Users/shinichiokada/Svelte/sveltekit-auth-app/src/routes/api/sign-out.js:5:26)
    at render_endpoint (file:///Users/shinichiokada/Svelte/sveltekit-auth-app/.svelte-kit/runtime/server/index.js:150:25)
    at resolve (file:///Users/shinichiokada/Svelte/sveltekit-auth-app/.svelte-kit/runtime/server/index.js:2663:17)
    at async respond (file:///Users/shinichiokada/Svelte/sveltekit-auth-app/.svelte-kit/runtime/server/index.js:2571:20)
    at async file:///Users/shinichiokada/Svelte/sveltekit-auth-app/node_modules/@sveltejs/kit/dist/chunks/index.js:245:24

@shinokada
Copy link

Change sign-ou.js to the following:

export async function get ({ request }) {
  const cookie = await request.headers.cookie;
	const cookies = parse(cookie || '');
...

@shinokada
Copy link

Opps. It's my bad. You already had it. Sorry.

@JannikWempe
Copy link
Owner

Thanks for the PR. I'll check it later.

@robots4life
Copy link

robots4life commented Feb 21, 2022

Thanks for the PR. I'll check it later.

When you do, you could also have a look at shadow endpoints, meaning instead of using the load function you can do something like this.

/profile/index.js

export async function get({ locals }) {
	// do your checks here on condition of availability of locals.user
	console.log(locals);

	if (locals.user) {
		return {
			status: 200,
			body: {
				locals
			}
		};
	} else {
		return {
			status: 302,
			headers: { Location: '/' }
		};
	}
}

Here is a simple little demo. https://github.com/robots4life/cookie-auth

@JannikWempe
Copy link
Owner

@robots4life thanks for pointing that out. For now, I'll stick with the load function as it is closer to the initial article and I (currently) find it more readable.

@JannikWempe JannikWempe merged commit 92f4f99 into JannikWempe:master Feb 21, 2022
@krivdat krivdat deleted the sveltekit-update branch October 21, 2025 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants