How we use a popup for Google and Outlook OAuth
Tags: tech, webdev, javascript, react
Published: 2019-05-09 14:49:04

---
title: How we use a popup for Google and Outlook OAuth
published: true
tags: Tech,Web Dev,JavaScript,React
canonical_url: https://blog.squarecat.io/how-to-oauth-popup
cover_image: https://images.unsplash.com/photo-1486312338219-ce68d2c6f44d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ
---

At [Leave Me Alone](https://leavemealone.xyz) we use Google and Microsoft OAuth for user sign in. To do this we redirect the user to the relevant login page, the user input their details, and is then directed back to our site and logged in. An unfortunate result of this is that our analytics report a great deal of referral traffic coming from “accounts.google.com” and “login.microsoft.com”.

![How we use a popup for Google and Outlook OAuth](https://blog.squarecat.io/content/images/2019/04/login-popup.jpg)

To solve this problem, instead of redirecting it would be better if we could open a new window or popup for the OAuth flow. Also this is probably a better experience for the user than being redirected around.

# How we implemented it

We use [Passport](http://www.passportjs.org/) for our authentication so when the user is directed back to our app after signing in the URL contains some parameters we need, including a token we use to authenticate them on our server.

Since we want to use a popup we need an additional step in the middle of the flow to catch that redirect, retrieve the URL parameters, close the popup, and use the token in the opening window (not the popup).

![How we use a popup for Google and Outlook OAuth](https://blog.squarecat.io/content/images/2019/04/oauth-popup.png)

We allow users to sign in with Google and Outlook and the implementation is the same for both. For ease of reading the example will use Google.

### Step 1: Open the popup

To open a new window we use [Window.open()](https://developer.mozilla.org/en-US/docs/Web/API/Window/open) with the passport login URL (/auth/google in our case) which opens the “Sign in to Leave Me Alone with Google” page in a new window. We also give the window a name and pass the [requested features](https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Window_features) we want it to have.

We assign the window reference and record the previous URL so that the same window will be used or focused if the user tries to click the sign in button again, even if it’s for a different provider. We don’t want two popups for different providers floating around causing confusion.

Finally, we add an event listener for messages as the popup is going to send the URL parameters and auth token when it’s finished.

{% gist https://gist.github.com/dinkydani/f9d8dd08b0e9a5299e11cd2a1f67f81d.js %}

_To get the window to open as a popup instead of a new tab we had to request the features `menubar=no,toolbar=no`._

### Step 2: Get the OAuth callback parameters in the popup

When the OAuth flow is complete Google redirects the user to a callback URL. Usually this would be a server route which would perform the Passport authentication. Since the auth is happening in a popup we use a page in our app, which when which when loaded grabs the search parameters and sends them to the parent.

This callback page uses a [React Use Effect Hook](https://reactjs.org/docs/hooks-effect.html) which executes when the page loads. We fetch the URL parameters which will include the auth token and send them to the opening window (the parent) using [Window.postMessage()](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage).

{% gist https://gist.github.com/dinkydani/64cefb99bb365139028e968ab67c3899.js %}

### Step 3: Authenticate the user and redirect to the app

The OAuth flow is almost complete the and the popup is now closed, we just need to authenticate the user on our server.

The receive message function needs to check the origin of the message to make sure it’s from the same domain for security. While coding this we realised that several Chrome developer tools use postMessage() from the same origin so we also check the source before trying to extract the payload.

Once we have the OAuth parameters, we redirect to the user to our own authentication endpoint o that we can use Passport to authenticate and login.

{% gist https://gist.github.com/dinkydani/b9b534ce00186436442b5a5996ee87dd.js %}

### Finished!

The process is quite simple and all we have done here is add an intermediary step in the OAuth flow to pass through the callback parameters.

There are probably lots of implementations but this was the quickest and simplest for us using React.js.

* * *

Hopefully this helped you or provided some inspiration for your own solution.

[Let us know](https://twitter.com/leavemealoneapp) if you have any questions or suggestions!