DEV Community

Cover image for Next.js authentication with existing backend
Szymon Kabelis
Szymon Kabelis

Posted on

Next.js authentication with existing backend

Recently I was working on some Next.js project and came to issues regarding the authentication using a built-in Next.js server and the existing backend that holds whole logic and access to a database.

I had a problem with understanding how the next-auth is working and how to properly use the credentials provider, or even if it can be used with an existing backend.

So, shortly, the answer is yes! You can use the credentials provider and make auth working together with an existing backend.

In this post, I will just focus on making auth working between the Next.js server and the existing API. If you need to set up the auth in the Next.js app from scratch, please follow the docs mentioned below.

Basics

Before I proceed to Next.js and auth itself I just want to mention that it would be good if you understand the basics correctly. I've prepared the list of links with useful content:

Next.js:

JWT:

Credentials provider

Next-auth lib gives various providers to authenticate against but we are interested in the classic one, just with the username and email. In fact, it doesn't care if you are using an existing backend or do all the auth stuff in the Next.js server (but in that case you should think about a different provider).

So, as the docs stand, the first thing you need to do is a setup of provider:

providers: [
  Providers.Credentials({
    name: 'Credentials',
    credentials: {
      username: { label: "Username", type: "text", placeholder: "jsmith" },
      password: {  label: "Password", type: "password" }
    },
    async authorize(credentials) {
      // Here call your API with data passed in the login form
      const token = await loginEndpoint(credentials);

      if (token) {
        return token
      } else {
        return null
      }
    }
  })
]
Enter fullscreen mode Exit fullscreen mode

In authorize() handler you need to make a request to your API to auth the user and if credentials are correct you can return the response (or directly the token).

And now you need to set up the jwt() callback. It can look like that:

const callbacks = {
  async jwt(prevToken, token) {
    // Initial call
    if (token) {
      return {
        accessToken: token,
      };
    }

    // Subsequent calls
    return prevToken;
  }
};
Enter fullscreen mode Exit fullscreen mode

This should work fine. But there is one issue.

Expired token issue

The token you get from your API is not refreshed. So, you can have a situation when auth between the Next.js client and Next.js server works fine but the token you have from your API has expired.

So, to make requests from the client through the Next.js server to your API, you would need to logout and login once again to have a valid token.

Solution

You can't just set the options of session.maxAge to match the one from the API you are using because every time you access the site (assuming you are using next-auth) time of the session is automatically extended.

However, what you can do is, manually set the expired time by hardcoding a value or returning the expired time value from the login/refresh endpoint. See how the code looks in that case:

const refreshAccessToken = async (prevToken) => {
  const token = await refreshEndpoint(prevToken);

  // Do what you want

  return {
    accessToken: token.accessToken,
    accessTokenExpires: Date.now() + token.expiresIn * 1000,
  };
}

const callbacks = {
  async jwt(prevToken, token) {
    // Initial call
    if (token) {
      return {
        accessToken: token.accessToken,
        // Assuming you can get the expired time from the API you are using
        // If not you can set this value manually
        accessTokenExpires: Date.now() + token.expiresIn * 1000,
      };
    }

    // Subsequent calls
    // Check if the expired time set has passed
    if (Date.now() < prevToken.accessTokenExpires) {
      // Return previous token if still valid
      return prevToken;
    }

    // Refresh the token in case time has passed
    return refreshAccessToken(prevToken);
  },
}
Enter fullscreen mode Exit fullscreen mode

As you can see, in the initial call, you need to set the expired time based on what the API returns or just hardcode the value (e.g. 30 * 30 - 1 hour).

Then, whenever the session is checked jwt() callback is called and the current time is compared to expired. If it's passed then you need to call the refreshAccessToken function, in which you can refresh the token using the API, otherwise, the previous token is returned because it still can be used.

With this approach, you can use the Next.js server as a proxy between the client app and the API you are using. This can be very useful due to the power of Next.js API Routes.

If you have any questions, let me know in the comments.

Top comments (11)

Collapse
 
dededavida profile image
David Cruz

Hi guys, if you need the token in an axios request, do it like this!

api.tsx

Collapse
 
wparad profile image
Warren Parad

Will have to say, it's much easier to pull in a package such as federated auth to do this automatically. You don't need to set anything extra or understand how JWTs are supported to work to make sure the UI app works correctly.

Collapse
 
szymkab profile image
Szymon Kabelis

Well, I would say using next-auth doesn't require knowing JWTs but I've included that in the post because I think it's always better to know at least something about what you are using...

Collapse
 
wparad profile image
Warren Parad

Sure, but it's better than to put that as the perspective in the article. "Hey, this is difficult, there are packages to handle it, and here we are going to dive into why these packages exist and what they are providing."

Thread Thread
 
jaketone profile image
Jake

Quite an aggressive way of referring others to your own package. Thank you @szymkab for the simple concept.

Collapse
 
dwalker93 profile image
Adam Walker

Hey Szymon this is great article.. But using this how to send authenticated requests to another server?
For example next app in client and main express graphql api in another api path. how to send authenticated request to other server?

Collapse
 
szymkab profile image
Szymon Kabelis

Hi, do you want to send requests from Next.js client (browser react app) or server?

Collapse
 
dwalker93 profile image
Adam Walker

yeah for example my front next js app in client folder and backend express graphql server in server folder. I can set token as a header when request from next to graphql api. but how to decode that jwt in graphql api ?

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
msahil1 profile image
msahil1

Hi
great info, Could also tell how we can do NextAuth Google login with exisitng backend API as backend API releases its own token.

Collapse
 
dmtrbch profile image
Dimitar Bochvarovski

Hello, is it possible to use this method for authentication with existing backend, but with other providers, such as google, twitter...

Thanks in advance