Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 951
Martyn Chamberlin Avatar asked Mar 13 '15 15:03

Martyn Chamberlin


3 Answers

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",
    // ...
like image 124
Austin Mullins Avatar answered Nov 09 '22 16:11

Austin Mullins


The new version (0.12.5) are not using this settings anymore. You need to set the url inside your controller

$auth.login({ Email: $scope.email, Password: $scope.password })
    .then(function() { 
        $location.path('your-new-route');
    })
    .catch(function (response, status, headers) {
        console.log(response);
        $scope.error = JSON.parse(response.data);
    });
like image 1
André Bueno Avatar answered Nov 09 '22 14:11

André Bueno


I was looking to do this and found that in version 0.13.0 (maybe earlier too?) you can pass an options parameter to login function like this:

$auth
  .login(user, {
    url: config.api + '/authenticate/customer'
  })
like image 1
JavaScript Warrior Avatar answered Nov 09 '22 15:11

JavaScript Warrior