6

Próbuję zalogować się przy użyciu istniejącego użytkownika devise_token_auth wersję 0.1.38, ale mam trafienia IndexError: string not matched w bibliotece na sessions_controller.devise_token_auth & Rails 5 - IndexError: ciąg nie pasuje

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' 

Odpowiedni kod z sessions_controller jest:

if @resource and valid_params?(field, q_value) and @resource.valid_password?(resource_params[:password]) and ([email protected]_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) 

Dodałem devise_token_auth do istniejącego projektu, więc jest to bardzo możliwe, że utworzyliśmy złe dane w kolumnie tokens. Próbowałem różnych sposobów niewywiązywania się z moich dotychczasowych użytkowników, w tym naśladowania kodu w 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 

widziałem wzmiankę o tym w issue #101, ale które nie mają określoną rozdzielczość. Każdy pomysł, gdzie idę źle?

+0

Dla mnie to tylko wydaje się zdarzyć, gdy uruchamiam moją app szyn na Heroku. Moja lokalna instancja nie ma tego problemu. Zauważyłem, że moja wersja Postgres (9.6.2) lokalnie różni się od wersji Heroku (9.6.4). Nie jestem pewien, czy to powoduje problem, czy nie –

Odpowiedz

4

Okazuje się, że muszę ustawić tokens na nil, a następnie opracować zajmie się ustawienie dla mnie. Znalazłem odpowiedź w this devise issue.

def change 
    add_column :users, :provider, :string, null: false, default: "email" 
    add_column :users, :uid, :string, null: false, default: "" 
    add_column :users, :tokens, :text 

    reversible do |direction| 
    direction.up do 
     User.find_each do |user| 
     user.uid = user.email 
     user.tokens = nil 
     user.save! 
     end 
    end 
    end 

    add_index :users, [:uid, :provider], unique: true 
end 
Powiązane problemy