目前,我正在做样例web应用程序。在这个应用程序中,用户可以通过JSON注册,也可以通过浏览器注册。
对于JSON身份验证,我使用了、gem、devise_token_auth、、auth,它运行得很好。我可以通过API注册。
但现在我必须从网络浏览器提供注册功能。
(URL:up)获取以下错误
The action 'new' could not be found for DeviseTokenAuth::RegistrationsController那么,我如何允许从网络浏览器注册。
这是我的routes.rb文件
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth'
end生成路径
Prefix Verb URI Pattern Controller#Action
new_user_session GET /auth/sign_in(.:format) devise_token_auth/sessions#new
user_session POST /auth/sign_in(.:format) devise_token_auth/sessions#create
destroy_user_session DELETE /auth/sign_out(.:format) devise_token_auth/sessions#destroy
new_user_password GET /auth/password/new(.:format) devise_token_auth/passwords#new
edit_user_password GET /auth/password/edit(.:format) devise_token_auth/passwords#edit
user_password PATCH /auth/password(.:format) devise_token_auth/passwords#update
PUT /auth/password(.:format) devise_token_auth/passwords#update
POST /auth/password(.:format) devise_token_auth/passwords#create
cancel_user_registration GET /auth/cancel(.:format) devise_token_auth/registrations#cancel
new_user_registration GET /auth/sign_up(.:format) devise_token_auth/registrations#new
edit_user_registration GET /auth/edit(.:format) devise_token_auth/registrations#edit
user_registration PATCH /auth(.:format) devise_token_auth/registrations#update
PUT /auth(.:format) devise_token_auth/registrations#update
DELETE /auth(.:format) devise_token_auth/registrations#destroy
POST /auth(.:format) devise_token_auth/registrations#create
auth_validate_token GET /auth/validate_token(.:format) devise_token_auth/token_validations#validate_token
auth_failure GET /auth/failure(.:format) devise_token_auth/omniauth_callbacks#omniauth_failure
GET /auth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#omniauth_success
GET|POST /omniauth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#redirect_callbacks
omniauth_failure GET|POST /omniauth/failure(.:format) devise_token_auth/omniauth_callbacks#omniauth_failure
GET /auth/:provider(.:format) redirect(301)我需要在这里添加单独的设计吗?请告诉我你的想法。
发布于 2017-09-19 09:38:59
实现这一目标的最佳方法是将普通的设计路由安装到一个作用域,并将API devise_token_auth路由安装到单独的作用域。
Rails.application.routes.draw do
# standard devise routes at /users
devise_for :users
# token auth routes available at /api/auth/
namespace :api, defaults: { format: :json } do
scope module: :v1 do
mount_devise_token_auth_for 'User', at: 'auth'
end
end
end发布于 2017-09-18 15:23:39
new_user_registration GET /auth/sign_up(.:format) devise_token_auth/registrations#new这不会导致任何结果,因为在GEM控制器devise_token_auth/registrations_controller中没有新的操作。
1-更改路由,以便将http GET请求根植于controller#new操作。
2-创建controller#new操作
3-让new.html.erb中的表单调用以下操作
POST /auth(.:format) devise_token_auth/registrations#create4-要创建用户,只需向以下URL发出HTTP POST请求
POST /auth(.:format) devise_token_auth/registrations#create使用传递给此控制器操作所需的参数。
def create
@resource = resource_class.new(sign_up_params)
@resource.provider = "email"
# honor devise configuration for case_insensitive_keys
if resource_class.case_insensitive_keys.include?(:email)
@resource.email = sign_up_params[:email].try :downcase
else
@resource.email = sign_up_params[:email]
end
# give redirect value from params priority
@redirect_url = params[:confirm_success_url]
# fall back to default value if provided
@redirect_url ||= DeviseTokenAuth.default_confirm_success_url
# success redirect url is required
if resource_class.devise_modules.include?(:confirmable) && !@redirect_url
return render_create_error_missing_confirm_success_url
end
# if whitelist is set, validate redirect_url against whitelist
if DeviseTokenAuth.redirect_whitelist
unless DeviseTokenAuth::Url.whitelisted?(@redirect_url)
return render_create_error_redirect_url_not_allowed
end
end
begin
# override email confirmation, must be sent manually from ctrl
resource_class.set_callback("create", :after, :send_on_create_confirmation_instructions)
resource_class.skip_callback("create", :after, :send_on_create_confirmation_instructions)
if @resource.save
yield @resource if block_given?
unless @resource.confirmed?
# user will require email authentication
@resource.send_confirmation_instructions({
client_config: params[:config_name],
redirect_url: @redirect_url
})
else
# email auth has been bypassed, authenticate user
@client_id = SecureRandom.urlsafe_base64(nil, false)
@token = SecureRandom.urlsafe_base64(nil, false)
@resource.tokens[@client_id] = {
token: BCrypt::Password.create(@token),
expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
}
@resource.save!
update_auth_header
end
render_create_success
else
clean_up_passwords @resource
render_create_error
end
rescue ActiveRecord::RecordNotUnique
clean_up_passwords @resource
render_create_error_email_already_exists
end
end这样就可以创建用户,但这不是一个好方法。您使用的是api,而您应该只使用devise。
https://stackoverflow.com/questions/46281078
复制相似问题