简体   繁体   English

如何使用户正确登录Rails 3? 而不只是重定向到访问者首页

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

相关问题 在Rails中对子控制器执行操作后,如何控制访问者重定向到的位置? - After performing an action on a child controller in Rails, how can I control where a visitor is redirected to? 如何在Rails中使用ActiveRecord仅获得一个记录而不是重复记录? - How can I just get one record instead of duplicates using ActiveRecord in Rails? Ruby-on-Rails:如何摆脱“你被重定向”页面 - Ruby-on-Rails: How to get rid of “you are being redirected” page ruby on rails:页面自动重定向 - ruby on rails :-page automatically getting redirected Rails-如何获得喜欢故事的用户 - Rails - How can i get the users who like the story 我怎样才能让 Rails 将 javascript 错误发送到日志,而不是警报? - How can I get rails to send javascript errors to the log, not alert? 在Rails中,有时我只是获取文本而不是呈现html - In rails, sometimes I just get text instead of rendering html Rails - 给定一组用户 - 如何获得电子邮件的输出? - Rails - given an array of Users - how to get a output of just emails? 如何知道页面是在Rails中呈现还是重定向? - How to know the page is rendered or redirected in Rails? 使用 geocoder gem for rails,我如何获得访问者的城市? - Using the geocoder gem for rails, how do i get the visitor's city?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM