简体   繁体   English

Firebase 3.0 + Ember 2.0:Torii 适配器必须实现“open”才能打开会话

[英]Firebase 3.0 + Ember 2.0: The Torii adapter must implement `open` for a session to be opened

I'm having an issue with facebook authentication with the torii adapter, the error being: 'The Torii adapter must implement open for a session to be opened'.我在使用 torii 适配器进行 facebook 身份验证时遇到问题,错误是:“Torii 适配器必须实现open才能打开会话”。

I've visited many tutorials, and tried all presented methods, but most of them are usually old ember code and none of them seem to actually work.我访问了许多教程,并尝试了所有提供的方法,但其中大多数通常是旧的 ember 代码,而且它们似乎都没有实际工作。

Current state: I am able to login, I get the facebook popup and I can authorize.当前状态:我可以登录,我得到了 facebook 弹出窗口,我可以授权。

Using fiddler, I can also see the response from the API containing a JSON response with all credentials from the user I authenticated with.使用 fiddler,我还可以看到来自 API 的响应,其中包含一个 JSON 响应,其中包含来自我进行身份验证的用户的所有凭据。 In the firebase console, I can see the authorized user, reset its password, deny access,...在 Firebase 控制台中,我可以看到授权用户、重置密码、拒绝访问、...

All this leads me to believe that it's 'merely' a front-end issue where I can't seem to establish a proper 'session' to work with.所有这一切让我相信这“仅仅是”一个前端问题,我似乎无法建立一个合适的“会话”来处理。

My end goal would be to pick up the relevant user data and transfer it to my firebase backend as a 'user' entry, allowing for quick registration for site visitors, but I'll be more than glad to have an active session so I can work out the rest on my own.我的最终目标是获取相关的用户数据并将其作为“用户”条目传输到我的 Firebase 后端,从而允许网站访问者快速注册,但我很高兴有一个活跃的会话,这样我就可以剩下的自己解决。

