因此,发送至new_user_session_path
或root_path
(如果未定义)内置在设备故障应用程序中。如果要更改此行为,则需要自定义该应用。这是通过使用自定义失败应用程序自定义此行为的较旧但仍然相关的指南:https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated
我正在将Devise与omniauth-google-oauth2 gem配合使用。我只想实现google sign_in。这是我的用户模型:
#app/models/user.rb
class User < ActiveRecord::Base
devise :omniauthable, :omniauth_providers => [:google_oauth2]
attr_accessible :email
def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
data = access_token.info
user = User.where(:email => data["email"]).first
unless user
user = User.create(email: data["email"] )
end
user
end
end
路由文件:
#config/routes.rb
Booking::Application.routes.draw do
#https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview#using-omniauth-without-other-authentications
devise_for :users , controllers: {omniauth_callbacks: "users/omniauth_callbacks"}
devise_scope :user do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
resources :events
end
根据官方文档,我在events_controller.rb中有这一行
before_filter :authenticate_user!, except: [:index, :show]
问题:如果我访问活动/新应用,则该应用要在/ sign_in中重定向我,这是错误消息:
Started GET "/events/new" for 127.0.0.1 at 2013-12-04 20:50:56 +0100
Processing by EventsController#new as HTML
Completed 401 Unauthorized in 0ms
Started GET "/sign_in" for 127.0.0.1 at 2013-12-04 20:50:56 +0100
Processing by Devise::SessionsController#new as HTML
Rendered /home/cristiano/.rvm/gems/ruby-1.9.2-p320@booking/gems/devise-3.2.2/app/views/devise/sessions/new.html.erb within layouts/application (13.6ms)
Completed 500 Internal Server Error in 17ms
ActionView::Template::Error (undefined method `session_path' for #<#<Class:0x007f42ecc695b8>:0x007f42ecc65b98>):
1: <h2>Sign in</h2>
2:
3: <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
4: <div><%= f.label :email %><br />
5: <%= f.email_field :email, :autofocus => true %></div>
6:
我想使用user_omniauth_authorize_path(:google_oauth2)网址而不是sign_path网址来重定向用户,因为我仅使用google登录名,并且我没有本地sign_in / sign_up
SOLUTION:] >>
将所有视图复制到我的应用程序中:
rails generate devise:views
编辑app / views / devise / sessions / new.html.erb删除所有内容,并将链接附加到Google身份验证:
#app/views/devise/sessions/new.html.erb <%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %>
感谢gregates
我正在将Devise与omniauth-google-oauth2 gem配合使用。我只想实现google sign_in。这是我的用户模型:#app / models / user.rb类User
因此,发送至new_user_session_path
或root_path
(如果未定义)内置在设备故障应用程序中。如果要更改此行为,则需要自定义该应用。这是通过使用自定义失败应用程序自定义此行为的较旧但仍然相关的指南:https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated
[但是,我认为您应该在new_user_session_path
(即app/views/devise/sessions/new.html.erb
)上创建一个自定义视图,该视图会提示您的用户使用google登录,而不是简单地自动登录。如果您将其发送到[ C0]并且他们已经授权了您的应用,并且他们具有当前的Google会话,那么它们将自动登录而没有任何提示,对我来说这是糟糕的UX。
这个问题很老,但是也许有人可以遇到同样的问题,例如我,;-)。这会将未登录的用户直接重定向到google新会话页面
user_omniauth_authorize_path(:google_oauth2)
devise :omniauthable, omniauth_providers: %i[google_oauth2]
class SessionsController < Devise::SessionsController
def new
redirect_to user_google_oauth2_omniauth_authorize_url
end
end
config.omniauth :google_oauth2, 'google_oauth2_api_key', 'google_oauth2_api_secret'
devise_for :users, controllers: { omniauth_callbacks: 'callbacks' }
devise_scope :user do
root 'sessions#new'
get 'logout', to: 'devise/sessions#destroy', as: :logout
get '/users/sign_in', to: 'sessions#new', as: :new_user_session
post '/users/sign_in', to: 'devise/sessions#create', as: :user_session
get '/users/sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session
end
这将产生@聚合认为用户体验不好的行为
因此,发送至new_user_session_path
或root_path
(如果未定义)内置在设备故障应用程序中。如果要更改此行为,则需要自定义该应用。这是通过使用自定义失败应用程序自定义此行为的较旧但仍然相关的指南:https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated
这个问题很老,但是也许有人可以遇到同样的问题,例如我,;-)。这会将未登录的用户直接重定向到google新会话页面