ember.js - 使用 ember-simple-auth 和 torii,尽管服务器响应成功,但模式永远不会关闭, session 也永远不会更新

标签 ember.js devise ember-cli ember-simple-auth torii

我正在尝试允许多个 oauth 提供商在我的 Rails 后端上使用 ember-cli、ember-simple-auth、torii 和 devise。一切似乎都正常,但模式永远不会关闭,客户端中的 session 也永远不会更新。

重现步骤:

  1. 点击链接通过 google-oauth2 提供商登录
  2. 观察模式打开情况以及帐户登录选项列表
  3. 选择帐户并登录
  4. 观察模式重定向到本地 Rails 服务器以进行 token 交换 ( http://localhost:3000/login_profiles/auth/google_oauth2/callback?state=STATE&code=string )
  5. 观察由本地 Rails 服务器发出的 json 响应填充的模式窗口

    { "access_token": "8gmvGfHsUx_1mrEAG1Vu", “login_profile_id”:1 }

  6. 观察模式保持打开状态。如果手动关闭浏览器会报告错误:

    错误:弹出窗口已关闭、授权被拒绝或在窗口关闭之前未收到身份验证消息。

  7. 观察到客户端 session 对象没有更新

下面是我的库版本和我的应用程序代码的相关部分。任何帮助将不胜感激。

调试:Ember:1.13.3

调试:Ember 数据:1.13.5

调试:jQuery:2.1.4

调试:Ember 简单例份验证:0.8.0

调试:Ember Simple Auth Torii:0.8.0

服务器:

class LoginProfiles::OmniauthCallbacksController < Devise::OmniauthCallbacksController

def google_oauth2
  login_profile = LoginProfile.find_or_create_by_google_oauth2(request.env['omniauth.auth'])

  render json: { access_token: login_profile.authentication_token, login_profile_id: login_profile.id }
end

def facebook
  login_profile = LoginProfile.find_or_create_by_facebook_oauth2(request.env['omniauth.auth'])

  render json: { access_token: login_profile.authentication_token, login_profile_id: login_profile.id }
end

结束

客户:

配置/环境.js

torii: {
  providers: {
    'google-oauth2': {
      clientId: 'string',
      redirectUri: 'http://localhost:3000/login_profiles/auth/google_oauth2/callback'
    },
    'facebook-oauth2': {
      clientId: 'string',
      redirectUri: 'http://localhost:3000/login_profiles/auth/facebook/callback'
    }

  }
},

'simple-auth': {
  authenticationRoute: 'sign-in',
  routeAfterAuthentication: 'index',
  authorizer: 'authorizer:application',
  crossOriginWhitelist: ['http://localhost:3000', 'http://localhost:4200']
},

路线/application.coffee

`import Ember from 'ember'`
`import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin'`

ApplicationRoute = Ember.Route.extend ApplicationRouteMixin,
  actions:

    authenticateWithFacebook: ->      
      @get('session').authenticate('simple-auth-authenticator:torii', 'facebook-oauth2')

    authenticateWithGooglePlus: ->
      @get('session').authenticate('simple-auth-authenticator:torii', 'google-oauth2')

`export default ApplicationRoute`

模板/application.hbs

<p>
  Sign in with
   <a {{action "authenticateWithGooglePlus"}}>Google</a>
    or
   <a {{action "authenticateWithFacebook"}}>Facebook</a>
   or
   {{#link-to 'register' id="register"}}register a new account.{{/link-to}}
 </p>

凉亭.json

{
  "name": "brand-management-client",
  "dependencies": {
    "ember": "1.13.3",
    "ember-cli-shims": "ember-cli/ember-cli-shims#0.0.3",
    "ember-cli-test-loader": "ember-cli-test-loader#0.1.3",
    "ember-data": "1.13.5",
    "ember-load-initializers": "ember-cli/ember-load-initializers#0.1.5",
    "ember-qunit": "0.4.1",
    "ember-qunit-notifications": "0.0.7",
    "ember-resolver": "~0.1.18",
    "jquery": "^2.1.4",
    "loader.js": "ember-cli/loader.js#3.2.0",
    "qunit": "~1.17.1",
    "foundation": "~5.5.0",
    "ember-simple-auth": "0.8.0"
  }
}

package.json

{
  "name": "brand-management-client",
  "version": "0.0.0",
  "description": "Small description for brand-management-client goes here",
  "private": true,
  "directories": {
    "doc": "doc",
    "test": "tests"
  },
  "scripts": {
    "start": "ember server",
    "build": "ember build",
    "test": "ember test"
  },
  "repository": "",
  "engines": {
    "node": ">= 0.10.0"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "broccoli-asset-rev": "^2.0.2",
    "broccoli-clean-css": "1.0.0",
    "ember-cli": "1.13.1",
    "ember-cli-app-version": "0.4.0",
    "ember-cli-babel": "^5.0.0",
    "ember-cli-coffeescript": "0.11.0",
    "ember-cli-dependency-checker": "^1.0.0",
    "ember-cli-foundation-sass": "1.1.1",
    "ember-cli-htmlbars": "0.7.9",
    "ember-cli-htmlbars-inline-precompile": "^0.1.1",
    "ember-cli-ic-ajax": "^0.2.1",
    "ember-cli-inject-live-reload": "^1.3.0",
    "ember-cli-qunit": "0.3.15",
    "ember-cli-release": "0.2.3",
    "ember-cli-sass": "3.1.0",
    "ember-cli-simple-auth": "0.8.0",
    "ember-cli-simple-auth-torii": "0.8.0",
    "ember-cli-uglify": "^1.0.1",
    "ember-data": "1.13.5",
    "ember-disable-proxy-controllers": "^1.0.0",
    "ember-export-application-global": "^1.0.2",
    "torii": "^0.5.1"
  }
}

最佳答案

重定向实际上不应该由 Rails 应用程序处理,而应该由 torii 处理,torii 将从查询字符串中读取身份验证代码并将其发布回父窗口(您可以在自定义身份验证器中使用它来将其交换为API 的访问 token ) - 检查 torii 的来源 Oauth 2.0 提供者的 open 方法供引用:https://github.com/Vestorly/torii/blob/master/lib/torii/providers/oauth2-code.js#L118

关于ember.js - 使用 ember-simple-auth 和 torii,尽管服务器响应成功,但模式永远不会关闭, session 也永远不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31790678/

相关文章:

javascript - Ember-bootstrap 从 javascript 触发模式

ember.js - 创建记录并设置服务器端 ID 后调用的 DS.Model 回调

ruby-on-rails - 设计重置密码链接

ruby-on-rails - 结合设计与资源 :users in Rails

ruby-on-rails - 使用 Devise 进行 Action 电缆测试

ember.js - ember init 'Yndh'的意思

ember.js - Ember CLI/浏览器刷新/生产/404

javascript - Ember.js 中的 CSS 边框和奇怪的元素边界

node.js - 由于 broccoli-sass 和 node-sass,无法将 ember cli 应用程序推送到 heroku

ember.js - 将 .findBy() 与 Ember 数据填充的数组 Controller 结合使用