javascriptangularjssatellizer

How to change Satellizer's $authProvider.loginUrl inside an Angular controller?


Here's my case scenario:

  1. User is not logged in, and they try to access a /settings page.
  2. My Settings controller recognizes based on $auth.isAuthenticated() != true that they aren't logged in, and redirects them to /login
  3. User fills out their email and password and hits submit.

What I would like to do on this third step is then redirect them to the /settings page, not the home page.

I'm thinking I would be changing this variable:

$authProvider.loginRedirect = '/';

The problem is that I cannot include $authProvider in my loginCtrl.js file without getting an "unknown provider" error in my console: https://docs.angularjs.org/error/$injector/unpr?p0= In other words, Angular does not recognize $authProvider when I try to include it. Here's what my loginCtrl.js file looks like:

/* Everything is blowing up because I'm trying to include $authProvider */
angular.module("PrayerChain")
    .controller("loginCtrl", ["$rootScope", "$scope", "$state", "$http", "$auth", "$authProvider", loginCtrl]);

function loginCtrl($rootScope, $scope, $state, $http, $auth, $authProvider) {
    $authProvider.loginRedirect = '/settings';
    $scope.login = function () {
        $auth.login({ Email: $scope.email, Password: $scope.password })
        .then(function() {

        })
        .catch(function (response, status, headers) {
            console.log(response);
            $scope.error = JSON.parse(response.data);
        });
    };
}

Is including $authProvider in a controller even possible? If not, what's an alternative solution to changing where people are redirected upon logging in using Satellizer?

Thanks.


Solution

  • Usually provider objects can only be accessed at config time, whereas controllers are created in runtime. If you need to setup the authProvider, try doing:

    angular.module('PrayerChain').config(
        [           "$authProvider",
            function($authProvider) {
                $authProvider.loginRedirect = '/settings';
            }
        ]).controller("loginCtrl",
        // ...