加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何使用Devise为Omniauth创建自定义登录表单

发布时间:2020-12-17 02:20:03 所属栏目:百科 来源:网络整理
导读:我使用Omniauth作为我唯一的身份验证,我的模型上没有: database_authentication.我想使用这个gem登录Atlassian Crowd: https://github.com/robdimarco/omniauth_crowd 该gem使用标准的Crowd登录表单,这是我不想要的.我想制作一个行为相同的自定义表单,但完
我使用Omniauth作为我唯一的身份验证,我的模型上没有: database_authentication.我想使用这个gem登录Atlassian Crowd: https://github.com/robdimarco/omniauth_crowd

该gem使用标准的Crowd登录表单,这是我不想要的.我想制作一个行为相同的自定义表单,但完全取代登录页面(没有“登录Crowd”)或类似的东西,只是一个登录到Crowd的表单.

为此,我已将此选项添加到devise.rb中的config.omniauth行:

:form => Devise::SessionsController.actions(:new)

从我在网上看到的内容,将使用Rack端点显示自定义表单.我已将app / views / devise / sessions / new.html.erb中的html更改为new.html.haml,其中包含以下格式:

= simple_form_for @user,:url => user_omniauth_authorize_path(:crowd) do |f|
  = f.input :username,:input_html => { :name => 'username' }
  = f.input :password,:input_html => { :name => 'password' }
  = f.button :submit

我希望模仿默认的OmniAuth表单.问题是,当我在devise.rb中使用:form选项访问users / auth / crowd时,我收到错误:

Could not find devise mapping for path "/users/auth/crowd".

有没有办法在使用Devise时为这样的OmniAuth提供自定义表单?

编辑:以下是路线:

devise_for :users,:controllers => { :omniauth_callbacks => 'users/omniauth' } do
  get 'login',:to => 'devise/sessions#new',:as => :new_user_session
  get 'logout',:to => 'devise/sessions#destroy',:as => :destroy_user_session
end

devise_scope :user do
  get '/users/auth/:provider',:to => 'users/omniauth#passthru'
  match '/users/auth/failure',:to => 'users/omniauth#failure'
end

resources :users

我在这里尝试了几件事,但是我尝试过的东西都没有用过.

解决方法

我知道这可能会迟到,但我需要做同样的事情,也许这将有助于将来的其他人.就我而言,我正在开发一个内部应用程序,需要使用与我们自己的众包服务器上相同的帐户.就我所知,没有任何安全问题,因为我们只与自己的内部人群服务器交谈.我想设置登录页面的样式以匹配应用程序的美学.

添加omniauth_crowd gem并在使用Devise配置后,您应该有一个如下所示的路由:

user_omniauth_authorize    GET|POST    /users/auth/:provider(.:format)    authentication#passthru {:provider=>/crowd/}

我所要做的就是创建一个自定义设计会话表单,模仿默认的omniauth_crowd工作方式.基本上它只是将用户名和密码param POST到您的服务器.创建自定义设计视图在https://github.com/plataformatec/devise#configuring-views中描述

我的表单看起来像这样:

<%= form_tag user_omniauth_authorize_path(:crowd) do %>
  <div><%= label_tag :username,"Username" %><br />
  <%= text_field_tag :username %></div>

  <div><%= label_tag :password,"Password" %><br />
  <%= password_field_tag :password %></div>

  <div><%= submit_tag "Sign in" %></div>
<% end %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读