[GH-ISSUE #1488] Help with SSO Authentication for Multiple Applications, Single Account (Similar to Google Accounts) #8783

Closed
opened 2026-04-13 03:59:17 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @FierceGrandpa on GitHub (Feb 18, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1488

Hello!

We have two applications built with Next.js + NestJS + Prisma. Currently, each application has a custom authentication system, but we want to implement a unified SSO solution through a third application (SSO server), similar to Google Account, which allows users to log in to multiple services.

Questions:

  1. Is it possible to implement this scenario using your library?
  2. What is the best way to structure the architecture in our case? For the frontend/backend of each application and the SSO/IDP server itself.

We would greatly appreciate your help!

Originally created by @FierceGrandpa on GitHub (Feb 18, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1488 Hello! We have two applications built with Next.js + NestJS + Prisma. Currently, each application has a custom authentication system, but we want to implement a unified SSO solution through a third application (SSO server), similar to Google Account, which allows users to log in to multiple services. Questions: 1. Is it possible to implement this scenario using your library? 2. What is the best way to structure the architecture in our case? For the frontend/backend of each application and the SSO/IDP server itself. We would greatly appreciate your help!
GiteaMirror added the locked label 2026-04-13 03:59:17 -05:00
Author
Owner

@jlzhjp commented on GitHub (Feb 24, 2025):

Also facing a similar challenge here.

It seems that better-auth is fundamentally architected as a third-party OIDC client library focused exclusively on user authentication rather than comprehensive resource server integration. Critical features like access token retrieval and automated token refresh mechanisms seem absent from its implementation.

As better-auth stores access tokens and refresh tokens in the account table, it may be possible to use it as a first-party client. However, you have to implement token refreshing yourself by directly accessing the database.

Something like Auth0 SDK for Next.js may fit the scenario better. But it appears exclusive to Auth0’s ecosystem rather than being compatible with generic OIDC-compliant identity providers like Keycloak :(

<!-- gh-comment-id:2678393557 --> @jlzhjp commented on GitHub (Feb 24, 2025): Also facing a similar challenge here. It seems that `better-auth` is fundamentally architected as a [third-party](https://auth0.com/docs/get-started/applications/confidential-and-public-applications/first-party-and-third-party-applications) OIDC client library focused exclusively on user authentication rather than comprehensive resource server integration. Critical features like access token retrieval and automated token refresh mechanisms seem absent from its implementation. As `better-auth` stores access tokens and refresh tokens in the `account` table, it may be possible to use it as a first-party client. However, you have to implement token refreshing yourself by directly accessing the database. Something like [Auth0 SDK for Next.js](https://github.com/auth0/nextjs-auth0) may fit the scenario better. But it appears exclusive to Auth0’s ecosystem rather than being compatible with generic OIDC-compliant identity providers like Keycloak :(
Author
Owner

@Bekacru commented on GitHub (Mar 13, 2025):

Hey @FierceGrandpa, if you own all the applications in your stack, I strongly recommend relying on a single auth server without OIDC or SSO. In most cases, you should only set up your own IDP with OIDC when you don’t own the services/applications—like when providing your API to third parties.

<!-- gh-comment-id:2722088468 --> @Bekacru commented on GitHub (Mar 13, 2025): Hey @FierceGrandpa, if you own all the applications in your stack, I strongly recommend relying on a single auth server without OIDC or SSO. In most cases, you should only set up your own IDP with OIDC when you don’t own the services/applications—like when providing your API to third parties.
Author
Owner

@Abenezer commented on GitHub (Mar 26, 2025):

@Bekacru Yes its good idea to use single auth server for such cases. but you need someway to share a session between the apps. does better auth have this feature or should we implement it on our own. (using redis or database)

<!-- gh-comment-id:2753955643 --> @Abenezer commented on GitHub (Mar 26, 2025): @Bekacru Yes its good idea to use single auth server for such cases. but you need someway to share a session between the apps. does better auth have this feature or should we implement it on our own. (using redis or database)
Author
Owner

@carlosmfreitas2409 commented on GitHub (May 22, 2025):

I'm having this problem too. The idea is that all the applications I have (and own) have a centralized login, like Google Accounts.
So I have my IdP (Google Accounts) and I have my SPs (Youtube, Gmail, etc).

A very important point to consider is that SPs can have different domains, so cross-domain is a thing...

I agree with @Bekacru that if you own all the applications, an OIDC is not necessary, however, I believe that to have this logic working cross-domain, you need to follow some little OIDC standards, such as token and authorization endpoint. Maybe there is another way? I personally can't think straight anymore hahah

I made the following sequence (image bellow), however it has some problems:

  1. After the token endpoint, following OIDC pattern, a session would be created. But should the SPs backend have knowledge of the IdP database to create the session?
    Futhermore, the user already authenticated within the IdP, which called /auth/sign-in and creates a session in the db. So would it create two "identical"/conflict sessions? Since it would not be safe to send the token created in /auth/sign-in to the callback in the SPs.
  2. Should I save session cookies on each SP and IdP or just on the IdP? If yes, different signatures per application? What happens if the sessionData or sessionToken expires? Should I call a introspect endpoint every time?

I couldn't think of an easier way than the sequence below. Maybe it has...

Image

<!-- gh-comment-id:2899675440 --> @carlosmfreitas2409 commented on GitHub (May 22, 2025): I'm having this problem too. The idea is that all the applications I have (and own) have a centralized login, like Google Accounts. So I have my IdP (Google Accounts) and I have my SPs (Youtube, Gmail, etc). A very important point to consider is that SPs can have different domains, so cross-domain is a thing... I agree with @Bekacru that if you own all the applications, an OIDC is not necessary, however, I believe that to have this logic working cross-domain, you need to follow some little OIDC standards, such as token and authorization endpoint. Maybe there is another way? I personally can't think straight anymore hahah I made the following sequence (image bellow), however it has some problems: 1. After the token endpoint, following OIDC pattern, a session would be created. But should the **SPs backend** have knowledge of the IdP database to create the session? Futhermore, the user already authenticated within the IdP, which called `/auth/sign-in` and creates a session in the db. So would it create two "identical"/conflict sessions? Since it would not be safe to send the token created in `/auth/sign-in` to the callback in the SPs. 2. Should I save session cookies on each SP and IdP or just on the IdP? If yes, different signatures per application? What happens if the `sessionData` or `sessionToken` expires? Should I call a introspect endpoint every time? I couldn't think of an easier way than the sequence below. Maybe it has... ![Image](https://github.com/user-attachments/assets/24651e6a-15ed-4faf-aa0c-acfe40326270)
Author
Owner

@Zeryther commented on GitHub (Jul 30, 2025):

Are there any updates on this? Clerk seems to handle most of this out of the box. I considered switching to better-auth, but this is a major blocker.

<!-- gh-comment-id:3134697913 --> @Zeryther commented on GitHub (Jul 30, 2025): Are there any updates on this? Clerk seems to handle most of this out of the box. I considered switching to better-auth, but this is a major blocker.
Author
Owner

@dosubot[bot] commented on GitHub (Oct 29, 2025):

Hi, @FierceGrandpa. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You are exploring how to implement SSO across two Next.js + NestJS + Prisma apps using a third-party SSO server.
  • You asked if better-auth supports this scenario and the best architecture for frontend/backend and SSO/IDP.
  • Comments highlighted that better-auth is primarily a third-party OIDC client focused on user authentication without built-in token refresh.
  • Alternatives like Auth0 SDK for Next.js were suggested, and some recommended a single auth server if all apps are owned by you.
  • The discussion also covered challenges with session sharing and cross-domain issues, with some users noting this is a blocker for adopting better-auth.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here.
  • If I don’t hear back within 7 days, I will automatically close this issue to keep the backlog manageable.

Thanks for your understanding and contribution!

<!-- gh-comment-id:3462539700 --> @dosubot[bot] commented on GitHub (Oct 29, 2025): Hi, @FierceGrandpa. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You are exploring how to implement SSO across two Next.js + NestJS + Prisma apps using a third-party SSO server. - You asked if better-auth supports this scenario and the best architecture for frontend/backend and SSO/IDP. - Comments highlighted that better-auth is primarily a third-party OIDC client focused on user authentication without built-in token refresh. - Alternatives like Auth0 SDK for Next.js were suggested, and some recommended a single auth server if all apps are owned by you. - The discussion also covered challenges with session sharing and cross-domain issues, with some users noting this is a blocker for adopting better-auth. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here. - If I don’t hear back within 7 days, I will automatically close this issue to keep the backlog manageable. Thanks for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8783