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

Svelte hook keeps redirecting #5687

Closed
JSaretin opened this issue Jul 23, 2022 · 6 comments
Closed

Svelte hook keeps redirecting #5687

JSaretin opened this issue Jul 23, 2022 · 6 comments

Comments

@JSaretin
Copy link

JSaretin commented Jul 23, 2022

Describe the bug

First, great job by the Svelte community, I have been using it for all my projects right from the day, I learned about it.

But I have been dealing with this problem, even in my previous project, I had to rewrite my auth logic at the last moment right before launching.

I don't want to do that with my current project because my previous project was just a single dashboard page, so I could just move the logic to the shadow endpoint of my page.

But this project contains many pages like Setting, Profile, and more.

In my login endpoint, I create and save a new cookie to the user browser, so I can use that to evaluate when they reload or try to visit some routes, in this case when they visit the login page, I check if there is a cookie named access_token and then redirect them to their dashboard, else, proceed with the user request, same logic if they visit the dashboard and the is no access_token

The issue is that everything is working as it should in development, but when I build and upload to netlify it starts acting weird, if you visit the dashboard when there's a cookie, the whole page is stuck. What is think is happing, is that somehow, the hook is not recognizing that there's a cookie, but when it redirects to the login, it recognizes there's a cookie and redirects to the dashboard, and the loop continues.

How to reproduce Error

git clone https://github.com/jsaretin/svelte-hook-auth-error
cd svelte-auth-error
npm install

npm run build
npm run preview

then login and visit the user dashboard

Reproduction

I uploaded a sample here

Logs

No response

System Info

System:
    OS: Linux 5.15 Ubuntu 20.04.4 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
    Memory: 2.05 GB / 7.46 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.15.0 - /usr/local/bin/node
    npm: 8.5.5 - /usr/local/bin/npm
  Browsers:
    Brave Browser: 103.1.41.100
    Chrome: 103.0.5060.134
    Firefox: 102.0
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.63 
    @sveltejs/kit: next => 1.0.0-next.392 
    svelte: ^3.44.0 => 3.49.0 
    vite: ^3.0.0 => 3.0.2

Severity

annoyance

Additional Information

No response

@Mlocik97
Copy link
Contributor

you hit this:

https://twitter.com/mlocik97/status/1528105175449231363

it's related to prerendering, but it's not actually issue in Kit...

@JSaretin
Copy link
Author

Thanks, for the reply, I didn't know where to post it, I have checked the referenced tweet, and I tried setting the prerender in my svelte template, yet the outcome is still the same.

@Mlocik97
Copy link
Contributor

if you do redirect before resolve call, it doesn't even use that prerender, in that case you need to either move your redirect logic to load function of pages, or set prerender in config.

@mustofa-id
Copy link

I can workaround this by skipping auth logic on build process. Here's example. Idk if this is correct, but it's work.

@Rich-Harris
Copy link
Member

Older versions of SvelteKit prerendered redirects even if the page itself wasn't marked as prerenderable. This is now fixed, so I'll close this issue.

@xmlking
Copy link

xmlking commented Dec 26, 2022

When using with AuthJs , it still causes problem nextauthjs/next-auth#6186
this workaround fixed it

import { building } from '$app/environment';

if (building) return resolve(event);

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

No branches or pull requests

5 participants