[英]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.