How to update session in a page load function? #6163
Unanswered
ponderingexistence
asked this question in
Q&A
Replies: 2 comments 3 replies
-
Use // +layout.ts
export const load: Load = async ({ session, fetch, depends }) {
await fetch('...verify-purchase');
depends('some-unique-string');
return await getSessionInfo();
};
// +page.ts
import { invalidate } from '$app/navigation';
import { browser } from '$app/env';
export const load: PageLoad = async ({ fetch }) {
await fetch('...verify-purchase');
if (browser) { // you don't need the surrounding if-clause if this page is not SSRd (SPA mode)
invalidate('some-unique-string');
}
}; It might also make sense to put the |
Beta Was this translation helpful? Give feedback.
0 replies
-
@dummdidumm It doesn't seem to work actually, the session isn't updated. This is what I'm doing:
export const load: LayoutLoad = async ({ depends }) => {
depends('session');
return {
session: await getSessionInfo()
};
};
export const load: PageLoad = async ({ fetch }) => {
await fetch('...verify-purchase');
if (browser) {
invalidate('session');
}
return { ... };
}; Am I doing anything wrong? Thanks. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
This is probably more of a question.
I have a
/purchase
page which users get redirected to after a payment transaction is complete. Inside the load function of that page, I'm sending a request to my API to verify the purchase, and I then need to subsequently update thesession
(which includes information about the purchased items). Beforesession
was removed this is what my page's load function looked like:routes/purchase/+page.ts
:Now that there's no session anymore, I have had to move what previously resided in the
getSession
hook over to my root layout'sload
function. But I don't know how I'm supposed to update it in mypurchase
page's load function, I'm doing the following:routes/purchase/+page.ts
:This appeared to work at first, but then I found out that as soon as the user navigates away from the purchase page (e.g. clicks on the home page link to go to the home page), the session info reverts back to its previous state, and the updated information that the
purchase
page returned is basically ignored. This wasn't the case with the built-insession
in the past.How do I solve this problem?
Beta Was this translation helpful? Give feedback.
All reactions