Firebase简单登录+角度 - 每个用户拥有自己的数据

时间:2014-04-27 09:25:40

标签: javascript angularjs firebase

这是我的控制器。注册功能正常。登录没有,但我的问题是,我如何为每个用户创建数据?例如,我有用户john@doe.cz,UID是simplelogin:29,ID 29.每个用户都有自己的帐户,类别等。那么什么是最佳实践或如何实现?我想考虑制作own-firebase.firebaseio.com/users/(id,uid或某种哈希)/类别或帐户。

app.controller('AuthCtrl', function($scope, $ionicModal, $firebase, DataFactory){

    var auth = new FirebaseSimpleLogin(usersAuth, function(error, user) {
        if (error) {
            // an error occurred while attempting login
            console.log(error);
        } else if (user) {
            // user authenticated with Firebase
            console.log('User ID: ' + user.uid + ', Provider: ' + user.provider);
        } else {
            // user is logged out
        }
    });

    $ionicModal.fromTemplateUrl('views/auth/register.html', function(modal) {
        $scope.registerModal = modal;
    }, {
        scope: $scope,
        animation: 'slide-in-up'
    });

    $ionicModal.fromTemplateUrl('views/auth/login.html', function(modal) {
        $scope.loginModal = modal;
    }, {
        scope: $scope,
        animation: 'slide-in-up'
    });

    $scope.registerModalShow = function() {
        $scope.registerModal.show();
    };

    $scope.registerModalHide = function() {
        $scope.registerModal.hide();
    };

    $scope.loginModalShow = function() {
        $scope.loginModal.show();
    };

    $scope.loginModalHide = function() {
        $scope.loginModal.hide();
    };

    $scope.newUser = function(user){
        $scope.userEmail = user.email;
        $scope.userPassword = user.password;

        auth.createUser($scope.userEmail, $scope.userPassword, function(error, user) {
            if (!error) {
                console.log('User UID: ' + user.uid + ', Email: ' + $scope.userEmail + ', User ID: ' + user.id + ', User Password: ' + $scope.userPassword);
            }
        });

        $scope.userRef = new Firebase(url + '/users' + $scope.userPassword);
        $scope.registerModalHide();
        user.email = '';
        user.password = '';
        alert($scope.userId);
    };

    $scope.userLogin = function(user){
        $scope.userLoginEmail = user.email;
        $scope.userLoginPassword = user.password;

        auth.login('password', {
            email: user.email,
            password: user.password
        });
        $scope.loginModalHide();
        user.email = '';
        user.password = '';
    };
});

2 个答案:

答案 0 :(得分:1)

我正在使用以下方法为每个用户创建“自己的数据”:

我创建了一个新的个人资料,当有人第一次登录我的应用时,它可以包含我想要的任何细节:

$scope.login = function () { // This logic handle the addition of new users to my firebase.

    $rootScope.auth.$login('twitter') // Because of I'm using Twitter for login.
        .then(function (user) {
            $scope.myFirebaseRef.$child(user.uid).$set({name: user.displayName}); // Create new profile.
            console.log("Welcome " + user.displayName + "!" );
            $state.go('home');
        }, function (error) {
            consolle.log(error); 
        }
    );
}

然后,当我更改视图时执行此resolve

resolve: { // Basically, It's asking for a logged user and waiting for a promise before load the controller. 
  session: function($rootScope) {
    return $rootScope.auth.$getCurrentUser().then(function(user) {
      if (user) {
        $rootScope.someCurrentUser = user.uid;
      }
    });
  }
}

最后,我使用$rootScope.someCurrentUser创建个性化会话:

.controller('sessionController', ['$rootScope', '$firebase', function($rootScope, $firebase) {
    var someUserRef = new Firebase('https://myfirebase.firebaseio.com/' + $rootScope.someCurrentUser);
    $scope.sessionRef = $firebase(someUserRef);

    // Do something with/in your personalized session.  

}]);

我希望这种方法能为您提供一些创建每个用户的会话/自己数据的想法。

答案 1 :(得分:0)

最佳做法是REST API,因此您建议的网址格式基本适合。

您可以使用例如“谷歌休息用户管理API”搜索字符串给谷歌一些提示。字符串中的“谷歌”部分是为了找到特别是“谷歌”如何做到这一点。

Google如何塑造网址→https://developers.google.com/admin-sdk/directory/v1/guides/manage-users

检查其他提供商(例如现在abandoned“Mozilla Persona”→https://developer.mozilla.org/en-US/Persona)以获取更多灵感