【问题标题】:Strange redirects in AngularJSAngularJS 中的奇怪重定向
【发布时间】:2017-03-07 20:50:54
【问题描述】:

我目前正在开发一个使用 AngularJS 使用 Firebase 日志系统制作的应用程序。当我尝试登录时,我有一个奇怪的重定向 - 无论登录是否成功,它都会出于某种原因重定向到注册页面。如果警报应该出现,它会显示在注册页面上。 尝试删除注册页面的路线 - 没有运气。尝试删除触发 OnAuthStateChanged 时发生的操作 - 仍然重定向。尝试调试应用程序-似乎该功能

for (var i = 0; i < eventFnsLength; i++) {
  if (!event.isImmediatePropagationStopped()) {
    handlerWrapper(element, event, eventFns[i]);
  }
}

在第 3532 行的 angular.js 中导致了这种情况,但我不知道如何让它停止。

SignInCtrl.js

angular.module('szybkiePisanie')
.controller('SignInCtrl', ["$scope", "$firebaseAuth", function ($scope, $firebaseAuth) {
    $scope.SignIn = function (event) {
        if ($scope.user.email != undefined) var email = $scope.user.email; else { alert("Wpisz adres e-mail"); return; }
        if ($scope.user.password != undefined) var password = $scope.user.password; else { alert("Wpisz hasło"); return; }
        firebase.auth().signInWithEmailAndPassword(email, password).catch(function (error) {                
            var errorCode = error.code;
            var errorMessage = error.message;

            if (email.length < 4) {
                alert('Proszę wpisać adres email.');
                return;
            }
            if (password.length < 4) {
                alert('Proszę wpisać hasło.');
                return;
            }
            ;
            if (errorCode === 'auth/wrong-password') {
                alert('Błędne hasło.');
            }
            else if (errorCode === 'auth/invalid-email') {
                alert('Nieprawidłowy adres email');
            }
            else if (errorCode === 'auth/user-disabled') {
                alert('Użytkownik zablokowany');
            }
            else if (errorCode) {
                alert(errorMessage);
            }
        })
    }
}]);

注册Ctrl.js

angular.module('szybkiePisanie')
.controller('RegisterCtrl', ["$scope", "$firebaseAuth", function ($scope, $firebaseAuth) {
    $scope.Register = function (event) {
        if ($scope.user.email != undefined) var email = $scope.user.email; else { alert("Wpisz mejl"); return; }
        if ($scope.user.password != undefined) var password = $scope.user.password; else { alert("Wpisz haslo"); return; }
        firebase.auth().createUserWithEmailAndPassword(email, password).catch(function (error) {
            var errorCode = error.code;
            var errorMessage = error.message;

            if (email.length < 4) {
                alert('Please enter an email address.');
                return;
            }
            if (password.length < 4) {
                alert('Please enter a password.');
                return;
            }
        })
    }
}]);

script.js(路由)

angular.module('szybkiePisanie', ['ngRoute', 'firebase'])
.config(['$routeProvider',
    function ($routeProvider) {
        $routeProvider
            .when('/home', {
                templateUrl: 'Home/home.html',
                controller: 'HomeCtrl'
            })

            .when('/practice', {
                templateUrl: 'Practice/practice.html',
                controller: 'PracticeCtrl'
            })

            .when('/signin', {
                templateUrl: 'SignIn/signin.html',
                controller: 'SignInCtrl'
            })

            .when('/profile', {
                templateUrl: 'Profile/profile.html',
                controller: 'ProfileCtrl'
            })

            .when('/register', {
                templateUrl: 'Register/register.html',
                controller: 'RegisterCtrl'
            })

            .otherwise({
                redirectTo: '/home'
            })
    }]);

编辑: 重定向不仅是由 Singing in 引起的。事实上,任何按钮或下拉菜单都会导致我的应用程序从“/singin”重定向到“/register”。奇怪。

【问题讨论】:

  • 你能提供一个小提琴来让我们重现这个吗?
  • 我无法重现任何重定向。由于我没有任何有效的电子邮件通行证,我尝试了一封带有任何密码文本的有效电子邮件,但我只收到一个导致 400 错误的请求,因为身份验证失败。但我没有看到任何重定向

标签: javascript angularjs firebase firebase-authentication


【解决方案1】:

所以我找出了导致重定向的原因。我还没有结束我网站上的“a”标签,网站的其余部分都充当了链接。

【讨论】:

    【解决方案2】:

    您只指定了两个可能的路由:/signin 和 /register。因此,路由器将仅支持这些路由之一,您最终将被重定向到其中之一。问题可能是您尚未定义登录状态的路由,该路由应该是默认路由。据我所知,在使用角度路由器时,您需要定义您想要支持的每条路由。

        angular.module('szybkiePisanie', ['ngRoute', 'firebase'])
        .config(['$routeProvider',
            function ($routeProvider) {
                $routeProvider
                    .when('/signin', {
                        templateUrl: 'SignIn/signin.html',
                        controller: 'SignInCtrl'
                    })
    
                    .when('/register', {
                        templateUrl: 'Register/register.html',
                        controller: 'RegisterCtrl'
                    })
    
                    .when('/', {
                        templateUrl: 'Home/home.html',
                        controller: 'HomeCtrl'
                    })
    
                    .otherwise({
                        redirectTo: '/'
                    });
            }]);
    

    【讨论】:

    • 我实际上在我的代码中包含了它——为了清楚起见,我没有在这里包含它。不过我会编辑它。
    猜你喜欢
    • 2019-07-06
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2011-08-05
    • 2015-12-13
    相关资源
    最近更新 更多