So after reading through the posts here, I was finally able to make things work and want to pay it forward by laying out what I did.
Special thanks to
@jannik
as I use a lot of his code and ideas here. Also thanks to
@dhruv2204
for guiding me in implementing Auth0 inside a background page.
I’m building a
Popup Chrome Extension
using
React
and use Auth0 for authentication/authorization.
Whilst I’m using react, I imagine the logic should work if you’re using something else. I’m not using any chrome-extension boilerplate and just use good old
npx create-react-app
.
It is also worth noting that I already have
an existing webapp
.
I used the
auth0-react library
. Initially, I was encountering problems wherein loginWithPopup would close my popup(extension) immediately after authorization. When I would reopen I would reopen the popup I’m already logged out so I really couldn’t login. The only thing that prevented this behavior was if the chrome devtools was open. (setting
cacheLocation={'localStorage'}
did not fix this which was
@jannik
’s fix)
What I was finally able to do was:
Have my
Sign In button
just open up my webapp by calling
chrome.tabs.create({url: 'https://mywebapp.com/'})
Users can then just login there then reopen my popup extension afterwards.
Inside
App.js
I have this running
to check authentication
everytime someone opens the popup:
thank you for sharing your effort! I came across exactly the same issues as you. In the last week, I started to use a more maintainable way. I’m using now the “Options” page of a chrome extension to manage the whole login and logout process:
In this way, I can use the normal login with popup and logout without any issues. Here is a little code fragment:
I like this solution the most, as you can just work with the official auth0 react library and things just work. The user-flow is a little different, but I think that is worth it.
hope that helps as well!! Getting auth0 work in a react chrome extension is a hell of a work!
Thanks @jannik I was actually thinking about doing this but didn’t push through since I’m just making the scaffolding and have not started working on the final UX.
I might try out having the options page too if our design requires it.
Are you using a boilerplate btw? If you are, can you share what boilerplate you’re using? Thanks!
I can highly recommend using the options page to manage auth0 login/logout.
If you need to run Auth0 in Content Script or Background
Special thanks to @dhruv2204 for guiding me with this.
Use Manifest V2. Also Use @auth0/auth0-spa-js inside the background page and make the calls there.
The reason for this is because auth0 needs access to the DOM and background service workers (from manifest v3) do not have access to the DOM. Please not that manifest v2 will be deprecated soon. They just have not released an official deprecation date so for now this can work
Just use the chrome messaging API to call the the methods from content script if necessary. Doing this still works with the earlier mentioned methods.