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

Redirects to signin page using Next.js v13.4.12, adapter and session strategy: database #8161

Closed
std4453 opened this issue Jul 28, 2023 · 10 comments
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@std4453
Copy link

std4453 commented Jul 28, 2023

Environment

System:
OS: Linux 6.4 Arch Linux
CPU: (16) x64 12th Gen Intel(R) Core(TM) i5-12600KF
Memory: 6.50 GB / 62.66 GB
Container: Yes
Shell: 3.6.1 - /usr/bin/fish
Binaries:
Node: 16.17.1 - ~/.local/share/nvm/v16.17.1/bin/node
Yarn: 1.22.19 - ~/.local/share/nvm/v16.17.1/bin/yarn
npm: 8.15.0 - ~/.local/share/nvm/v16.17.1/bin/npm
pnpm: 6.7.1 - ~/.local/share/nvm/v16.17.1/bin/pnpm

Reproduction URL

https://github.com/std4453/next-auth-bug/

Describe the issue

next-auth does not work when:

  • next.js v13.4.12 (other versions not tested)
  • Using app router
  • Using middlewares
  • Using adapters
  • Using database as session strategy

When entering /, it jumps to /api/auth/signin, you login, it jumps back to /api/auth/callback/keycloak, then to /, then to /api/auth/signin again.

As a result, you never get to use the app.

It works when using jwt as session strategy.

I'm not sure whether this is a duplicate of #5008 , that one claims to be fixed in Next.js v12.2.5-canary.2 or newer, yet there has since been more feedback about it not working, and it might be worth to take another look.

How to reproduce

See README .

Expected behavior

It should redirect to the OAuth provider, then back to the app, and you should be able to use it.

@std4453 std4453 added the triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. label Jul 28, 2023
@tmackness
Copy link

I am having the same issue as you. I did notice in your code you didn't have SessionProvider context though. Not sure if you missed that in the example only.

Otherwise have you found a solution.

@vladimir-voinea
Copy link

Same issue here. Have you found any workaroud?

@tmackness
Copy link

Same issue here. Have you found any workaroud?

I'm still trying to figure out the issue with limited time. Yet to try previous next Auth version.

Does appear this has been an issue in the past too.

To clarify I am using App directory and middleware. I think it works without the middleware from memory.

@std4453
Copy link
Author

std4453 commented Aug 5, 2023

@tmackness I think this all happened on server-side, the route simply redirects with 307 to signin page without any client-side js being executed, and <SessionProvider> is on the client side, so it shouldn't matter here.

@DennisKo
Copy link

DennisKo commented Sep 5, 2023

Isn't that a documented limitation? From https://next-auth.js.org/configuration/nextjs#caveats:

Only supports the "jwt" session strategy. We need to wait until databases at the Edge become mature enough to ensure a fast experience. (If you know of an Edge-compatible database, we would like if you proposed a new Adapter)

@sky4git
Copy link

sky4git commented Sep 14, 2023

I have the same issue with using DynamoDB adapter. Next auth middleware is always redirect to sign in page with session: 'database' strategy.

@stunaz
Copy link

stunaz commented Sep 15, 2023

damn its been driving me crazy.... just went and look at the middleware code, it just support jwt .... that's really unfortunate, so I wonder how people uses it than? what's the workaround?

@JasperAlexander
Copy link
Contributor

damn its been driving me crazy.... just went and look at the middleware code, it just support jwt .... that's really unfortunate, so I wonder how people uses it than? what's the workaround?

There is no workaround. The middleware only supports jwt sessions. I added this prerequisite to the docs with #10667.

@ndom91
Copy link
Member

ndom91 commented Apr 21, 2024

The reason that caveat was added back in the old docs is no longer accurate - many database adapters are "edge compatible", meaning you can use the database adapters / database sessions with middleware now.

See:

That of course doesn't mean all database adapters will work, you have to double check that your database runs in an edge runtime, aka not using node.js.

I'm going to close this issue as we've seemingly figured out the main issue and there is newer info available ^^

@stunaz
Copy link

stunaz commented Apr 21, 2024

@ndom91 , sorry to ask it here, where would you augment user data session, since we dont have a token which hold additional information. is this pattern ok ?

    session: async ({ session, user }) => {
      const result = await db.query.users.findFirst({
        columns: { firstName: true, lastName: true },
        where: (users, { eq }) => eq(users.id, user.id),
      });

      session.user.id = user.id;
      session.user.firstName = result?.firstName;
      session.user.lastName = result?.lastName;

      return session;
    },
  },```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

8 participants