DEV Community

Eissa Saber
Eissa Saber

Posted on Originally published at eissa.top

How I Fixed Google and Facebook SSO in a Mobile WebView

Originally published on my blog.

Google and Facebook block sign-in inside WebViews, and our Cordova app had been stuck on it for two years. The fix: sign in in the real browser, then come back to the app through a link.

The problem

The mobile apps I worked on were built with Cordova, so each app is a web page running inside a WebView. Google and Facebook sign-in used to work there, until both providers changed their rules and it stopped working. The bug was two years old, several attempts had failed, and one of our biggest clients made fixing it a condition of renewing.

Why it happens

Sign-in inside the WebView worked fine at first. Then Google and Facebook restricted SSO to the normal browser only, and blocked it inside WebViews. They did it on purpose: the app that hosts a WebView can read everything typed into it, including a password. So the providers refuse to show their login page there, and there's no setting to turn that off.

The only real fix is to not sign in inside the WebView.

The idea: copy Postman

Postman's desktop app already solves this. When you sign in, it opens your normal browser. You sign in there, and the browser sends you back to the app, signed in. I did the same thing on mobile.

The flow

  • The user taps the same SSO button as before.
  • The app opens the phone's real browser, not the WebView, on a small static index.html hosted on the web.
  • That page holds the Google and Facebook SSO code and handles the routing: it sends the user straight to Google or Facebook to sign in.
  • From there everything is automatic. After login, the redirect URL opens the app, with the credentials in the URL.
  • The app reads them from the URL's search params and continues its normal sign-in flow.

The static page is the only new piece on the web side, and it does one job: start the sign-in where Google and Facebook allow it, then send the user back. The app's existing login code didn't change. It just gets the credentials from the URL instead of the WebView.

// The SSO button opens the static SSO page in the real browser.
window.open(`https://example.com/sso/index.html?provider=${provider}`, "_system");

// That page signs the user in with Google or Facebook,
// then redirects to myapp://auth?token=...
// which opens the app and calls:
function handleOpenURL(url) {
  const token = new URL(url).searchParams.get("token");
  if (token) completeSignIn(token); // the existing flow
}
Enter fullscreen mode Exit fullscreen mode

Why mobile is harder than desktop

The idea is simple. Making it reliable on a phone takes more care:

  • The OS may close the app while the user is in the browser, so the link might start the app from scratch instead of resuming it. Both cases have to work.
  • The link back to the app has to be set up separately on Android and iOS.
  • The user can give up halfway, so tapping the button again has to be safe.

The result

Google and Facebook sign-in worked again, with the same single tap. A two-year-old blocker was fixed by borrowing an idea from a desktop app.

If you build this today, pass a short-lived one-time code in the URL instead of real credentials, and have the app exchange it with your server.


I write about web development, Linux and IoT at eissa.top. This post first appeared there.

Top comments (0)