tôi sau bài viết tuyệt vời này để thiết lập phần xác thực của đường ray của tôi (3.2) API:
http://blog.joshsoftware.com/2011/12/23/designing-rails-api-using-rabl-and-devise/xác thực Mã với Rails và Vạch
Tôi đã làm các bước sau đây:
devise -Nhập để Gemfile
-Enabled đưa ra cho các mô hình sử dụng và chạy di cư cần
mô hình người dùng -My là
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
devise :token_authenticatable
attr_accessible :email, :authentication_token, :password, :password_confirmation, :remember_me
end
cũng như token_authenticable trong Cơ sở dữ liệu (thông qua di chuyển).
-Subclassed các RegistrationController với:
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
resource = warden.authenticate!(:scope => resource_name, :recall => " {controller_path}#new")
sign_in(resource_name, resource)
current_user.reset_authentication_token!
respond_with resource, :location => after_sign_in_path_for(resource)
end
def update
super
end
end
-Trong routes.rb, tôi có:
devise_for :users, :controllers => {:registrations => "registrations"}
USER TẠO
Tôi muốn yêu cầu sau để tạo người dùng và gửi lại authentification_token:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"user":{"email":"[email protected]", "password":"pass"}}' 'http://localhost:3000/users.json
Sự hiểu biết của tôi là logic nên đi vào phương thức "tạo" của bộ điều khiển đăng ký (nên tạo người dùng và đăng nhập cùng lúc). Tôi nghĩ tôi nên sai vì thông điệp tôi nhận được là:
{"error":"You need to sign in or sign up before continuing."}
Phần còn thiếu để người dùng mới tạo và đăng nhập là gì? Không phải POST đối với users.json được ánh xạ tới RegistrationController # create?
USER ĐĂNG NHẬP
Ngoài ra, tôi muốn yêu cầu sau để đăng nhập người dùng trong (gửi anh trở lại authentification_token của mình một lần đăng nhập/mật khẩu đã được kiểm tra)
curl -H "Accept: application/json" -H "Content-type: application/json" -X GET -d '{"user":{"email":"[email protected]","password":"pass"}}' 'http://localhost:3000/users.json
Tôi đoán logic nên đi trong phương thức "update" của RegistrationController nhưng không chắc chắn 100% về điều đó. Sau khi đăng nhập xong, sau đó tôi sẽ thêm xác thực mã thông báo để bảo vệ việc tạo/xem một số mô hình khác.
CẬP NHẬT
Khi tôi phát hành:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"user":{"email":"[email protected]", "password": "mypass", "phone":"1234567890"}}' 'http://localhost:3000/users.json'
Tôi nhận được thông báo sau:
Started POST "/users.json" for 127.0.0.1 at 2012-03-11 20:50:05 +0100
Processing by RegistrationsController#create as JSON
Parameters: {"user"=>{"email"=>"[email protected]", , "password"=>"[FILTERED]", "phone"=>"1234567890"}, "registration"=>{"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "phone"=>"1234567890"}, "action"=>"create", "controller"=>"registrations", "format"=>"json"}}
WARNING: Can't verify CSRF token authenticity
Completed 401 Unauthorized in 1ms
Bất cứ ý tưởng tại sao người dùng không được tạo ra và đăng nhập và tại sao không có authentication_token Được trả lại ?
Nếu bạn muốn người dùng được tạo trước khi xác thực, bạn nên thêm mã đó. Devise không làm điều đó. –