Ionic和Firebase自动登录

Ionic and Firebase auto login

本文关键字:登录 Firebase Ionic      更新时间:2023-09-26

自从firebase进行了主要更新以来,我几乎不可能找到基本问题的答案。甚至使用他们的文档。

我的问题是我如何允许我的用户在我的应用程序上登录一次,并无限期或一段时间保持登录?

一旦我有一个用户登录,我如何访问他们的数据?比如姓和名?是否需要创建数据库并链接到userID ?

我也在努力理解如何"firebase.auth()。signInWithEmailAndPassword(email, password)"工作,因为我没有提供它我的firebase url。它是从索引页上的配置对象中拉出来的吗?

下面是我的angular代码:

appControllers.controller('userCtrl', ['$scope', '$rootScope', '$ionicPlatform', '$cordovaDevice', '$mdToast', '$mdBottomSheet', '$timeout', '$stateParams', '$state', 'LogIn', 'SignUp', '$http', '$firebaseAuth',
  function($scope, $rootScope, $ionicPlatform, $cordovaDevice, $mdToast, $mdBottomSheet, $timeout, $stateParams, $state, LogIn, SignUp, $http, $firebaseAuth) {
    var devid;
    $scope.id = "1";
    $scope.errorMsg = "";
    // timeout to get device ID
    $timeout(function() {
      document.addEventListener("deviceready", onDeviceReady, false);
      function onDeviceReady() {
        console.log(device.cordova);
        $scope.id = $cordovaDevice.getUUID();
        return $scope.id;
      }
    }, 1000);
    // watches change in DEVID
    $scope.updateID = function() {
      devid = $scope.id;
      console.log(devid);
      return devid;
    };
    $scope.initialForm = function() {
      $scope.moveBox = function() {
          // $("#signupbox").fadeOut();
          $('#signupbox').animate({
            'marginTop': "+=170px" //moves down
          });
          $timeout(function() {
            $state.go('app.signup');
          }, 500);
          $timeout(function() {
            $('#signupbox').animate({
              'marginTop': "-=170px" //moves down
            });
          }, 1000);
        } // end animate
      // Toast for empty Fields
      $scope.showAlert = function(menuName, time) {
          //Calling $mdToast.show to show toast.
          $mdToast.show({
            controller: 'toastController',
            templateUrl: 'toast.html',
            hideDelay: time,
            position: 'top',
            locals: {
              displayOption: {
                title: menuName
              }
            }
          });
        } // End showToast.
      // check LogIn
      var em, pw;
      $scope.user = {};
      $scope.updateEmail = function() {
        em = $scope.user.email;
        console.log(em);
        return em;
      };
      $scope.updatePass = function() {
        pw = $scope.user.pass;
        console.log(pw);
        return pw;
      };
      // Password Validation
      $scope.validatePass = function(ppw) {
        if (ppw.length < 8) {
          $scope.errorMsg = "Password must be at least 8 characters long";
        }
      };
      // start login
      $scope.logInNow = function() {
        var sdata = {
          em: em,
          pw: pw
        };
        if (pw == "" || pw == null) {
          $scope.errorSignIn = "Fields cannot be blank";
        }
        // FIREBASE LOGIN
        else {
          firebase.auth().signInWithEmailAndPassword(sdata.em, sdata.pw)
            .then(function(authData) {
              console.log("Logged in as:", authData.uid);
              $state.go('app.types');
            }).catch(function(error) {
              console.error("Authentication failed:", error);
              $scope.errorSignIn = "Email or Password is invalid";
            });
        }
      }
    };
    $scope.initialForm();

  }
]);

  1. 永远保持登录是默认行为,但这都是关于cookie的,所以如果Ionic清除cookie,用户就会注销。我不喜欢离子,所以我猜你需要弄清楚它是否和离子有关。相关Ionic博客文章。如果您想在某个时间点注销用户,您可以通过设置计时器并在X时间过后注销用户来做到这一点。注意这是一个客户端操作。

  2. 在我的web应用中,我做了这样的事情:

    firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
      $scope.photoURL = user.photoURL;
      $scope.name = user.displayName;
      $scope.logged_in = true;
    } else {
      $scope.photoURL = "";
      $scope.name = "";
      $scope.logged_in = false;
    }
    });
    

它工作,因为每次用户进入应用程序的状态是变化的,我总是有正确的数据(Firebase用户界面)。我记得还有另一种制作方法,但是我遇到了一些问题。

  • Firebase首先配置,然后初始化,你在应用程序上做的每个调用都是为你的项目配置的。所以是的,它是从配置中提取的。
  • 顺便说一下。您是否正在使用像https://firebase.google.com/docs/这样的新文档。