简体   繁体   English

使用Rails中的Omniauth重定向登录的用户

[英]Redirect logged in user using Omniauth in Rails

I've created a login controller so that people can log in using Facebook. 我创建了一个登录控制器,以便人们可以使用Facebook登录。 When a user logs in he gets routed to frontpage/show and when they logout they get redirected to the root_url and it shows the login page. 当用户登录时,他被路由到frontpage/show而当他们注销时,他们被重定向到root_url,并显示登录页面。

The problem is that when a user logs in through Facebook, closes the site and then revisits the page he also gets directed to the root_url and not frontpage/show . 问题在于,当用户通过Facebook登录时,关闭该站点,然后重新访问该页面,他还被定向到root_url而不是frontpage/show

I've used Omniauth with Rails for this. 我为此使用了Omniauth和Rails。

This is my session_controller.rb 这是我的session_controller.rb

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to '/frontpage/show'
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url
  end

end

and my frontpage_controller.rb 和我的frontpage_controller.rb

class FrontpageController < ApplicationController
  def show
  end
end

I've tried adding 我尝试添加

def show
  if authenticate_user?
    redirect_to :controller=>'dashboard', :action => 'index'
  else
    redirect_to '/public/example_html_file.html'
  end
end

To the frontpage_controller but it gives the error ndefined method authenticate_user` which makes sence. 给frontpage_controller,但它给出了错误的ndefined method authenticate_user`,该ndefined method会产生影响。 But I believe something like this is the answer. 但我相信答案就是这样。

Well, i'm assuming that you're using Devise . 好吧,我假设您正在使用Devise So, you have the method authenticate_user! 因此,您具有方法authenticate_user! and not authenticate_user? 而不是authenticate_user? .

Now, to check if the user is logged, you can use the user_signed_in? 现在,要检查用户是否已登录,可以使用user_signed_in? instead of authenticate_user? 而不是authenticate_user? and your code should works. 和您的代码应该工作。

More information about those helper methods here https://github.com/plataformatec/devise#controller-filters-and-helpers 有关这些帮助器方法的更多信息,请参见https://github.com/plataformatec/devise#controller-filters-and-helpers

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 omniauth 和 Facebook 对 Rails API 的用户进行身份验证? - Authenticate user using omniauth and Facebook for a rails API? 如何将使用omniauth(Rails with Devise)注册的用户重定向到特定页面? - How to redirect a user signed up with omniauth (Rails with Devise) to a specific page? Facebook omniauth rails重定向到URL,用户单击facebook按钮 - Facebook omniauth rails redirect to url, where user click facebook button 在Rails中使用Omniauth时,如果该用户存在并使用其他身份验证,则Google_omniauth2会创建一个新用户 - When using Omniauth in Rails, Google_omniauth2 creates a new user if the user exists and use different auth OmniAuth ruby​​ on rails,迫使facebook用户在当前登录facebook时重新进行身份验证 - OmniAuth ruby on rails, forcing facebook user to re-authenticate when currently logged into facebook 使用Rails作为omniauth提供程序 - Using rails as the omniauth provider 无法让新用户使用omniauth登录 - Can't keep new user logged in with omniauth 使用Omniauth和Devise为用户登录Rails应用程序创建密码 - Create password for user logging into Rails app using Omniauth and Devise Rails 3中的用户身份验证示例,使用Devise,OmniAuth,Mongoid和JSON响应 - User auth example in Rails 3, using Devise, OmniAuth, Mongoid, and JSON responses 当用户在Rails 4中使用omniauth登录时,路由到其他控制器吗? - Route to a different controller when a user is signed in using omniauth in Rails 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM