我有一个要在Rails应用程序API上显示的用户列表。我正在使用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...">,
我正在本地主机上对此进行测试。我的目标是访问http://localhost:3000/api/users
并获得json格式的所有用户的列表。
当我转到该站点时,系统提示我用户名和密码。我输入了用户的用户名和密码之一:“ iggy1”,“ helloworld”。我看到此错误消息:
SQLite3::SQLException: no such column: users.password: SELECT "users".* FROM "users" WHERE "users"."username" = ? AND "users"."password" = '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
我尝试了一下。我遵循bcrypt网站上给出的代码。
这是我的代码:
#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
在我的架构上没有特别的要求(在应用程序控制器上,它明确显示了用户名和密码)。我认为Rails可能试图寻找“密码”,但找不到它。我对bcrypt的了解还不足以说是这种情况。
其次,在bcrypt网站上,由于不确定将代码放置在何处,因此未实现以下代码。难道是bcrypt无法按照我的要求行事吗?
def login
@user = User.find_by_email(params[:email])
if @user.password == params[:password]
give_token
else
redirect_to home_url
end
end
我不确定缺少什么。
数据库模式中有两个字段可用于输入密码
如果您正在使用Devise进行身份验证,则该encrypted_password
字段用于存储密码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句