As a front-end rookie (I normally code C#), Ember may not have been the best choice to get the hang it, but I'm this far now, I'm not about to let it all slide and pick up a different framework.作为前端菜鸟(我通常编写 C#),Ember 可能不是解决问题的最佳选择,但我现在已经走到这一步,我不打算让它全部溜走并选择一个不同的框架。

My code:我的代码:

config/environment.js配置/环境.js

firebase: {
    apiKey: 'xxxxxxx',
    authDomain: 'myappname.firebaseapp.com',
    databaseURL: 'https://myappname.firebaseio.com',
    storageBucket: 'myappname.appspot.com',
    messagingSenderId: '1234567890'
  },

    torii: {
      sessionServiceName: 'session'
    }

torii-adapters/application.js (I've changed this so much, I can't even remember what the original code was, because none of what I change/add/delete here seems to do anything at all.) torii-adapters/application.js(我已经改变了很多,我什至不记得原始代码是什么,因为我在这里更改/添加/删除的内容似乎根本没有做任何事情。)

import Ember from 'ember';
import ToriiFirebaseAdapter from 'emberfire/torii-adapters/firebase';

export default ToriiFirebaseAdapter.extend({
  firebase: Ember.inject.service(),
});

routes/application.js路线/application.js

import Ember from 'ember';

export default Ember.Route.extend({
    beforeModel: function() {
    return this.get('session').fetch().catch(function() {
    });
  },

  actions:{

    login: function(provider) {
      this.get('session').open('firebase', {
        provider: provider,
      }).then(function(data) {
        console.log(data.currentUser);
      });
    },

    logout: function() {
      this.get('session').close().then(function() {
        this.transitionTo('application');
      }.bind(this));
    }
  }
});

application.hbs应用程序.hbs

<div class="container">
  {{partial 'navbar'}}
<a {{action "signIn" "facebook"}} class="btn">{{fa-icon "facebook"}}</a>
<a {{action "signIn" "twitter"}} class="btn">{{fa-icon "twitter"}}</a>
<a {{action "signIn" "github"}} class="btn">{{fa-icon "github"}}</a>
<a {{action "signIn" "google"}} class="btn">{{fa-icon "google"}}</a>
  {{outlet}}
</div>

EDIT 1编辑 1

Above code is giving me alot more errors after restarting ember server.上面的代码在重新启动 ember 服务器后给了我更多的错误。 Is this the cause of my troubles ?这是我烦恼的原因吗? All the changes that seemingly didn't change a thing, weren't registered until after a server restart ?所有看似没有改变的变化,直到服务器重启后才被注册? If that's the case, I may have passed the correct solution about a hundred times already...如果是这样,我可能已经通过了大约一百次正确的解决方案......

EDIT 2编辑 2

Changed the code to reflect the actual issue.更改了代码以反映实际问题。 The previous code was screwed beyond measure, but I never realized it because it didn't pick up until after a server restart.以前的代码被搞砸了,但我从未意识到这一点,因为它直到服务器重新启动后才恢复。

EDIT 3 Found and tried this, to no avail: https://stackoverflow.com/a/32079863/4309050编辑 3发现并尝试了这个,但无济于事: https : //stackoverflow.com/a/32079863/4309050

This is Lorem Ipsum Dolor's answer , but updated for Ember 3.16+这是Lorem Ipsum Dolor 的回答,但已针对 Ember 3.16+ 更新


// Inside routes/application.js
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class ApplicationRoute Route {
  @service session;
    
  async beforeModel() {
    try {
      return this.session.fetch();
    } catch {}
  }
}

Note that in Ember 3.16+, it is not recommended to add actions to your Route.请注意,在 Ember 3.16+ 中,不建议向您的 Route 添加操作。 Instead, you can add them to a Controller or Component context:相反,您可以将它们添加到控制器或组件上下文中:

import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';

export default class LoginLogout extends Component {
  @service session;
  @service router;

  @action
  async login(provider)  {
    let data = await this.session.open('firebase', { provider });

    console.log(data.currentUser);
  }

  @action
  async logout() {
    await this.session.close();

    this.router.transitionTo('application');
  }
}

Note the addition of the router service .注意路由器服务的添加 The Router service is the way we interact with routing anywhere in our apps.路由器服务是我们与应用程序中任何地方的路由交互的方式。

import ToriiFirebaseAdapter from 'emberfire/torii-adapters/firebase';
    
export default class MyAdapter extends ToriiFirebaseAdapter {

}

NOTE: for Ember 3.16+ apps, here is the same code, but with updated syntax / patterns: https://stackoverflow.com/a/62500685/356849注意:对于 Ember 3.16+ 应用程序,这里是相同的代码,但更新了语法/模式: https ://stackoverflow.com/a/62500685/356849

The below is for Ember < 3.16, even though the code would work as 3.16+ as fully backwards compatible, but it's not always fun to write older code.下面是针对 Ember < 3.16 的,尽管代码可以像 3.16+ 一样完全向后兼容,但编写旧代码并不总是很有趣。


Try to inject service inside your application route and move the beforeModel outside of the actions hash,尝试在您的应用程序路由中注入服务并将beforeModel移到actions哈希之外,

// Inside routes/application.js

export default Ember.Route.extend({
  session: Ember.inject.service(), // (1)

  beforeModel: function() {
    return this.get('session').fetch().catch(function() {});
  },

  actions:{
    login: function(provider) {
      this.get('session').open('firebase', {
        provider: provider,
      }).then(function(data) {
        console.log(data.currentUser);
      });
    },

    logout: function() {
      this.get('session').close().then(function() {
        this.transitionTo('application');
      }.bind(this));
    }
  }
});

I have completed the same thing (Firebase Torii Auth) yesterday, try to follow the guide carefully.我昨天完成了同样的事情(Firebase Torii Auth),请仔细按照指南进行操作。 The only thing missing from the guide is to inject session service manually.指南中唯一缺少的是手动注入session服务。

Still remember the session you declared inside environment.js file?还记得你在environment.js文件中声明的session吗? You have to inject it to make it available您必须注入它才能使其可用

session: Ember.inject.service(), // (1) 

https://github.com/firebase/emberfire/blob/master/docs/guide/authentication.md https://github.com/firebase/emberfire/blob/master/docs/guide/authentication.md

Inside my ToriiFirebaesAdapter,在我的 ToriiFirebaesAdapter 里面,

import ToriiFirebaseAdapter from 'emberfire/torii-adapters/firebase';

export default ToriiFirebaseAdapter.extend({
});

I had the same issue and I noticed that my torii-adapters/application.js located under the pods structure (because I use it).我遇到了同样的问题,我注意到我的torii-adapters/application.js位于pods结构下(因为我使用它)。 So I moved the torii-adapters folder to app folder and everything started to work.所以我将torii-adapters文件夹移动到app文件夹,一切都开始工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM