2016-07-21 9 views
0

rails app APIに表示したいユーザーのリストがあります。私はパスワードをハッシュするためにbcryptを使用しています。私は作成し、レールコンソールにいくつかのユーザーのリストがあることを確認しました:bcryptを使用したRails認証エラー

2.2.2 :010 > User.all 
    User Load (8.9ms) SELECT "users".* FROM "users" 
=> #<ActiveRecord::Relation [#<User id: 2, created_at: "2016-07-19 06:23:35", updated_at: "2016-07-19 06:23:35", username: "iggy1", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, password_digest: "$2a$10$We2V5sFx3XJNHP9iHTx.5udLA9hEbJVDOcA01nemj0R...">, 

私はlocalhostでこれをテストしています。目標はhttp://localhost:3000/api/usersに行き、すべてのユーザーのリストをjson形式で持っていることです。

サイトに行くと、ユーザー名とパスワードの入力を求められました。ユーザーのユーザー名とパスワードの1つ、「iggy1」、「helloworld」を入力しました。私はそれを周りに少し遊んでみました

create_table "users", force: :cascade do |t| 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.string "username" 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.string "password_digest" 
    end 

SQLite3::SQLException: no such column: users.password: SELECT "users".* FROM "users" WHERE "users"."username" = ? AND "users"."password" = 'helloworld' 

私のスキーマは次のようになります。私は、このエラーメッセージが表示されます。私はbcrypt websiteに与えられたコードに従った。ここで

は私のコードです:

#controller/api/users_controller.rb 

class UsersController < ApplicationController 
    def create 
    @user = User.new(params[:user]) 
    @user.password = params[:password] 
    @user.save! 
    end 
end 


#controllers/users_controller.rb 

class UsersController < ApplicationController 
    def create 
    @user = User.new(params[:user]) 
    @user.password = params[:password] 
    @user.save! 
    end 
end 


#models/user.rb 

require 'bcrypt' 

class User < ActiveRecord::Base 
    #include 'bcrypt' 
    has_many :lists 
    has_many :items, through: :lists 
    has_secure_password 

    def password 
    @password ||= Password.new(password_hash) 
    end 

    def password=(new_password) 
    @password = Password.create(new_password) 
    self.password_hash = @password 
    end 

end 


#controllers/application_controller.rb 

class ApiController < ApplicationController 
    skip_before_action :verify_authenticity_token 
    private 

def authenticated? 
    authenticate_or_request_with_http_basic {|username, password| User.where(username: username, password: password).present? } 
    end 
end 

#config routes 

Rails.application.routes.draw do 
    namespace :api, defaults: { format: :json} do #supports JSON requests 
    resources :users 
    end 
end 

今、私は「bcryptの」行の非コメントが含まれていたとき、私はhttp://localhost:3000/api/users

wrong argument type String (expected Module) 

に行くとき、私は別のエラーを参照してください、私はいくつかの仮説を持っていますなぜこれが起こるかについて。

まず、私はbcryptを使用していると思いますし、password_digestを持っています。私は具体的には私のスキーマにpasswordを持っていません(アプリケーションコントローラ上では、明示的にユーザー名とパスワードと言います)。私はレールが「パスワード」を探してみたかもしれないが、それを見つけることができなかったと思う。私は、これが事実だと言うのに十分なbcryptを知らない。

第2に、bcrypt websiteで、どこに配置するかわからないため、次のコードを実装しませんでした。私が望んでいたようにbcryptが動作しない場合がありますか?

def login 
    @user = User.find_by_email(params[:email]) 
    if @user.password == params[:password] 
     give_token 
    else 
     redirect_to home_url 
    end 
    end 

私は何が欠けているか分からなかった。

答えて

1

その後、encrypted_passwordフィールドはパスワードを格納するために使用される認証のための

あなたが工夫を使用している場合は、あなたのDBスキーマ内のパスワードで利用可能な2つのフィールドがあります。

+0

'password'の代わりに' encrypted_pa​​ssword'を使用します。この場合、 'helloworld'ですか? – Iggy

+0

はい、Device Gemを使用している場合、自動的にパスワードが変換されます –

関連する問題