Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS App Authentication with CAS - XHR Request

Tags:

php

angularjs

cas

I am currently creating an angular web app that requires authentication for certain parts, but no the whole thing.

I have to use a CAS authentication page from an external party and cannot figure out how to meld the two together.

Normally, I simply include the phpCas::ForceAuthentication method to enable auth, but since the call is coming from a partial view, it is an XHR request and thus cannot redirect to the CAS page (Cross Domain Error).

I dabbled with solutions involving the authentication on the main page that would be triggered only sometimes, redirect from main services and a few others, but nothing has worked yet.

If need be, I can go into more detail on the exact setup I have if it is relevant.

Thanks!

like image 584
Ty Rozak Avatar asked Jun 03 '14 15:06

Ty Rozak


1 Answers

This is what i do, i check on each route change if the requested view required authentication, and if so a redirect to a login page. Here is some of my code:

Check if view requires authentication:

app.run(['$rootScope', '$state', 'UserFactory',
    function ($rootScope, $state, UserFactory) {
        $rootScope.$on("$stateChangeStart", function (event, toState) {
           if (toState.authenticate && !UserFactory.IsAuthenticated()) {
                $state.transitionTo("login");
                event.preventDefault();
            }
        });
    }
]);

View configuration:

    $stateProvider.state({
        name        : 'stateName',
        url         : '/path/',
        templateUrl : 'public/partials/template.html',
        controller  : 'SomeController',
        authenticate: true //or false if authentication is not required
    })

Hope this helps you, goodluck

like image 137
Chancho Avatar answered Sep 27 '22 21:09

Chancho