GitHub App and OAuth ~ Practical Kick-Starter
Enhance your GitHub OAuth implementation by using a popup approach for authentication in single-page applications (SPAs). This guide walks you through the process of spawning a popup for user authentication and securely handling the access token.
In a previous post we have implemented the GitHub OAuth flow from scratch. However, the redirection to the authentication page was via hard redirection, which is definitely not wanted behavior especially when you're dealing with a SPA.
Not surprisingly OAuth in the wild use a popup approach. Easy to solve, the redirection will still happen but in a spawned popup that, before dissolving will pass the access_token
back to main tab.
This post is to be read as an enhancement of the previous one, so it has a slightly higher pacing. If in doubt, please refer to the accompanying code or the previous post .
we assemble the GitHub OAuth authorization link and attacch it to the <a>
tag for accessibility reason. However, we also listen for click on it, preventing the default hard redirection.
Next we are going to use the Web API window.open to spawn the popup.
It expect as third parameter a string containing width
, height
and more.
Personally, I prefer the explicity of an object that is then converted to the above mentioned string.
top
andleft
properties have theauto
value - that's not in the specifications of the API, in fact it is interpreted by the following snippet as an instruction to place in the center of the relative axis. Basically if both areauto
the popup will always spawn in the center, even if you changewidth
orheight
.
Thus, the user clicks your fancy GitHub button and authenticate via the popup. But it is important to instruct the server to send back to some page whose function is:
- Ensure it is a popup
- Extrapolate
access_token
from query params - Dispatch it to parent window (
window.opener
) - Close itself
**Remember: OAuth callback Once the user gets authenticated, GitHub redirects to the Callback URL specified at OAuth App/GitHub App creation. This is addressed in more detail in the previous post.
The client gets redirected to /popup
and popup.html
is shown.
window.opener
is null
if the page has not been opened via the window.open
. This way, if the user directly routes to /popup
, gets redirected to /
.
The computation is minimal, it should be pretty fast. Showing a spinner though can only do you good.
Tip SPA and their router solutions offers some
beforeEnter
method that can be associated with a route; well, you could check thewindow.opener
value in there, in order to provide an even better experience.
Almost done! The popup is dashing the access_token
back to the parent but it is not listening!
As a precaution ignore any message coming from another origin. Please, consider removeEventListener
in your code.
Save the access_token
somewhere. From this point the flow rejoins the previous post .
There's nothing stopping you from using this pattern for GitHub App installation and permissions changes as well.
Related Posts
Contacts
On This Page