AngularJS and Google OAuth2 redirect_uri AngularJS and Google OAuth2 redirect_uri angularjs angularjs

AngularJS and Google OAuth2 redirect_uri


The following worked for me, with html5 mode set to true in my app.

This code goes into your controller for the redirect page (make sure to inject the $window service):

//Get parameters from hash fragmentvar params = {};if ($window.location.hash.indexOf('#') === 0) {    var regex = /([^&=]+)=([^&]*)/g, m;    while (m = regex.exec($window.location.hash.substr(1))) {        params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);    }}//Validate params, store access token etc.if (!params.access_token) {    //...}

If your app doesn't recognise the non-# routes, make sure that your server has the rewrite engine enabled and that you redirect all relevant requests to your angular app main HTML file.


If you are using IIS, you can install URL Rewrite extension. Then update your web.config file. For example, below config will redirect everything to your Angular app and let it handle the routing.

<?xml version="1.0" encoding="utf-8"?><configuration>    <system.webServer>        <rewrite>            <rules>                <rule name="Angular Default" stopProcessing="true">                    <match url="(.*)" />                    <action type="Rewrite" url="index.html" logRewrittenUrl="true" />                </rule>            </rules>        </rewrite>    </system.webServer></configuration>