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

In authorized callback auth object is null all the time #9097

Closed
amit548 opened this issue Nov 9, 2023 · 7 comments
Closed

In authorized callback auth object is null all the time #9097

amit548 opened this issue Nov 9, 2023 · 7 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@amit548
Copy link

amit548 commented Nov 9, 2023

Environment

System:
OS: Windows 11 10.0.22631
CPU: (12) x64 AMD Ryzen 5 5500U with Radeon Graphics
Memory: 5.76 GB / 15.38 GB
Binaries:
Node: 20.9.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
pnpm: 8.9.2 - ~\AppData\Roaming\npm\pnpm.CMD
Browsers:
Edge: Chromium (119.0.2151.44)
Internet Explorer: 11.0.22621.1

npmPackages:
next: ^14.0.1 => 14.0.1
next-auth: ^5.0.0-beta.3 => 5.0.0-beta.3
react: 18.2.0 => 18.2.0

Reproduction URL

https://github.com/nextauthjs/next-auth-example/blob/main/auth.ts

Describe the issue

image
image

After login with credentials, in the authorized callback I can't access auth object, all time auth object is null but when I check cookies the session token is present

How to reproduce

use credentials provider

Expected behavior

the auth object should not be empty or null after login

@amit548 amit548 added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Nov 9, 2023
@apshenichniy
Copy link

I encountered the same problem. Adding AUTH_URL to the environment variables solved it.

@amit548
Copy link
Author

amit548 commented Nov 10, 2023

I encountered the same problem. Adding AUTH_URL to the environment variables solved it.

Thank you, it works.

@amit548 amit548 closed this as completed Nov 10, 2023
@travelerr
Copy link

Confirmed also works for me. Was encountering this with my localhost:3000 while auth worked as expected in prod vercel site. Vercel must automatically set an AUTH_URL env variable?

Working example: AUTH_URL="http://localhost:3000"

@vinayavodah
Copy link

Thanks @apshenichniy!

@leohxj
Copy link
Contributor

leohxj commented Dec 12, 2023

Confirmed also works for me. Was encountering this with my localhost:3000 while auth worked as expected in prod vercel site. Vercel must automatically set an AUTH_URL env variable?

add AUTH_URL="http://localhost:3000" in .env file, but always got null.

@Vibrat
Copy link

Vibrat commented Dec 31, 2023

I experienced the same issue. In my case it's due to same-site origin policy that prevents exchanging cookies which leads to null on session = await auth(). The solution is changing to fetch local API. I'm not sure what causes this and would love to hear an answer from everyone.

// This will work
return await fetch(
        `/api/payments`,
        {
          method: "POST",
          body: JSON.stringify(data),
        }
);

// This will not work as expected
return await fetch(
        `http://127.0.0.1:3000/api/payments`,
        {
          method: "POST",
          body: JSON.stringify(data),
        }
);

@iamdevlinph
Copy link

Adding AUTH_URL="http://localhost:3000" didn't help for me. It's still returning null.

Image

// auth.ts
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";

export const { handlers, signIn, signOut, auth } = NextAuth({
  providers: [Google],
  pages: {
    signIn: "/",
    error: "/",
  },
  callbacks: {
    authorized: async ({ auth }) => {
      console.log("🍉 ~ authorized: ~ auth:", auth);
      console.log(
        "🍉 ~ authorized: ~ process.env.AUTH_URL:",
        process.env.AUTH_URL,
      );
      // return Promise.resolve(!!auth);
      return {};
    },
    // redirect: () => {
    //   return "/admin";
    // },
  },
});

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

No branches or pull requests

7 participants