2012-03-09 44 views
5

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 ?

+0

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 đó. –

Trả lời

10

Đó là lỗi của tôi, tôi sẽ cập nhật bài đăng trên blog. Bạn cần phải thêm mã sau để tạo người dùng trong bộ điều khiển đăng ký

if params[:api_key].blank? or params[:api_key] != API_KEY 
    render :json => {'errors'=>{'api_key' => 'Invalid'}}.to_json, :status => 401 
    return 
end 
build_resource 
if resource.save 
    sign_in(resource) 
    resource.reset_authentication_token! 
    #rabl template with authentication token 
    render :template => '/devise/registrations/signed_up' 
else 
    render :template => '/devise/registrations/new' #rabl template with errors 
end 

Hãy cho tôi biết nếu bạn gặp phải bất kỳ sự cố nào?

+0

cảm ơn rất nhiều, đăng ký đang hoạt động tốt. Về phần sign_in, sự hiểu biết của tôi là mã để sửa đổi nằm trong phương thức "tạo" của SessionsController, bạn có xác nhận không? Trong trường hợp này, đăng nhập/mật khẩu có được gửi đi để xác thực không? – Luc

+0

Bạn cần thêm dòng sau vào phương thức tạo session_controller # sau 'sign_in (resource_name, resource) current_user.reset_authentication_token' – Sethupathi

0

Về câu hỏi của Luc, đó cũng là sự hiểu biết của tôi.

Ví dụ: sử dụng thông tin đăng nhập không phải API mặc định, SessionsController.create xử lý thông tin đăng nhập. Làm cách nào để truy xuất số authentication_token và sử dụng lại nó như một phần của các cuộc gọi API sau? Ngoài ra, làm cách nào để ghi đè số SessionsController.create để gọi resource.reset_authentication_token!?

+0

Nếu bạn đang sử dụng non api, bạn có thể truy cập nó như current_user.authentication_token – Sethupathi