-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the best way to have same authentication on all subdomains? #405
Comments
This a really great question! Right now, the only way to do this would be to set a custom cookie policy. e.g. You should be able to set a 'domain' option on the 'sessionToken' cookie. However, that should work as long as they sign in on Note: This is a little awkward as you will likely want different different cookie policies in development and live - unless you are also running HTTPS in development. If using the default policy NextAuth.js does this automatically, but if you are using a custom policy you will have to handle this yourself. We could always provide a configuration option to make this as simple as passing an option in NextAuth.js. |
I already have an API server which I want to make accessible at api.domain.com and it would be great to have this option in NextAuth! Meanwhile I am trying to understand how exactly to achieve it using the options you've quoted above. That said, @iaincollins thanks for such a fantastic library! I am moving my UI from CRA to Next JS and it was such a smooth experience to integrate Next Auth into the new application UI. What earlier tooks me 2 days was done in a matter of half an hour. And the documentation is very clean and clear too! |
Hi @iaincollins, could you please provide the exact setting that @neoromantic would need? We have a very similar setup and usecase. |
I've implemented this with the following and it seems to work:
Basically it's a bunch of code to add a @iaincollins would you accept a PR to make this a flag in the options? It's a lot of work to go through for something which I think many people will want. |
Hey I would love a an option to set this that would then get applied to all relevant cookies set by NextAuth.js. I've been thinking about the best way for this to work. e.g.
For context, I've also been thinking about adding support for cross-domain silent login in the longer term. That might as a top level option like I've been pondering the simplest way for these options to exist to help folks avoid confusion. If it's more like option 2. then maybe it should be a top level option and not nested inside cookies, to make it easier to find, understand and set. (e.g. just |
Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep it open. (Read more at #912) Thanks! |
Hi there! It looks like this issue hasn't had any activity for a while. To keep things tidy, I am going to close this issue for now. If you think your issue is still relevant, just leave a comment and I will reopen it. (Read more at #912) Thanks! |
I'm having a similar issue. I tried with the cookies configuration as @Xodarap showed above and this is what I got: app 1 running on https://abc.mydomain.com: I successfully log in and can see the profile information. The cookie "__Secure-next-auth.session-token" is set to the domain ".mydomain.com" app 2 running on https://xyz.mydomain.com: The cookie is not present and even gets removed from "abc.mydomain.com" as soon as I enter to this app.
|
@iaincollins this works for my current use case – do you have an example for this? I'm a little confused by the current example in the docs – what exactly are
Also, should we insert this code snippet under Thanks in advance! |
Did you ever figure this out? I am currently trying exactly the same thing!! |
Quick update: @balazsorban44 released this example that allows Next-Auth to persist auth state across different subdomains: https://github.com/vercel/examples/tree/main/solutions/subdomain-auth |
Hello! I've managed to follow this thread to solve my issue with getting my custom cookie to work with my subdomains. However, I am stuck at the moment trying to figure out how to renew an expired cookie on a subdomain that is not hosting the next-auth code. is this possible, or am I heading into uncharted waters? I figured with renewing the cookie on the subdomain that hosts the next-auth code, I can quickly check the cookie, and redirect my user to the sign in page to sign in and get a new cookie. however, in my app that resides on a subdomain, I'm unsure how to go about checking whether my cookie is expired, and how to go about refreshing it. one solution may be to have the user redirected to the sign in page, but it might not feel like good 'ux'. |
I'm having the same problem with subdirectories app1 -> mydomain.com/abc app2 -> mydomain.com/xyz The cookie is removed from "mydomain.com/abc" as soon as I enter to "mydomain.com/xyz" Any solution? |
@steven-tey that example is great for subdomains, but is there anything for cross site domains? Specifically in development I have a backend server at 0.0.0.0 and then next.js is at localhost (127.0.0.1). I want to set a cookie to have a domain of I am setting my own cookie for a custom backend in a next-auth callback to have the domain of 0.0.0.0 but it never gets set. When I do
nothing appears in my browser. It seems that I can only set a cookie with the domain of the like |
Is there an easy to way to update users |
Your question
Is there a way and what's the best approach to have same authentication on subdomains as well as on main domain?
What are you trying to do
Suppose, I have a website, which has some subprojects on various subdomains and I'd love to have cross-domain authentication.
domain.com — my main website build on Next.js
app.domain.com — some kind of app
app2.domain.com — some other app
Some of these apps might be same Next.js app, other might be separate Next.js app.
I'd love to have same authentication for these app. So user can log in on one of these sites and become logged in on all others. Same with logout and session handling.
Can it be done with next-auth? How? What would be the best approach?
Documentation feedback
The text was updated successfully, but these errors were encountered: