4

我一直试图从这篇博客文章和这个源代码中找出如何在我的 ember.js 应用程序中正确实现 firebase 的 simpleLogin,但无济于事。

博客文章示例使用 github 作为身份验证提供程序,但我需要使用电子邮件和密码选项。到目前为止,我已经成功地将用户添加到有问题的 firebase 的注册用户中,并且可以返回 user.id 和 user.email,但我无法使用用户登录,因为它没有将用户添加到 firebase用户/路径。

AuthController.js

App.AuthController = Ember.Controller.extend({
    authed: false,
    currentUser: null,

    init: function() {
        this.authClient = new FirebaseSimpleLogin(dbRef, function(error, user) {
            if (error) {
                alert('Authentication failed: ' + error);
            } else if (user) {
                this.set('authed', true);
                var userRef = new Firebase(usersPath + '/simplelogin:' + user.id);
                var controller = this;
                var properties = {
                    id: user.id,
                    email: user.email
                };
                userRef.once('value', function(snapshot) {
                    var user = App.User.create({ ref: userRef });
                    user.setProperties(properties);
                    controller.set('currentUser', user);
                });
            } else {
                this.set('authed', false);
            }
        }.bind(this));
    },

    login: function() {
        this.authClient.login('password', {
            email: this.get("email"),
            password: this.get("password"),
            rememberMe: this.get("remember")
        });
    },

    logout: function() {
        this.authClient.logout();
    },

    createUser: function() {
        var email = createEmail.value;
        var password = createPassword.value;
        this.authClient.createUser(email, password, function(error, user) {
            if (!error) {
                console.log('User Id: ' + user.id + ', Email: ' + user.email);
            }
        }
    )}
});

创建用户模板

<form {{action createUser on='submit'}} class="form-horizontal" role='form'>
    <div class='form-group'>
        <label class="col-sm-5 control-label" for='createEmail'><p class='lead'>What's your email?</p></label>
        <div class="col-sm-7">
            {{input type="email" classNames="form-control" value=createEmail id="createEmail" placeholder="Don't worry, we won't spam you..."}}
        </div>
    </div>
    <div class='form-group'>
        <label class="col-sm-5 control-label" for='createPassword'><p class='lead'>Choose a password...</p></label>
        <div class="col-sm-7">
            {{input type="password" classNames="form-control" value=createPassword id="createPassword" placeholder="Make it super hard to guess!"}}
        </div>
    </div>
    <div class='form-group'>
        <label class="col-sm-5 control-label" for='confirm'><p class='lead'>And confirm it</p></label>
        <div class="col-sm-7">
            {{input type="password" classNames="form-control" value=confirm id="confirm" placeholder="Make sure you know what you typed..."}}
        </div>
    </div>
    <button type='submit'>Submit</button>
</form>

如果您需要查看我的更多代码,请告诉我,我会在此处提供。谢谢!

@kingpin2k 你有什么想法吗?

@mike-pugh 提供的答案:

我的 AuthController 现在看起来像这样:

App.AuthController = Ember.Controller.extend({
  authed: false,
  currentUser: null,

  init: function() {
    this.authClient = new FirebaseSimpleLogin(dbRef, function(error, user) {
      if (error) {
        alert('Authentication failed: ' + error);
      } else if (user) {
        this.set('authed', true);
        var userRef = new Firebase(usersPath + '/simplelogin:' + user.id);
        var controller = this;
        var properties = {
            id: user.id,
            email: user.email
        };
        userRef.once('value', function(snapshot) {
            var data = snapshot.val();
            var user = App.User.create({ ref: data});
            user.setProperties(properties);
            controller.set('currentUser', user);
        });
      } else {
        this.set('authed', false);
      }
    }.bind(this));
  },

    login: function() {
        console.log('made it inside login');
        console.log(email.value);       
        console.log(password.value);
        console.log(remember.value);
        this.authClient.login('password', {
            email: email.value,
            password: password.value,
            rememberMe: remember.value
        });
    },

    logout: function() {
        this.authClient.logout();
    },

    createUser: function() {
        console.log('made it inside createUser');
        var email = createEmail.value;
        console.log(email);
        var password = createPassword.value;
        console.log(password);
        this.authClient.createUser(email, password, function(error, user) {
            if (!error) {
                console.log('User Id: ' + user.id + ', Email: ' + user.email);
                var userRef = new Firebase(usersPath + '/simplelogin:' + user.id);
                userRef.set({
                    email: user.email,
                    id: user.id
                });
            }
        }
    )}
});

我还必须添加一个空的 UserModel:

App.User = EmberFire.Object.extend({

});

而已!!现在我可以进行身份​​验证,然后提取 currentUser 数据。

4

1 回答 1

2

The Firebase Simple Login functions don't add users to your users/ path within your forge. It stores the users in some system defined area. If you want to store data about your users within a users/ path then you'll need to create that information.

For example:

createUser: function() {
        var that = this;
        var email = createEmail.value;
        var password = createPassword.value;
        this.authClient.createUser(email, password, function(error, user) {
            if (!error) {
                console.log('User Id: ' + user.id + ', Email: ' + user.email);
                var userRef = new Firebase(usersPath + '/simplelogin:' + user.id);
                userRef.set({email: user.email}); // This is where you'd add Display Name, phone #, whatever

                that.authClient.login('password', email, password);
            }
        }
    )}

Your login code appears to correctly call the Firebase Simple Login code, which upon login should trigger the callback you specified in the constructor. Within that callback, I think you'll want to change your userRef.once() method to:

userRef.once('value', function(snapshot) {
                    var data = snapshot.val();
                    var user = App.User.create({ ref: data});
                    user.setProperties(properties);
                    controller.set('currentUser', user);
                });

The once() success callback provides you the snapshot of the data at that path, so you'd want to bind to that information instead of the userRef object itself which is just a Firebase reference.

于 2014-01-13T16:42:58.073 回答