EmberFire认证和ToriiFirebaseAdapter [英] EmberFire Authentication and ToriiFirebaseAdapter

查看:116
本文介绍了EmberFire认证和ToriiFirebaseAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了EmberFire 指南设置与torii提供商的身份验证,它的工作完美。在用户进行身份验证后,我希望将用户数据存储在防火墙中,并在整个应用程序中访问用户对象。

I've followed the EmberFire guide to setting up authentication with the torii provider and it works perfectly. I'm looking to store the users data in firebase after the user has been authenticated and to have access to the user object throughout the application.

我以前已经完成了ember一会儿,但我不确定如何使用新的torii提供商。

I have previously done this with ember awhile back but i'm unsure how to do it using the new torii provider.

我想我应该测试用户已经存储在firebase中初始化功能,然后将用户注入控制器/路由。

I'm thinking i should test wether the user is already stored in firebase in an initialiser function and then inject the user into the controllers/routes.

任何帮助我指向正确的方向将是有用的或一些示例代码。

Any help pointing me in the right direction would be helpful or some example code.

谢谢

推荐答案

您需要一个Torii适配器来实现 / code>和 fetch 并执行查找 / 保存到您的应用程序特定的用户模型。我们的 ToriiFirebaseAdapter 不这样做, 。我已经走了,一个人为你工作:

You need a Torii adapter that implements open and fetch and does a find/save to your app specific user model. Our ToriiFirebaseAdapter does not do this, yet. I've gone ahead and knocked one up for you that will work:

// app/torii-adapters/application.js

import Ember from 'ember';

export default Ember.Object.extend({
  firebase: Ember.inject.service(),
  store: Ember.inject.service(),

  /**
   * Executed after Firebase authentication.
   *
   * Find or create the user based on the Firebase `authData`
   *
   * @param  {Object} authData
   * @return {Promise<Object>} Updated session info
   */
  open(authData) {
    return this._findOrCreateUser(authData)
      .then((user) => {
        return { currentUser: user };
      });
  },

  /**
   * Fetch an existing Firebase auth session and place into `session.currentUser`
   *
   * @return {Promise<Object>} Updated session info
   */
  fetch() {
    let ref = this.get('firebase');
    let authData = ref.getAuth();

    if (!authData) {
      return Ember.RSVP.Promise.reject(new Error('No Firebase session found'));
    }

    return this._findOrCreateUser(authData)
      .then((user) => {
        return { currentUser: user };
      });
  },

  /**
   * Teardown a session. Remove the `session.currentUser`.
   *
   * @return {Promise<Object>} Updated session info
   */
  close() {
    this.get('firebase').unauth();
    return Ember.RSVP.Promise.resolve({ currentUser: null });
  },

  /**
   * Find the user with the given `authData`, create if not found
   *
   * @param  {Object} authData
   * @return {Promise<Object>} The user
   */
  _findOrCreateUser(authData) {
    let store = this.get('store');

    return store.find('user', authData.uid)
      .catch(() => {
        let newUser = store.createRecord('user', this.extractUserProperties(authData));

        return newUser.save();
      });
  },

  /**
   * Extract the user properties from `authData` that you care about.
   *
   * @param  {Object} authData
   * @return {Object} An updated property hash
   */
  extractUserProperties(authData) {
    var name = 'Unknown';
    var provider = authData.provider;
    var userData = authData[provider];

    if (userData.displayName) {
      name = userData.displayName;
    } else if (userData.username) {
      name = userData.username;
    }

    return {
      id: authData.uid,
      name: name,
      email: userData.email || null
    };
  }
});

所有您需要做的是更新 extractUserProperties 方法来获取您关心的属性到您的用户模型的正确位置 - 所有人都以不同的方式实现其用户模型。

All you need to do is update the extractUserProperties method to get the properties you care about into their correct place on your user model - everyone implements their user model differently.

现在您应该能够查找 session.currentUser ,它将返回与登录用户对应的Ember Data模型。

Now you should be able to look up session.currentUser and it will return an Ember Data model that corresponds to the logged in user.

希望有所帮助。我们正在将其添加到网站文档中,并将尝试找到一种将其推送到EmberFire提供的ToriiFirebaseAdapter 的方法。

Hope that helps. We are in the process of adding this to the website documentation and will try to find a way to roll this into the EmberFire supplied ToriiFirebaseAdapter.

这篇关于EmberFire认证和ToriiFirebaseAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