
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
next-auth-axios-adapter
Advanced tools
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
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.
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
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 };
// ...
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 };
// ...
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
};
},
// ...
},
};
// ...
FAQs
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
The npm package next-auth-axios-adapter receives a total of 5 weekly downloads. As such, next-auth-axios-adapter popularity was classified as not popular.
We found that next-auth-axios-adapter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.