[英]how can I get users to log in correctly on rails 3? instead of just getting redirected to the visitor front page
I'm using omniauth to allows users to sign up/sign in. I'm trying to add this on top of a simple authentication log in/log out system that I already have in place. 我正在使用omniauth允许用户注册/登录。我试图将其添加到我已经安装的简单身份验证登录/注销系统的顶部。 I am not using Devise.
我没有使用Devise。 How can I have a user who logs in with omniauth have the same status as currently defined :signed_in_user?
我如何让使用omniauth登录的用户具有与当前定义的:signed_in_user相同的状态?
I have most of the code set up except I'm trying to figure out how to get the user to actually log in and show their logged in page when doing it with omniauth. 我已经完成了大多数代码设置,只是我想弄清楚如何使用omniauth来使用户实际登录并显示其登录页面。
First here's the omniauth authentications_controller which seems to work so far 首先,这里是omniauth authentications_controller,到目前为止,它似乎仍然有效
def create
omniauth = request.env['omniauth.auth']
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
flash[:success] = "Signed in successfully"
sign_in_and_redirect User.find(authentication.user_id)
elsif current_user
token = omniauth['credentials'].token
secret = omniauth['credentials'].secret
current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => token, :secret => token_secret)
flash[:success] = "Authentication successful"
sign_in_and_redirect current_user
else
user = User.new
user.apply_omniauth(omniauth)
if user.save!
flash[:success] = "Account created"
sign_in_and_redirect User.find(user.id)
else
session[:omniauth] = omniauth.except('extra')
redirect_to '/signup'
end
end
end
Here's the sessions_controller which is used by the first authentication system 这是第一个身份验证系统使用的sessions_controller
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to root_url
else
flash.now[:error] = "Invalid email/password combination"
render 'new'
end
end
def destroy
sign_out
redirect_to root_path
end
end
This is my sessions_helper module SessionsHelper 这是我的sessions_helper模块SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
current_user = user
end
def sign_in_and_redirect(user)
#what should go here?#
end
Users_controller Users_controller
Class UsersController < ApplicationController
before_filter :signed_in_user,
only: [:index, :edit, :update, :destroy]
before_filter :correct_user, only: [:edit, :update]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome!"
redirect_to root_url
else
render 'new'
end
end
With my current authentication system (not omniauth), a redirect to root_url makes the signed-in user go to 'static_pages#home' 使用我当前的身份验证系统(不是omniauth),重定向到root_url可使登录的用户转到“ static_pages#home”
class StaticPagesController < ApplicationController
def home
if signed_in?
@post = current_user.posts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
I would move the sign_in
helper functions to ApplicationHelper
so that you can use them in places other than the SessionsController
. 我会将
sign_in
帮助ApplicationHelper
函数移至ApplicationHelper
以便可以在SessionsController
之外的其他地方使用它们。
After that, it should be very simple. 之后,它应该非常简单。
def sign_in_and_redirect user
sign_in user
redirect_to root_url # or wherever you want
end
It might even be easier to just do sign_in user and redirect_to root_url
instead of using another helper. 仅使用
sign_in user and redirect_to root_url
而不是使用其他帮助sign_in user and redirect_to root_url
甚至可能更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.