Hey there supertokens community πŸ‘‹ I stumbled upo...
# support-questions-legacy
d
Hey there supertokens community πŸ‘‹ I stumbled upon supertokens and am pretty excited to use it in a side project of mine! TL;DR:
getRoutingComponent()
does not render in chrome extension, but does when run via
npm start
or
npm run build && serve -s {{path}}
My current project involves a chrome extension running a react app in its popup. It is in this react app that I would like users to register or login using supertokens. To this end, I followed your getting started guide. As a minimal example, I simply initialize
SuperTokens
and call
getRoutingComponent()
, which IIUC, returns an element that renders the login/registration UI. That being said, when I use
npm start
or
npm run build && serve -s {{path}},
I do see the login UI being rendered. When turning on debug logs and logging the return value of
getRoutingComponent()
, I do see
SuperTokens
being initialized as well as the return value of the function call not being
null
. Instead, I can see it is of type
react.element
with a props
path
of
index.html
and
supertokensInstance
being the supertokens instance. This led me to the conclusion that soemthing's wrong with my configuration since it's working with both the devserver and as a production build being served. I've searched for similar use cases and indeed found some who were using supertokens in a chrome extension but nothing comparable here
r
hey @deezy5456
@porcellus can help here
d
πŸ™‰ 😍 thanks a bunch
p
hi
d
I'm currently debugging a bit and it seems like the app is running within the url of
chrome-extension://{{ either random string or some hash }}
, whereas the value of websitedomain I used to initialize SuperTokens was referring to a specific website
p
yeah, that's what I wanted to say, it's likely an issue with url.
Also, that random string is not really random I think, it's the id of the extension so it should be stable
d
Just tried it out and am now getting an error from
normalizedURLDomain.js
complaining this is not a valid URL
How did you solve that @porcellus ?
p
hmm, I think there is no way around that with the released version :/
also, I'm not sure if that's the root of problem
so what does the "does not render" part mean here? you go to the
/auth
route in the extension and it doesn't show up?
r
I think you can hack around this by overriding the window handler object in the frontend supertokens.init
There should be a function which allows you to get the current url and in that you can modify the chrome:// part and replace it with http:// and maybe then it will work
@porcellus @deezy5456
p
that's true, there's always a way around almost everything in js πŸ™‚
still, if the problem is rendering the auth component, and not redirecting to it, then the issue may be something different
r
Hmm. Maybe
Try this method though.
d
> There should be a function which allows you to get the current url and in that you can modify the chrome:// part and replace it with http:// and maybe then it will work Funny enough, I already had a look at this but since I've admittedly invested less than an hour into setting this up and debugging, I figured I'd first go through the code and understand what the domain really does. At least at first glance, I thought this might be used for CSP and CORS validation, so I didn't follow through on that thought (yet)
Re: not redering - this has been solved. I figured simply returning the turn value of the method would render the login UI, but it seems I was wrong.
I got that working πŸ™‚
r
Ok great! So it’s working now?
d
Yes, it's rendering, now I'll just have to figure out how to perform the callbak to the extension πŸ™ƒ sorry for my blatant stupidty. I just have never dealt with chrome extensions before and am discovering my unknown unknowns
Seems like
chrome
offers an identity api to deal with the shortcomings of running in a browser extension, generating a ephemeral URL for the extension, but I'd really like to use supertokens for this projects. In case you have any guide or someone that has already set this up, lmk.
r
Sure. Will let you know In case we have something that can help you πŸ™‚
d
Great thanks!
d
hey @deezy5456 , did you end up figuring a way to callback to the extension?
d
Not really :/ I finally resorted to chromeβ€˜s identity API which features a proxying callbacks back to the extension
d
Managed to get it working! Adding an explanation in case anyone is doing something similar to us. I add http:// to the start of my callback url (chrome-extension://...). When this callback url is returned after successfully logging in, the : (colon) in chrome-extension:// is removed. So in my chrome extension, I run the following code which checks the tabs for this url, and updates it if it matches.
Copy code
// This event is fired each time the URL of a tab changes
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
      let newUrl = changeInfo.url;
      if (newUrl.includes('http://chrome-extension//<id>/options.html')) {
        const stringSplit = newUrl.split('?')
        const queryParams = stringSplit[1]
        chrome.tabs.update(tabId, {url: `chrome-extension://<id>/options.html?${queryParams}`})
      }
      console.log(newUrl)
  });
Once logged in, I check if the session exists. If it exists, I update the url to remove the query parameters.
It's a bit hacky, and will try to figure out a way to only run the listener under certain conditions
r
hey @djarran thanks for this. Would you be so kind enough to make an issue about this in our supertokens-auth-react SDK and also answer the issue with the above explanation? Github issues are way easier to find than discord conversations.
10 Views