2015-06-30 47 views
5

Tôi đang sử dụng đá quý Devise auth token để xác thực một số phần của ứng dụng đường ray của tôi. Nhưng khi tôi cố gắng tạo một người dùng mới với đường dẫn đăng ký, nó sẽ cho tôi lỗi sau đây {"errors":["Authorized users only."]}.Làm cách nào để truy cập bộ điều khiển đăng ký auth token của devise?

Đây là mã rspec mà tôi đang sử dụng cho các kiểm tra,

it 'creates a user using email/password combo' do 
    post api_user_registration_path, { email: 'xxx', password: 'yyy',password_confirmation: 'yyy'} 
    puts last_response.body 
    expect(last_response.body).not_to have_content('error') 
end 

Bổ sung thông tin: tên model là 'Người dùng' và các tuyến đường có vẻ như,

namespace :api do 
    scope :v1 do 
    mount_devise_token_auth_for 'User', at: 'auth' 
    end 
end 

Tôi hiểu rằng sự suy nghĩ mong muốn người dùng được xác thực trước khi truy cập vào đường dẫn này, nhưng đây là đăng ký người dùng, nó cần phải nằm ngoài xác thực. Bạn có thể đề xuất một giải pháp cho điều này? Có bất kỳ cấu hình nào mà tôi thiếu ở đây không?

+0

bạn có nghĩ ra: đăng ký được thêm vào trong Mẫu Người dùng của bạn –

+0

có. có trong mô hình người dùng của tôi, 'devise: lockable,: database_authenticatable,: registerable,: recoverable,: rememberable,: trackable,: validatable,: confirmable,: omniauthable' – quixote

+1

Bạn có kế thừa 'Api :: UserRegistrationsController' từ' Devise :: RegistrationsController' –

Trả lời

4

Hãy thử với:

namespace :api do 
    namespace :v1 do 
     mount_devise_token_auth_for 'User', at: '/auth' 
    end 
    end 

này sẽ tạo ra các tuyến đường sau:

 new_api_v1_user_session GET /api/v1/auth/sign_in(.:format)  devise_token_auth/sessions#new                                 
      api_v1_user_session POST /api/v1/auth/sign_in(.:format)  devise_token_auth/sessions#create                                
    destroy_api_v1_user_session DELETE /api/v1/auth/sign_out(.:format)  devise_token_auth/sessions#destroy                                
      api_v1_user_password POST /api/v1/auth/password(.:format)  devise_token_auth/passwords#create                                
     new_api_v1_user_password GET /api/v1/auth/password/new(.:format) devise_token_auth/passwords#new                                 
     edit_api_v1_user_password GET /api/v1/auth/password/edit(.:format) devise_token_auth/passwords#edit                                 
           PATCH /api/v1/auth/password(.:format)  devise_token_auth/passwords#update                                
           PUT /api/v1/auth/password(.:format)  devise_token_auth/passwords#update                                
cancel_api_v1_user_registration GET /api/v1/auth/cancel(.:format)   devise_token_auth/registrations#cancel                               
     api_v1_user_registration POST /api/v1/auth(.:format)    devise_token_auth/registrations#create                               
    new_api_v1_user_registration GET /api/v1/auth/sign_up(.:format)  devise_token_auth/registrations#new                                
    edit_api_v1_user_registration GET /api/v1/auth/edit(.:format)   devise_token_auth/registrations#edit                                
           PATCH /api/v1/auth(.:format)    devise_token_auth/registrations#update                               
           PUT /api/v1/auth(.:format)    devise_token_auth/registrations#update                               
           DELETE /api/v1/auth(.:format)    devise_token_auth/registrations#destroy                               
    api_v1_auth_validate_token GET /api/v1/auth/validate_token(.:format) devise_token_auth/token_validations#validate_token 

Cũng tạo một bộ điều khiển trong app/controllers/api/v1/api_base_controller.rb

class Api::V1::BaseApiController < ActionController::Base 

    include DeviseTokenAuth::Concerns::SetUserByToken 

end 

Cũng thêm vào tập tin của bạn app/controllers/application_controller.rb

before_action :configure_permitted_parameters, if: :devise_controller? 
+0

Cảm ơn câu trả lời. Nhưng tôi nghĩ rằng tôi đang làm khá nhiều điều tương tự. Ngoại trừ bộ điều khiển api của tôi không nằm trong một thư mục api, như bạn có nó trong mã của bạn. Điều đó không khắc phục được. : (Tôi có tất cả các tuyến đường được tạo ra bởi devise auth token – quixote

+0

@quixote kiểm tra chỉnh sửa của tôi về ApplicationController. –

+0

cảm ơn cho chỉnh sửa. Vấn đề của tôi có vẻ là một vấn đề tương thích với phát sinh. Tôi sẽ đăng cập nhật khi tôi có thể giải quyết nó hoàn toàn. – quixote

Các vấn đề liên quan