# Ultimate Guide to Auth0 In a Chrome Extension Popup

**URL:** <https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362>\
**Category:** Get Help\
**Tags:** chrome-extension, api, firefox, login, auth0\
**Created:** [April 19, 2021, 3:03pm UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362 "2021-04-19T15:03:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lvillacin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lvillacin/32/12977_2.png) [@lvillacin](https://community.auth0.com/u/lvillacin)\
**Post date:** [April 19, 2021, 3:03pm UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/1 "2021-04-19T15:03:57Z")

</div>

I’ve been reading around how to implement Auth0 in a Chrome Extension but have not encountered any clear step by step guide on what to do. Is there anyone here that has successfully used Auth0 in a chrome popup with react that would be willing to guide us here? Thanks!

---

<div class="post-metadata">

**Author:** ![lvillacin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lvillacin/32/12977_2.png) [@lvillacin](https://community.auth0.com/u/lvillacin)\
**Post date:** [April 20, 2021, 1:15am UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/2 "2021-04-20T01:15:33Z")

</div>

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:

```auto
const getToken = async () => {
    try {
      const token = await getAccessTokenSilently()
     setToken(token)
    } catch (e) {
      console.log(e)
    }
  }

  useEffect(() => {
    getToken()
  }, [])

```

My **logic for switching the ui for authenticated user** is like this

```auto
const { isAuthenticated, getAccessTokenSilently, isLoading } = useAuth0()

const [authenticated, setAuthenticated] = useState(isAuthenticated)

useEffect(()=> {
    setAuthenticated(isAthenticated)
}, [isAuthenticated] )

return(
    <div>
        { isLoading ? <LoadingPage /> : !authenticated ? <SignInPage /> : <HomePage /> }
    </div>
)

```

The reason why we need to cast `isAuthenticated` from Auth0 to a custom `authenticated` state is because we need to manually `setAuthenticated(false)` on logout. @jannik explains the situation better here [Sync auth state between multiple applications (SPA & Chrome Extension) - #21 by jannik](https://community.auth0.com/t/sync-auth-state-between-multiple-applications-spa-chrome-extension/33994/21)

Finally, to **Logout** , this is what I do. This is basically straight from @jannik

```auto
const logoutHandler = () => {
    logoutHelper({ federated: true, returnTo: window.location.origin })
    fetch(`https://${auth0Domain}/v2/logout?federated=true&client_id=${auth0ClientId}`, {
      credentials: 'include',
      mode: 'no-cors'
    }).catch()
  }

  const logoutHelper = (options) => {
    logout(options)
    setAuthenticated(false)
  }

//some button in ui calls logoutHandler() onClick

```

Hope this helps. If you have suggestions please list it out here and just let me know. Thanks guys!

---

<div class="post-metadata">

**Author:** ![jannik](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jannik/32/20760_2.png) [@jannik](https://community.auth0.com/u/jannik)\
**Post date:** [April 20, 2021, 10:11am UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/3 "2021-04-20T10:11:14Z")

</div>

hey @lvillacin

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:  
 ![Screenshot 2021-04-20 at 11.55.10](https://global.discourse-cdn.com/auth0/original/3X/c/3/c38b0202e76703dca4d8ed314acf9382e4dcbde2.png)

In this way, I can use the normal login with popup and logout without any issues. Here is a little code fragment:

```auto
                                <Button
                                    id="loginButton"
                                    variant="contained"
                                    color="primary"
                                    onClick={loginWithPopup}
                                >
                                    {isAuthenticated ? 'Re-Authenticate' : 'Login'}
                                </Button>

```

The options page and the popup are sharing the same localstorage, that’s why that way works seamlessly.

My popup page is just showing a “settings” button, that is opening the options page in a new tab:

 ![Screenshot 2021-04-20 at 11.58.59](https://global.discourse-cdn.com/auth0/original/3X/9/0/9078c3ffebfb666ca86fe3d3571b38ef65ec87c6.png)

Here is the code for the settings button:

```auto
                                        <Button
                                            id="loginButton"
                                            variant="contained"
                                            color="primary"
                                            onClick={() => chrome.tabs.create({
                                                url: `${window.location.origin}/options.html`,
                                            })}
                                        >
                                            Start here
                                        </Button>

```

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!

---

<div class="post-metadata">

**Author:** ![lvillacin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lvillacin/32/12977_2.png) [@lvillacin](https://community.auth0.com/u/lvillacin)\
**Post date:** [April 20, 2021, 10:20am UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/4 "2021-04-20T10:20:35Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jannik](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jannik/32/20760_2.png) [@jannik](https://community.auth0.com/u/jannik)\
**Post date:** [April 20, 2021, 10:37am UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/5 "2021-04-20T10:37:09Z")

</div>

I can highly recommend using the options page to manage auth0 login/logout.

And my boilerplate is this one: [GitHub - lxieyang/chrome-extension-boilerplate-react: A Chrome Extensions boilerplate using React 18 and Webpack 5.](https://github.com/lxieyang/chrome-extension-boilerplate-react)  
Working pretty good!

---

<div class="post-metadata">

**Author:** ![lvillacin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lvillacin/32/12977_2.png) [@lvillacin](https://community.auth0.com/u/lvillacin)\
**Post date:** [April 20, 2021, 10:39am UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/6 "2021-04-20T10:39:42Z")

</div>

Thank you very much! Will check this out!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 20, 2021, 10:48am UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/7 "2021-04-20T10:48:30Z")

</div>

Teamwork makes the dreamwork!

---

<div class="post-metadata">

**Author:** ![lvillacin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lvillacin/32/12977_2.png) [@lvillacin](https://community.auth0.com/u/lvillacin)\
**Post date:** [April 25, 2021, 6:44am UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/8 "2021-04-25T06:44:52Z")

</div>

**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.

**Background.html**

```auto
<script src="./background.js"></script>

```

**Background.js:**

```auto
import { Auth0Client } from '@auth0/auth0-spa-js'

const auth0 = new Auth0Client({
  domain: <YOUR_AUTH0_DOMAIN>,
  client_id: <YOUR_AUTH0_CLIENT_ID>,
  redirect_uri: <YOUR_CHROME_EXTENSION_URI> //chrome-extension:extensionid
})

const getToken = async () => {
  const token = await auth0.getTokenSilently()
  return token
}

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 26, 2021, 8:39am UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/9 "2021-04-26T08:39:47Z")

</div>

Thanks for sharing that with the rest of community!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [May 11, 2021, 8:40am UTC](https://community.auth0.com/t/ultimate-guide-to-auth0-in-a-chrome-extension-popup/61362/10 "2021-05-11T08:40:37Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
