Kayote
Kayote

Reputation: 15647

Next-Auth - Custom OAuth 2.0 Provider Integration

We have our own OAuth 2.0 implementation and I am using next-auth to connect and get a jwt in my Next.js app.

Output: At the moment, I am able to login. However, I get null as token on the client-side.

Steps I have implemented:

    function MyApp({ Component, pageProps }) {
      return (
        <Provider
          options={{
            clientMaxAge: 0,
            keepAlive: 0,
          }}
          session={pageProps.session}>
          <Component {...pageProps} />
        </Provider>
      );
    }
export default NextAuth({
  providers: [
    {
      id: "quot-test",
      name: "quot-test",
      type: "oauth",
      version: "2.0",
      scope: "openid email",
      state: true,
      protection: "state",
      params: { grant_type: "authorization_code" },
      idToken: true,
      authorizationUrl:
        "http://localhost:9000/oauth2/authorize?response_type=code",
      accessTokenUrl: "http://localhost:9000/oauth2/token",
      requestTokenUrl: "http://localhost:9000/oauth2/jwks",
      clientId: process.env.QUOT_ID,
      clientSecret: process.env.QUOT_SECRET,
    },
  ],
  secret: process.env.SECRET,
  debug: true,
  session: {
    jwt: true,
    maxAge: 60 * 5,
  },
  jwt: {
    secret: process.env.SECRET,
    encryption: false,
  },
  callbacks: {
    async signin(user, account, profile) {
      console.log("user", user, account, profile);
      return true;
    },
    async jwt(token, user, account, profile, isNewUser) {
      console.log(token);
      console.log(user);
      console.log(account);
      console.log(profile);
      console.log(isNewUser);
      if (account.accessToken) {
        token.accessToken = account.accessToken;
      }
      return Promise.resolve(token);
    },
    async session(session, token) {
      console.log(session);
      console.log(token);
      return session;
    },
  },
});

This is where the first issue is: none of the callbacks are triggered and no console.log is printed on the terminal. There appears to be something wrong in my config above?

import { signIn, signOut, useSession, getSession } from "next-auth/client";

export default function Home(){
  ...
  const [session, loading]  = useSession();
  console.log(session); // prints null after signin
  return(
  <button onClick={() =>signIn(null, { callbackUrl: "http://localhost:3000/happy" })}>
    Sign 
  </button>
  ...

This is where the next issue is: even though the callbackUrl in signin function is to the page '/happy', I am still taken to home page!

NEXTAUTH_URL=http://localhost:3000/

QUOTECH_ID=quot-test
QUOTECH_SECRET=[secret-from-authenticator-goes-here]
SECRET=[random-string-goes-here]

What am I missing ? Why am I getting null once I am redirected by the client-app. It appears some of the options are being ignored in the [...nextauth].js file above?

Upvotes: 3

Views: 11205

Answers (1)

tunaayberk
tunaayberk

Reputation: 466

Profile values are required on custom provider, if you update them that should work.

{
  ...,
  profileUrl: string,
  profile: function,
  ...
}

On documentation you can see those areas are required fields.

profileUrl should be a profile link to get profile info and profile is a function you manipulate this profile url returns to be fit in next-auth profile modal or your custom profile modal.

example;

{
 id: "quot-test",
 name: "quot-test",
 type: "oauth",
 version: "2.0",
 scope: "openid email",
 state: true,
 protection: "state",
 params: { grant_type: "authorization_code" },
 idToken: true,
 authorizationUrl: "http://localhost:9000/oauth2/authorize?response_type=code",
 accessTokenUrl: "http://localhost:9000/oauth2/token",
 requestTokenUrl: "http://localhost:9000/oauth2/jwks",
 profileUrl: "[ProfileURL]",
 profile(profile, tokens) {
  return {
          id: profile.id,
          name: profile.name,
          email: profile.email
         };
 },
 clientId: process.env.QUOT_ID,
 clientSecret: process.env.QUOT_SECRET,
}

Upvotes: 0

Related Questions