'Sveltekit newly created cookie is not showing in the hook's getSession() function

Summary

Hello everyone, as in this topic, I'm experimenting issues with my newly created cookie. I'm implementing Oauth2 to authenticate with Discord account in my SvelteKit project. The flow is as follows:

  1. User clicks on index.svelte hyperlink which redirect to /api/auth endpoint in my app.
  2. The /api/auth endpoint is managed by auth.ts which makes a request to Discord Authentification Endpoint using my ClientID, RedirectURI (/api/callback) and Scope identify
  3. Discord redirects to my callback endpoint /api/callback with a code set in the url, that a retrive using url.searchParams.get('code')
  4. Then my callback function redirect the user to my main index.svelte file by setting a cookie containing the code sent by discord, I coded it to create the cookie no matter if the code is correctly retrived or no so I would know if the problem was here
  5. In my index.svelte file, I call the getSession in hooks.ts to retrieve the cookie value and console log it.

The attended behavior is that after my callback redirection, the cookie should appear on the main page and it's value printed to my console. Which is not the case. Instead I have to refresh my index.svelte page to get the cookie.

The link code to access /api/auth endpoint from index.svelte:

<a rel="external" href="/api/auth">
    Connexion
</a>

All my redirections are set with a status code of 302.

Reproduction

You can find all my files at pastebin.com by following this link.

Anyone got an idea ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source