6

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.

Martyn Chamberlin
  • 1,277
  • 14
  • 17

3 Answers3

3

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",
    // ...
Austin Mullins
  • 7,307
  • 2
  • 33
  • 48
  • 1
    Perfect Austin. That cut the mustard. Thanks! – Martyn Chamberlin Mar 13 '15 at 15:55
  • One quick question here though. I need to grab the redirect from the URL using $stateParams, but that isn't available at config time. How on earth is Satellizer expecting you to be able to do this?? – Martyn Chamberlin Mar 13 '15 at 16:25
  • That's a great question. I don't actually know anything about Satellizer yet, but I will search with you. – Austin Mullins Mar 13 '15 at 16:27
  • Thank you! Here's the un-minified Satellizer library: https://raw.githubusercontent.com/sahat/satellizer/master/satellizer.js Satellizer has a `set` and a `get` method for the `loginRedirect` variable but I don't think it's externally available. I could be wrong though... I still have a lot to learn about Javascript. – Martyn Chamberlin Mar 13 '15 at 16:32
  • If we can't get it figured out, I'm going to post this as a new Issue at Github, because I've got to believe I'm not the only one who needs this... – Martyn Chamberlin Mar 13 '15 at 16:34
  • I agree. File an issue. It looks like it wouldn't be hard to add a parameter to login to implement dynamic redirect. I'll probably submit a PR myself if no one beats me to it. – Austin Mullins Mar 13 '15 at 16:56
  • Here we go: https://github.com/sahat/satellizer/issues/319 That explains the issue more or less, I think. – Martyn Chamberlin Mar 13 '15 at 17:12
1

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);
    });
André Bueno
  • 89
  • 1
  • 3
1

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'
  })