【问题标题】:Page didn't redirect in Angularjs页面没有在 Angularjs 中重定向
【发布时间】:2017-06-07 09:45:52
【问题描述】:

我已经使用 firebase 和 angularjs 创建了一个身份验证过程。

在认证过程之后,页面必须重定向到htmlnew.html 使用$routeprovider。但它不会在身份验证后重定向。

index.html

<html ng-app="appName">
 <body>
 <div ng-controller="loginCtrl" id="message">
    <a ng-click="googleSignin()">SignIn into App</a>
 </div>
   <p id="load">Firebase SDK Loading&hellip;</p>
 </body>
</html>

htmlnew.html

<body>
<div id="message" ng-controller="loginpage">
    <h1>Welcome</h1>
    <a ng-click="googleSignout()">Signout</a>
</div>
</body>

app.js

 var app = angular.module("appName", ["ngRoute", "firebase"]);
 app.config(function ($routeProvider) {
 $routeProvider
    .when("/", {
        templateUrl: 'public/index.html',
        controller: 'loginCtrl'
    })
    .when('/login', {
        templateUrl: 'public/htmlnew.html',
        controller: 'loginpage'
    })
  });

  app.controller('loginCtrl', function ($scope) {
  var provider = new firebase.auth.GoogleAuthProvider();
  $scope.googleSignin = function () {
  firebase.auth()
        .signInWithPopup(provider).then(function (result) {
    var token = result.credential.accessToken;
    var user = result.user;
    console.log(token);
    console.log(user);

  }).catch(function (error) {
    var errorCode = error.code;
    var errorMessage = error.message;
    console.log(errorCode);
    console.log(errorMessage);
    });
   }
 });

 app.controller('loginpage', function ($scope) {
 $scope.googleSignout = function () {
  firebase.auth().signOut()
        .then(function () {
            console.log('Signout Succesfull');
        }, function (error) {
            console.log('Signout Failed');
        });
   }
 });

项目文档

对于这个项目的演示检查link

提前致谢

【问题讨论】:

  • 页面加载后,您的演示在开发控制台TypeError: Cannot read property 'auth' of undefined 中出现错误。似乎firebase 没有正确注入。
  • 对不起,这是以前的副本。现在部署项目。立即检查。
  • 试试下面的答案。可以工作

标签: javascript angularjs firebase firebase-authentication


【解决方案1】:

使用$location 重定向到路径。将其注入控制器依赖项并使用如下

firebase.auth()
    .signInWithPopup(provider).then(function (result) {
        $location.path('/login');
})

【讨论】:

    【解决方案2】:

    对于成功signwithpopup 后的重定向,您应该在function(result) 中提供状态更改,如果出现错误,它将在function(error) 中捕获

    $location.path('/login);

    因为您正在使用 ngRoute。你可以使用$location.path 来路由你的状态。

    希望这会对你有所帮助。

    app.controller('loginCtrl', function ($scope,$location) {
      var provider = new firebase.auth.GoogleAuthProvider();
      $scope.googleSignin = function () {
      firebase.auth()
            .signInWithPopup(provider).then(function (result) {
        var token = result.credential.accessToken;
        var user = result.user;
        $location.path('/login');
        console.log(token);
        console.log(user);
    
      }).catch(function (error) {
        var errorCode = error.code;
        var errorMessage = error.message;
        console.log(errorCode);
        console.log(errorMessage);
        });
       }
     });
    

    【讨论】:

    • 你得到你的控制台令牌和用户了吗? @PrasathV
    • 登录完成,页面没有重定向到 htmlnew.html。在控制台中检查。 $$absUrl : "http://localhost:5000/#!/login"
    • 是的。代币:ya29.GlxiBL5ICWFYMX3DC8eQ_Phl1vuev1LP-6iaelDUfQFtSqTUmi4Mj3o1EgAcv4czW53jEvuIjICRL3i5MkjOibjGs0B69u2lP9kaqnerI25lk0E5pdYRtHifXL3oVw...
    • 当您登录时。您的网址是否更改为 /login ? @PrasathV
    • 不。没有更改为 /login @Himesh Suthar
    【解决方案3】:
    firebase.auth()
            .signInWithPopup(provider).then(function (result) {
    
        var token = result.credential.accessToken;
        var user = result.user;
    
         // You missed this $location.path('/pathYouNeed ');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2014-08-28
      相关资源
      最近更新 更多