Next.js Discord

Discord Forum

authorize function not executed

Answered
Cuvier’s Dwarf Caiman posted this in #help-forum
Open in Discord
Cuvier’s Dwarf CaimanOP
I have a callback page where I get redirected to (localhost:3000/callback)
"use client";

import React from "react";
import { signIn } from "next-auth/react";
import { useEffect } from "react";
import { useState } from "react";

function CallbackPage() {
  const [signingIn, setSigningIn] = useState(false);

  useEffect(() => {
    const handleSignIn = async () => {
      try {
        setSigningIn(true);
        const result = await signIn("credentials", {
          redirect: false,
          state: "1",
          code: "123",
        });
        setSigningIn(false);
        console.log({ result });
      } catch (error) {
        console.error(error);
      }
    };

    handleSignIn();
  }, []);

  return <div>CallbackPage</div>;
}

export default CallbackPage;

Using next-auth, I immediately want to log the user in.
When I check the dev console I see logged:
{
    "result": {
        "error": null,
        "status": 200,
        "ok": true,
        "url": "https://nextauth_url/api/auth/signin?csrf=true"
    }
}
Answered by Masai Lion
the try catch block is ... DMed ya take a look over there also did you try using postman for that fetch post request to check if returns the right data ?
View full answer

12 Replies

Cuvier’s Dwarf CaimanOP
I am trying to log in user the credentials provider
const providers = [
  CredentialsProvider({
    name: "Gwap Secret Credentials",
    credentials: {
      state: { label: "State", type: "text" },
      code: { label: "Code", type: "text" },
    },
    async authorize(credentials: any, req) {
      const { state, code } = credentials;
      console.log(credentials);

      try {
        const res = await fetch(
          `${process.env.GWAP_CMS_BASE_URL}/issue-token?state=${state}&code=${code}&client_id=${process.env.GWAP_CLIENT_ID}&redirect_uri=${process.env.GWAP_REDIRECT_URI}&client_secret=${process.env.GWAP_CLIENT_SECRET}`,
          {
            method: "POST",
            body: JSON.stringify({ state, code }),
            headers: {
              "Content-Type": "application/json",
            },
          }
        );

        console.log({ res });
        return {
          id: "1",
          phone: "+597",
        };
      } catch (error) {
        console.log(error);
      }
      return null;
    },
  }),
];


Here is the full authOptions object:
export const authOptions: NextAuthOptions = {
  providers,
  secret: process.env.NEXTAUTH_SECRET,
};


Which gets used in /api/auth/[...nextauth]/route.ts like so:
import { authOptions } from "@/utils/auth-options";
import NextAuth from "next-auth/next";

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };


The problem is, the authorize function is nog getting executed because I don't see the credentials logged in the terminal. When I check the dev tools Network tab, I see a post request to
http://localhost:3000/api/auth/callback/credentials with status 200 OK.
What is going on here?
Cuvier’s Dwarf CaimanOP
I downgraded to next 13.4.2

that did not solve the issue/
Cuvier’s Dwarf CaimanOP
I still need help.
Cuvier’s Dwarf CaimanOP
Still need help 😖
Masai Lion
@Cuvier’s Dwarf Caiman can you put up a simple repo so I can take a look in to the full picture? I have a few guesses 🙂
Cuvier’s Dwarf CaimanOP
okay
Cuvier’s Dwarf CaimanOP
i'm creating the repo ok
Masai Lion
the try catch block is ... DMed ya take a look over there also did you try using postman for that fetch post request to check if returns the right data ?
Answer
Cuvier’s Dwarf CaimanOP
I did
it gave me an error
so didn't go through with that

something about nextauth_url being undefined.