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

Here is my scenario:

  • The user did not log in and tried to access the settings / settings page.
  • My Settings controller recognizes based on $auth.isAuthenticated() != true that they are not logged in, and redirects them to / login
  • The user fills in their email messages and passwords and sends them.

What I would like to do in this third step then redirects them to the / settings page, and not to the home page.

I think I would change this variable:

$authProvider.loginRedirect = '/';

The problem is that I cannot include $authProvider in my loginCtrl.js file without getting the "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 enable it. This is 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); }); }; } 

Includes $authProvider in the controller even possible? If not, what is the alternative change solution when people are redirected when they log in using Satellizer?

Thanks.

+6
source share
3 answers

Typically, provider objects can only be accessed at configuration time, while controllers are created at run time. If you need to configure authProvider, try doing:

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

The new version (0.12.5) no longer uses these settings. 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); }); 
+1
source

I tried to do this and found that in version 0.13.0 (maybe earlier too?) You can pass the parameter to the login function as follows:

 $auth .login(user, { url: config.api + '/authenticate/customer' }) 
+1
source

Source: https://habr.com/ru/post/983755/


All Articles