[GH-ISSUE #1426] Feature Request / Question: External API Login in Next.js #8747

Closed
opened 2026-04-13 03:56:31 -05:00 by GiteaMirror · 10 comments
Owner

Originally created by @dev-SR on GitHub (Feb 12, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1426

I'm using Next.js with a .NET API for authentication and want to integrate Better-Auth to handle sign-in via my external .NET API. Specifically, I need guidance on:

  1. Using Better-Auth Credentials to authenticate users against a external API (.NET in my case).
  2. Storing and managing the access token from the external API in cookies (or session storage) securely.
  3. Ensuring the token is available for authenticated requests in both client-side and server-side Next.js components.

With NextAuth.js, this is typically implemented:

import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

export const authOptions = {
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        username: { label: "Username", type: "text", placeholder: "john_doe" },
        password: { label: "Password", type: "password" },
      },
      async authorize(credentials, req) {
        const res = await fetch("http://127.0.0.1:4000/auth/login/", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({
            username: credentials.username,
            password: credentials.password,
          }),
        });

        const user = await res.json();

        if (!res.ok) {
          throw new Error(user.message || "Login failed");
        }

        return user;
      },
    }),
  ],
  callbacks: {
    async jwt({ token, user }) {
      if (user) {
        token.accessToken = user.token; // Store .NET API token in JWT
      }
      return token;
    },
    async session({ session, token }) {
      session.accessToken = token.accessToken;
      return session;
    },
  },
  secret: process.env.NEXTAUTH_SECRET,
  pages: {
    signIn: "/login",
  },
};

export default NextAuth(authOptions);

Thanks! 😊

Originally created by @dev-SR on GitHub (Feb 12, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1426 I'm using **Next.js** with a **.NET API** for authentication and want to integrate **Better-Auth** to handle sign-in via my external .NET API. Specifically, I need guidance on: 1. Using **Better-Auth Credentials** to authenticate users against a **external API (.NET in my case)**. 2. Storing and managing the **access token from the external API** in **cookies** (or session storage) securely. 3. Ensuring the token is available for authenticated requests in both **client-side** and **server-side** Next.js components. With **NextAuth.js**, this is typically implemented: ```ts import NextAuth from "next-auth"; import CredentialsProvider from "next-auth/providers/credentials"; export const authOptions = { providers: [ CredentialsProvider({ name: "Credentials", credentials: { username: { label: "Username", type: "text", placeholder: "john_doe" }, password: { label: "Password", type: "password" }, }, async authorize(credentials, req) { const res = await fetch("http://127.0.0.1:4000/auth/login/", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username: credentials.username, password: credentials.password, }), }); const user = await res.json(); if (!res.ok) { throw new Error(user.message || "Login failed"); } return user; }, }), ], callbacks: { async jwt({ token, user }) { if (user) { token.accessToken = user.token; // Store .NET API token in JWT } return token; }, async session({ session, token }) { session.accessToken = token.accessToken; return session; }, }, secret: process.env.NEXTAUTH_SECRET, pages: { signIn: "/login", }, }; export default NextAuth(authOptions); ``` Thanks! 😊
GiteaMirror added the locked label 2026-04-13 03:56:31 -05:00
Author
Owner

@margaretjoanmiller commented on GitHub (Feb 13, 2025):

I'm in the exact same boat so I'd love to see what folks say about this

<!-- gh-comment-id:2657911633 --> @margaretjoanmiller commented on GitHub (Feb 13, 2025): I'm in the exact same boat so I'd love to see what folks say about this
Author
Owner

@jslno commented on GitHub (Feb 14, 2025):

you could just write your own plugin with a sign-up / sign-in endpoint. This allows passing more credentials than just username and password

<!-- gh-comment-id:2659752454 --> @jslno commented on GitHub (Feb 14, 2025): you could just write your own plugin with a sign-up / sign-in endpoint. This allows passing more credentials than just username and password
Author
Owner

@strangemodern commented on GitHub (Feb 21, 2025):

Subscribed. I'm doing something similar with a Nest API, but keep running into errors when implementing plugin after hooks.

<!-- gh-comment-id:2673299354 --> @strangemodern commented on GitHub (Feb 21, 2025): Subscribed. I'm doing something similar with a Nest API, but keep running into errors when implementing plugin after hooks.
Author
Owner

@itisvincent commented on GitHub (Mar 7, 2025):

you could just write your own plugin with a sign-up / sign-in endpoint. This allows passing more credentials than just username and password

is there any document to guide us to do that? thanks

<!-- gh-comment-id:2705516478 --> @itisvincent commented on GitHub (Mar 7, 2025): > you could just write your own plugin with a sign-up / sign-in endpoint. This allows passing more credentials than just username and password is there any document to guide us to do that? thanks
Author
Owner
<!-- gh-comment-id:2705881066 --> @jslno commented on GitHub (Mar 7, 2025): > is there any document to guide us to do that? thanks https://www.better-auth.com/docs/guides/your-first-plugin https://www.better-auth.com/docs/concepts/plugins#creating-a-plugin https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/routes/sign-in.ts https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/routes/sign-up.ts
Author
Owner

@whitemyrat commented on GitHub (Mar 8, 2025):

Has anyone had success with an external API?

<!-- gh-comment-id:2708529779 --> @whitemyrat commented on GitHub (Mar 8, 2025): Has anyone had success with an external API?
Author
Owner

@christianwebdev200 commented on GitHub (Apr 16, 2025):

Joining the boat here, I hope they will support external API or include an adapter for NestJs and Laravel. I want to escape the Next-Auth nightmare haha!

<!-- gh-comment-id:2808075863 --> @christianwebdev200 commented on GitHub (Apr 16, 2025): Joining the boat here, I hope they will support external API or include an adapter for NestJs and Laravel. I want to escape the Next-Auth nightmare haha!
Author
Owner

@lagroms commented on GitHub (May 30, 2025):

I'm in the same situation, I'd like to use my external API as well. Anything that won't make me use auth.js 🤢

<!-- gh-comment-id:2921295351 --> @lagroms commented on GitHub (May 30, 2025): I'm in the same situation, I'd like to use my external API as well. Anything that won't make me use auth.js 🤢
Author
Owner

@MuhammadM1998 commented on GitHub (Jun 25, 2025):

This would be great!

<!-- gh-comment-id:3006129977 --> @MuhammadM1998 commented on GitHub (Jun 25, 2025): This would be great!
Author
Owner

@frectonz commented on GitHub (Aug 11, 2025):

To integrate this into better auth the authentication server would need to conform to some kind of standard like OIDC, which Better Auth supports. If the server doesn't conform to a standard then it doesn't make sense to add this to the core of better auth, so i recommend creating a separate plugin.

<!-- gh-comment-id:3176803699 --> @frectonz commented on GitHub (Aug 11, 2025): To integrate this into better auth the authentication server would need to conform to some kind of standard like OIDC, which Better Auth supports. If the server doesn't conform to a standard then it doesn't make sense to add this to the core of better auth, so i recommend creating a separate plugin.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8747