首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember-simple-auth:从URL进行身份验证

Ember-simple-auth:从URL进行身份验证
EN

Stack Overflow用户
提问于 2014-05-01 08:41:07
回答 2查看 475关注 0票数 1

我有一个要求,我可以将?auth_token=x追加到我的应用程序中的任何网址,它将恢复会话,就像你有一个cookie或细节存储在本地存储中一样。

在过去的几个小时里,我尝试了我能想到的所有方法来实现这一点,但我没有取得任何进展。这个是可能的吗?我应该在哪里添加这样的功能?

EN

回答 2

Stack Overflow用户

发布于 2014-05-08 16:12:38

我在这里回答了我自己的问题,因为我设法找到了一个解决方案,尽管我不确定它是否正确!

application.js:

代码语言:javascript
复制
// Really simple query parameter access
(function($) {
  $.QueryString = (function(a) {
    if (a == "") return {};
    var b = {};
    for (var i = 0; i < a.length; ++i)
    {
      var p=a[i].split('=');
      if (p.length != 2) continue;
      b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }
    return b;
  })(window.location.search.substr(1).split('&'))
})(jQuery);

initializers/Authentication.js咖啡:

代码语言:javascript
复制
LocalStorageWithURLAuth = Ember.SimpleAuth.Stores.LocalStorage.extend
  init: ->
    if $.QueryString['auth_token']
      auth_token = $.QueryString['auth_token']
      Ember.$.ajax(
        async: false
        url: '/users/sign_in'
        type: 'POST'
        data: { auth_token: auth_token }
        dataType: 'json'
      ).then (data) =>
        @persist
          auth_token: data.auth_token
          auth_email: data.auth_email
          user_id: data.user_id
          authenticatorFactory: 'authenticator:devise'
        @_super()
      , (error) =>
        @_super()
    else
      @_super()

Ember.Application.initializer
  name: 'authentication'
  initialize: (container, application) ->
    Ember.SimpleAuth.Session.reopen
      user: (->
        userId = @get 'user_id'
        if !Ember.isEmpty userId
          container.lookup('store:main').find 'user', userId
      ).property('user_id')

    container.register 'session-store:local-storage-url', LocalStorageWithURLAuth

    Ember.SimpleAuth.setup container, application,
      storeFactory: 'session-store:local-storage-url'
      authenticatorFactory: 'authenticator:devise'
      authorizerFactory: 'authorizer:devise'
票数 1
EN

Stack Overflow用户

发布于 2014-05-09 16:19:23

我不太明白你在做什么。您似乎是在从查询字符串中的auth令牌恢复会话?这实际上就是验证器的restore方法的作用(参见这里的文档:http://ember-simple-auth.simplabs.com/ember-simple-auth-devise-api-docs.html#Ember-SimpleAuth-Authenticators-Devise-restore)。另外,当应用程序启动时,查询字符串不是空的吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23400413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档