Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
Authentication with axios throwing internal server error in Next JS

Getting 500 internal server error for my next authentication.

I implemented everything according to the docs from Next Auth.

import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import axios from "axios";
import { API_URL } from "./../../../helpers/api/mutations";

const options = {
  providers: [
    CredentialsProvider({
      name: "Credentials",

API works fine in a separate react component but fails only on Next Auth.

      async authorize(credentials) {
        const user = axios({
          url: API_URL,
          method: "post",
          data: {
            query: `mutation($mobileNumber:String,$mobileCountryCode:String,$otp:Int){
              verifyPhoneOTP(
            mobileNumber: $mobileNumber,
             mobileCountryCode: $mobileCountryCode,
                otp:$otp
              )
              {accessToken
              expiresAt}
            }`,
            variables: {
              mobileNumber: credentials.mobile,
              mobileCountryCode: credentials.email,
              otp: credentials.otpFormatt,
            },
          },
          validateStatus: (status) => {
            if (status == 500) {
              console.log(status, "status axios log");
              console.log(credentials, "credentials log");
              return true;
            }
          },
          headers: {
            "User-Agent":
              "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
            Accept: "application/json; charset=UTF-8",
          },
        })
          .catch((error) => {
            console.log(error.response.data, "axios fail calls");
          })
          .then((result) => {

Currently I can see this console log "axios calls" successfully displays itself on the terminal but the tokens and sessions aren't returned.

            console.log(result.data, "axios calls");
            console.log(credentials, "creds");
                return result.data.data 
          });
        if (user) {
          return user;
        } else {
          return null;
        }
      },
    }),
  ],
};


const callbacks = {
  async jwt(token, user) {
    if (user) {
      token.accessToken = user.token;
    }

    return token;
  },
  
  secret:process.env.SECRET,

  async session(session, token) {
    session.accessToken = token.accessToken;
    return session;
  },
};

export default (req, res) => NextAuth(req, res, options);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Error 500 means server error. Check you api server whether it is running or stopped. Error 500 here has nothing to do with nextjs, it is all about the back-end server.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!