Replies: 5 comments 1 reply
-
So I will answer my own question and will try to update this discussion as I make changes. Unfortunately, that approach was flawed as I got into many CORS errors as I was hitting that endpoint from the extension running in another tab with a different origin. I needed a way to get a JWT in the extension. So the idea is when a user log in, I redirect them to the protected route. In this case, the In the getserversideprops hook, I check that the user is logged in, then generate a JWT. That message is then received in a This question help me find the answer: So now we have a JWT, I created a few API endpoints that validate the JWT in the headers and return the user's data. So now I can retrieve the user data from the extension running in another tab from an origin that is not my domain. For now this solution very much feels like duck tape and pieces of strings put together, but it seems to work pretty well, so I ll try to refine it and harden it. If someone has any feedback or a better way to do things I am all hears. |
Beta Was this translation helpful? Give feedback.
-
Here's some very bare bone implementation of what @YannHulot has suggested for the ones who new to extensions. content.js:
background.js:
popup.js
manifest.js
popup.html
|
Beta Was this translation helpful? Give feedback.
-
I need some help here to see if I understand whats going on. User is redirected to perform the authorization on the auth page and then the extension saves the JWT to local storage? |
Beta Was this translation helpful? Give feedback.
-
@YannHulot , @cguagenti, @Dizotoff , thanks so much for your question and comments on the topic! Did you guys end up figuring a better approach? |
Beta Was this translation helpful? Give feedback.
-
I'm facing the same problem, I use I will try the above |
Beta Was this translation helpful? Give feedback.
-
Hi there,
I am kind of new to
next-auth
and to authentication flows on the web in general so please forgive me if what I am asking is evident or stupid.I am using
next-auth
to authenticate users on myNextjs
app. I only use the Google provider and use the database strategy to save the sessions in my MongoDB.I am also building a Chrome extension that ideally would be able to interact with the
NextJS
app.By interact I mean, retrieve data and be able to know if a user is currently logged in to the web app and if so retrieve the profile and/ or make authenticated calls to the API functions of the web app.
I guess what I am looking for is the flow you see in many current extensions nowadays, where if you log in on the website, you are automatically logged in on the extension, and if you log out from one of them, you should be logged out as well on the other.
Currently, my approach is when a user opens the extension, I make a request to
api/auth/session
to retrieve the session.When logged in, it actually works and I get the session data and when not logged it, it fails, which is expected.
With the session data, I can infer that a user is logged in.
In the session data, I add a custom
JWT
that I store temporarily in the state of the extension(not in local storage, just in a useState variable). When the extension popup is closed, the state is wiped, so i don't think this poses an issue security wise.I want to use the JWT to make calls to the API functions of the web app that would verify the JWT signature before allowing the handlers to receive/send the data back to the extension.
My questions are as follows:
api/auth/session
?Thanks in advance for your help on this matter.
Beta Was this translation helpful? Give feedback.
All reactions