npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-auth-axios-adapter

v1.0.4

Published

Axois 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

Downloads

49

Readme

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