next-auth-axios-adapter
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

next-auth-axios-adapter

next-auth-axios-adapter is an authentication adapter for NextAuth.js, which offers complete flexibility to authenticate with any server, allowing you to define fully custom HTTP methods and URL paths using Axios. This adapter provides an extremely versatile way to integrate NextAuth.js into your Next.js application while adapting to your server's unique authentication requirements.

Features

  • Full integration of NextAuth.js with Axios for custom server authentication.
  • Define and customize the HTTP methods and URL paths for authentication requests.
  • Accommodate a wide range of authentication strategies and server setups.
  • Easily configure authentication providers and adapt to any server's APIs.
  • Simple and efficient setup for both new and existing Next.js applications.

Installation

To install next-auth-axios-adapter, you can use npm or yarn:

npm install next-auth-axios-adapter

or

yarn add next-auth-axios-adapter

Usage

Set up NextAuth.js: Configure NextAuth.js in your project with the next-auth-axios-adapter. Define your authentication providers, strategies, and callback functions.

// src/app/api/auth/[...nextauth]/route.ts
import NextAuth from 'next-auth';
import AxiosAdapter, { AdapterSettings } from 'next-auth-axios-adapter';
import {
  AdapterAccount,
  AdapterSession,
  AdapterUser,
  VerificationToken as AdapterVerificationToken,
} from 'next-auth/adapters';
import axios, { AxiosResponse } from 'axios';

const settings: AdapterSettings = {
  baseUrl: `${process.env.SERVER_URL}/api`,
  configs: {
    createUser(user) {
      return {
        method: 'POST',
        path: '/users',
        sendBody: user,
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
      };
    },
    getUser(id) {
      return {
        method: 'GET',
        path: `/users/${id}`,
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
      };
    },
    getUserByEmail(email) {
      return {
        method: 'POST', // I prefer POST to GET (send email data)
        path: `/users/email`,
        sendBody: { email: email },
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
      };
    },
    getUserByAccount(data) {
      return {
        method: 'POST', // I prefer POST to GET (send account data)
        path: `/users/account`,
        sendBody: data,
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
      };
    },
    updateUser(data) {
      return {
        method: 'PUT',
        path: `/users/${data.id}`,
        sendBody: data,
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
      };
    },
    deleteUser(id) {
      return {
        method: 'DELETE',
        path: `/users/${id}`,
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
      };
    },
    linkAccount(data) {
      return {
        method: 'POST',
        path: `/accounts`,
        sendBody: data,
        selectedData: (res: AxiosResponse<{ account: AdapterAccount }, any>) =>
          res.data.account,
      };
    },
    unlinkAccount(data) {
      return {
        method: 'PUT', // I prefer PUT to DELETE (send provider data)
        path: `/accounts/delete`,
        sendBody: data,
        selectedData: (res: AxiosResponse<{ account: AdapterAccount }, any>) =>
          res.data.account,
      };
    },
    getSessionAndUser(sessionToken) {
      return {
        method: 'GET',
        path: `/sessions/session-tokens/${sessionToken}`,
        selectedData: (
          res: AxiosResponse<
            { user: AdapterUser; session: AdapterSession },
            any
          >
        ) => ({
          user: res.data.user,
          session: res.data.session,
        }),
      };
    },
    createSession(data) {
      return {
        method: 'POST',
        path: `/sessions`,
        sendBody: data,
        selectedData: (res: AxiosResponse<{ session: AdapterSession }, any>) =>
          res.data.session,
      };
    },
    updateSession(data) {
      return {
        method: 'PUT',
        path: `/sessions/session-tokens/${data.sessionToken}`,
        sendBody: data,
        selectedData: (res: AxiosResponse<{ session: AdapterSession }, any>) =>
          res.data.session,
      };
    },
    deleteSession(sessionToken) {
      return {
        method: 'DELETE',
        path: `/sessions/session-tokens/${sessionToken}`,
        selectedData: (res: AxiosResponse<{ session: AdapterSession }, any>) =>
          res.data.session,
      };
    },
    createVerificationToken(data) {
      return {
        method: 'POST',
        path: `/verification-tokens`,
        sendBody: data,
        selectedData: (
          res: AxiosResponse<
            { verificationToken: AdapterVerificationToken },
            any
          >
        ) => res.data.verificationToken,
      };
    },
    useVerificationToken(data) {
      return {
        method: 'PUT', // I prefer PUT to DELETE (send verificationToken data)
        path: `/verification-tokens/identifier`,
        sendBody: data,
        selectedData: (
          res: AxiosResponse<
            { verificationToken: AdapterVerificationToken },
            any
          >
        ) => res.data.verificationToken,
      };
    },
  },
};

const handler = NextAuth({
  // ...
  adapter: AxiosAdapter(axios, settings),
  // ...
});

export { handler as GET, handler as POST };

Use AxiosInstance

// ...
import axios, { AxiosResponse } from 'axios';

const axiosInstance = axios.create({
  baseURL: process.env.SERVER_URL,
  headers: {
    // Authorization: `Bearer ${process.env.SERVER_API_KEY}`,
    'Content-Type': 'application/json',
  },
  // timeout: 10000,
});

const settings: AdapterSettings = {
  baseUrl: `/api`, // Set baseURL in AxiosInstance
  configs: {
    // ...
  },
};

const handler = NextAuth({
  // ...
  adapter: AxiosAdapter(axiosInstance, settings),
  // ...
});

export { handler as GET, handler as POST };

Configs Method

// ...
const settings: AdapterSettings = {
  // ...
  configs: {
    // ...
    // Example createUser
    createUser(user) {
      return {
        method: 'POST', // GET POST PUT PATCH DELETE
        path: '/users',
        sendBody: user, // send body support POST PUT PATCH
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
        isMongoDb: true, // optional (Auto convert _id to id)
        requestConfig: { headers: { 'Content-Type': 'application/json' } }, // optional
      };
    },
    // ...
    // Example getUserByEmail
    getUserByEmail(email) {
      return {
        method: 'GET', // GET POST PUT PATCH DELETE
        path: `/users/email/${encodeURIComponent(email)}`,
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
        isMongoDb: true, // optional (Auto convert _id to id)
        requestConfig: { headers: { 'Content-Type': 'application/json' } }, // optional
      };
    },
    // ...
    // Example updateUser
    updateUser(data) {
      return {
        method: 'PUT', // GET POST PUT PATCH DELETE
        path: `/users/${data.id}`,
        sendBody: data, // send body support POST PUT PATCH and If isMongoDb: true (Auto convert id to _id)
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
        isMongoDb: true, // optional (Auto convert _id to id)
        requestConfig: { headers: { 'Content-Type': 'application/json' } }, // optional
      };
    },
    // ...
    // Example deleteUser
    deleteUser(id) {
      return {
        method: 'DELETE', // GET POST PUT PATCH DELETE
        path: `/users/${id}`,
        selectedData: (res: AxiosResponse<{ user: AdapterUser }, any>) =>
          res.data.user,
        isMongoDb: true, // optional (Auto convert _id to id)
        requestConfig: { headers: { 'Content-Type': 'application/json' } }, // optional
      };
    },
    // ...
  },
};
// ...

Credits and Thanks to inspire me

Auth.js: Creating a database adapter @auth/mongodb-adapter

Package Sidebar

Install

npm i next-auth-axios-adapter

Weekly Downloads

16

Version

1.0.4

License

ISC

Unpacked Size

86.9 kB

Total Files

23

Last publish

Collaborators

  • vanzinvestor