-
-
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
Use/Validate Authorization in external API #402
Comments
Currently NextAuth.js provides account creation, authentication and session management. It doesn't currently support also handling rotating provider Access Tokens or tracking when they expire for API access - if you want to do that you will need to write code to handle that explicitly. To make this easier, it records the Refresh Token (and initial Access Token) for each user and provider on the first time a signs in with a provider (if they are supplied by the provider) and it creates a table to store Access Tokens and when they expire so you know when you need to fetch a new one. RE: Cross domain access You can use a custom cookie policy to allow same domain access to the Session Token for subdomains - this actually just came up in #405 which links to the relevant documentation - but you cannot share cookies between domains as that is a fundamental limitation of cookies. If you need to share data across domains then you are probably going to need to look at a solution involving iFrames and the postMessage API. |
@iaincollins Thanks for the feedback!
I think I won't approach this option further just as I expected initially, as I can't seem to cleanly fit this into my usecase.
This is good to know, and the part that might actually be interesting to me. I assume these tokens only are stored in the database after they were verified on the Provider they belong to? Like, I can't somehow inject a wrong token there as an user, assuming I somehow got past the cookie encyption? Now, instead of directly grabbing the access token and doing a provider-specific check in my API, I could pass some identifier to my api, which could then call some api route that makes use of some to-be-determined next-auth functionality, which returns if an entry exists for that identifer, which provider it belongs to, plus the auth token and maybe other relevant session data. Also, I considered to make things a bit more secure by having both the nextjs api and my API use a secret known to both, so don't even have the pass the bare token, but that is probably redundant as at that point, leaking the encrypted token would have the same result and give access anyway. Instead, protecting the next-auth API route as such might make more sense. So in short, what I would probably need:
|
We are building a version 2 site with NextJS and next-auth is a great fit for us. However it will take time to have this feature complete in production and for the current version 1 (plain html/javescript) we already need to use the same authentication system. So the plan is to have version 2 in production with just the next-auth features and have v1 use this cross domain. Is there an example of using on iframe with postMessage to make this work? |
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 ot open. 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! |
Your question
I'm looking for the most straight forward way to use authentication in a separate GraphQL API.
What are you trying to do
Currently, I have implemented a custom oauth 2 flow, and each client can request their access token(but not request token) in an api endpoint, where it is read from their encrypted cookie. That token is then passed as Header when querying the GraphQL API from client.
I want to use next-auth to provide additional signup options, and I already found issues describing how to make the access token available as part of the session that the client can fetch.
However, this has two drawbacks:
What I'm looking for is a generalized way to validate a session coming from one of the next-auth supported Providers, including email. Also, I wondered if there is some kind of caching for finished authorization flows during their lifespan, which I could make use of?
Because I imagine that fetching e.g. the twittter api each time an authenticated request comes in to validate the token is highly inefficient and slow.
A general consideration: While most implementations will use the same domain, this should also work if the API is not using the domain and therefore probably has no safe access to the cookie.
Documentation feedback
The text was updated successfully, but these errors were encountered: