2016-07-21 1 views
0

레일즈 앱 API에 표시 할 사용자 목록이 있습니다. bcrypt를 사용하여 암호를 해시합니다. 내가 만든 레일 콘솔에 여러 사용자의 목록이 있는지 확인했다 :bcrypt를 사용하여 레일 인증 오류가 발생했습니다.

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...">, 

로컬 호스트에서이를 테스트하고있다. 목표는 http://localhost:3000/api/users으로 이동하여 json 형식의 모든 사용자 목록을 작성하는 것입니다.

사이트에 갈 때 사용자 이름과 암호를 묻는 메시지가 표시되었습니다. 사용자의 사용자 이름과 암호 중 하나 인 "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 

내가 http://localhost:3000/api/users

wrong argument type String (expected Module) 

에 갈 때 해제 주석, 나는 다른 오류가 'bcrypt'라인은 내가 여러 가지 가설이 왜 이런 일이 일어 났는지.

먼저 나는 bcrypt를 사용하고 있기 때문에 그것이 password_digest라고 생각합니다. 구체적으로 내 스키마에 password이 없다 (명시 적으로 사용자 이름과 암호라고하는 응용 프로그램 컨트롤러에서). 나는 레일즈가 '패스워드'를 찾지 만 찾지 못했을 것이라고 생각한다. 나는 이것이 bcrypt를 말할만큼 충분히 알지 못한다.

둘째, 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 스키마에서 암호에 해당하는 두 개의 필드가 있습니다.

+0

'password' 대신'encrypted_password'를 사용합니까?이 경우 'helloworld'입니까? – Iggy

+0

예, Device Gem을 사용했다면 자동으로 비밀번호를 변환합니다. –

관련 문제