Next.js authentication with existing backend
Tags: nextjs, nextauth, authentication, credentials
Published: 2021-03-31 09:03:00

---
title: Next.js authentication with existing backend
published: true
description: Next.js authentication approach using next-auth with credentials provider
tags: nextjs, nextauth, authentication, credentials
cover_image: https://images.pexels.com/photos/4291/door-green-closed-lock.jpg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260
---

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:
- https://nextjs.org/docs - Next.js docs
- https://next-auth.js.org/getting-started/introduction - next-auth docs
- https://next-auth.js.org/providers/credentials - next-auth credentials provider description

JWT:
- https://youtu.be/7Q17ubqLfaM - quick explanation about the 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:

```javascript
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
      }
    }
  })
]
```

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:

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

    // Subsequent calls
    return prevToken;
  }
};
```

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:

```javascript
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);
  },
}
```

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.