【发布时间】:2016-11-20 07:32:35
【问题描述】:
我正在尝试使用devise_token_auth 版本0.1.38 登录现有用户,但我在库的sessions_controller 中遇到了IndexError: string not matched。
IndexError (string not matched):
devise_token_auth (0.1.38) app/controllers/devise_token_auth/sessions_controller.rb:37:in `[]='
devise_token_auth (0.1.38) app/controllers/devise_token_auth/sessions_controller.rb:37:in `create'
actionpack (5.0.0) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
来自sessions_controller的相关代码是:
if @resource and valid_params?(field, q_value) and @resource.valid_password?(resource_params[:password]) and (!@resource.respond_to?(:active_for_authentication?) or @resource.active_for_authentication?)
# create client id
@client_id = SecureRandom.urlsafe_base64(nil, false)
@token = SecureRandom.urlsafe_base64(nil, false)
# !! Next line is line 37 with the error !!
@resource.tokens[@client_id] = {
token: BCrypt::Password.create(@token),
expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
}
@resource.save
sign_in(:user, @resource, store: false, bypass: false)
我已将devise_token_auth 添加到现有项目中,因此我很可能在tokens 列中创建了错误数据。我尝试了各种将token json 默认设置为现有用户的方法,包括模仿sessions_controller 中的代码。
add_column :users, :tokens, :json, null: false, default: {}
User.reset_column_information
client_id = SecureRandom.urlsafe_base64(nil, false)
token = SecureRandom.urlsafe_base64(nil, false)
User.find_each do |user|
user.uid = user.email
user.provider = 'email'
user.tokens[client_id] = {
token: BCrypt::Password.create(token),
expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
}
user.save!
end
我在issue #101 中看到了这一点,但没有具体的解决方案。知道我哪里出错了吗?
【问题讨论】:
-
对我来说,这似乎只发生在我在 Heroku 上运行我的 rails 应用程序时。我的本地实例没有这个问题。我注意到我的 Postgres 版本 (9.6.2) 在本地与 Heroku 版本 (9.6.4) 不同。不确定这是否导致问题
标签: ruby-on-rails devise ruby-on-rails-5 http-token-authentication