nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.96k stars 3.52k forks source link

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

Closed std4453 closed 7 months ago

std4453 commented 1 year ago

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:

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.

tmackness commented 1 year ago

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 commented 1 year ago

Same issue here. Have you found any workaroud?

tmackness commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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

stunaz commented 1 year ago

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 commented 7 months ago

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 https://github.com/nextauthjs/next-auth/pull/10667.

ndom91 commented 7 months ago

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 commented 7 months ago

@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;
    },
  },```