JamesRandall / AngularJS-OAuth2

Package for allowing an AngularJS application to authenticate with an OAuth 2 / Open ID Connect identity provider using the implicit flow.
MIT License
46 stars 42 forks source link

Use with ui-router #41

Open simo3o opened 7 years ago

simo3o commented 7 years ago

Is it possible to use the requireToken: true with ui-router?

SteveDowsett commented 7 years ago

I would like to know this too, can you use this with ui-router at all?

dtkern commented 7 years ago

Hi guys. I made a lot of changes for this to work with the latest version of IdentityServer4. I also created a Sign Up button option for my personal use.

I changed this part of code in order to use it with ui-router:

// Open ID Sign In directive
    angular.module('oauth2.directive', [])
        .config(['$stateProvider', function ($stateProvider) {
            $stateProvider
            .state('silent-renew', {
                name: 'SilentRenew',
                url: '/silent-renew',
                template: ''
            })
        }])

On function init() I changed:

$rootScope.$on('$stateChangeStart', stateChangeHandler);

and the "routeChangeHandler" function I changed for:

function stateChangeHandler(event, toState, toParams, fromState, fromParams, options) {
                    if (toState && toState.requireToken) {
                        if (!accessToken.get() || expired(accessToken.get())) {
                            event.preventDefault();
                            $window.sessionStorage.setItem('oauthRedirectRoute', toState.url);

                            if (scope.signUpMode) {
                                endpoint.signUp();
                            } else {
                                endpoint.authorize();
                            }
                        }
                    }
                };

Good luck. Let me know if it works for you.