Ruby on rails 使用OmniAuth而不使用Desive时获取其他参数?

Ruby on rails 使用OmniAuth而不使用Desive时获取其他参数?,ruby-on-rails,ruby,ruby-on-rails-3,omniauth,Ruby On Rails,Ruby,Ruby On Rails 3,Omniauth,嗨,我在应用程序中使用omniauth。我没有使用Desive就创建了这个应用程序。所以我手工编写了所有的加密方法。我试图将imniauth与我的应用程序集成,但我遇到了大量的问题。我制作了一个身份验证控制器: class AuthenticationsController < ApplicationController def index @authentications = current_user.authentications if current_user end

嗨,我在应用程序中使用omniauth。我没有使用Desive就创建了这个应用程序。所以我手工编写了所有的加密方法。我试图将imniauth与我的应用程序集成,但我遇到了大量的问题。我制作了一个身份验证控制器:

class AuthenticationsController < ApplicationController
  def index
    @authentications = current_user.authentications if current_user
  end

  def create
    omniauth = request.env["omniauth.auth"]
    authentication= Authentication.find_by_provider_and_uid(omniauth['provider'],omniauth['uid'])
    if authentication
      flash[:notice]= "Signed in with " + omniauth['provider'] + " Successfully"
      sign_in(authentication.user)
      redirect_back_or authentication.user
    elsif current_user
      current_user.authentications.create!(:provider => omniauth['provider'],:uid => omniauth['uid'])
      flash[:notice]="authentication successfull"
      redirect_to authentications_url
    else

   authenticate= Authentication.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
   flash[:notice] = "You still need to fill up the following"
   # This is where I have doubt as to how should I proceed. Should I first create authentication for omniauth and then redirect or just send everything together and then create in sign up?
   # session[:omniauth] = omniauth.except('extra')
   #  redirect_to signup_path(omniauth)

 end
end

  def destroy
    @authentication = current_user.authentications.find(params[:id])
    @authentication.destroy
    flash[:notice] = "Successfully destroyed authentication."
    redirect_to authentications_url
  end
end
类身份验证控制器omniauth['provider'],:uid=>omniauth['uid'])
flash[:notice]=“身份验证成功”
将\u重定向到身份验证\u url
其他的
authenticate=Authentication.create!(:provider=>omniauth['provider'],:uid=>omniauth['uid'])
flash[:注意]=“您仍然需要填写以下内容”
#这就是我怀疑我应该如何继续的地方。我应该先为omniauth创建身份验证,然后重定向,还是将所有内容一起发送,然后在注册中创建?
#会话[:omniauth]=omniauth.except('extra')
#重定向到注册路径(omniauth)
结束
结束
def销毁
@authentication=当前用户.authentications.find(参数[:id])
@身份验证。销毁
flash[:notice]=“已成功销毁身份验证。”
将\u重定向到身份验证\u url
结束
结束
我的用户控制器如下所示:

class UsersController < ApplicationController
  before_filter :authenticate, :except => [:show, :new, :create]
  before_filter :correct_user, :only => [:edit, :update]
  before_filter :admin_user,   :only => :destroy

# Id ont know what to do with omniauth here! :( 
  def new(omniauth)
    @user  = User.new
    @title = "Sign up"
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      redirect_to @user, :flash => { :success => "Welcome to the World Student Alliance!" }
    else
      @title = "Sign up"
      render 'new'
    end
  end

  def edit
    @title = "Edit user"
  end

  def update
    if @user.update_attributes(params[:user])
      redirect_to @user, :flash => { :success => "Profile updated." }
    else
      @title = "Edit user"
      render 'edit'
    end
  end

  def destroy
    @user.destroy
    redirect_to users_path, :flash => { :success => "User destroyed." }
  end

  private

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end

    def admin_user
      @user = User.find(params[:id])
      redirect_to(root_path) if !current_user.admin? || current_user?(@user)
    end
end
class SessionsController < ApplicationController
  skip_before_filter :check_sign_in, :only => [:new, :create]
  def new
    @title = "Sign in"
  end

  def create
    user = User.authenticate(params[:session][:email],
                             params[:session][:password])
    if user.nil?
      flash.now[:error] = "Invalid email/password combination."
      @title = "Sign in"
      render 'new'
    else
      sign_in user
      redirect_back_or user
    end
  end

  def destroy
    sign_out
    redirect_to root_path
  end
end
class UsersController[:显示,:新建,:创建]
在\u筛选器之前:更正\u用户,:only=>[:编辑,:更新]
在\u filter:admin\u user之前,:only=>:destroy
#我不知道如何处理omniauth( 
def新建(omniauth)
@user=user.new
@title=“注册”
结束
def创建
@user=user.new(参数[:user])
如果@user.save
在@user中登录
将_重定向到@user:flash=>{:success=>“欢迎加入世界学生联盟!”)
其他的
@title=“注册”
呈现“新”
结束
结束
定义编辑
@title=“编辑用户”
结束
def更新
如果@user.update_属性(参数[:user])
将_重定向到@user,:flash=>{:success=>“配置文件已更新。”}
其他的
@title=“编辑用户”
渲染“编辑”
结束
结束
def销毁
@用户破坏
将\重定向到用户\路径:flash=>{:success=>“User destromed.”
结束
私有的
def正确的用户
@user=user.find(参数[:id])
重定向到(根路径),除非当前用户(@user)
结束
def管理员用户
@user=user.find(参数[:id])
将_重定向到(根路径)if!current_user.admin?| current_user?(@user)
结束
结束
和会话控制器如下所示:

class UsersController < ApplicationController
  before_filter :authenticate, :except => [:show, :new, :create]
  before_filter :correct_user, :only => [:edit, :update]
  before_filter :admin_user,   :only => :destroy

# Id ont know what to do with omniauth here! :( 
  def new(omniauth)
    @user  = User.new
    @title = "Sign up"
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      redirect_to @user, :flash => { :success => "Welcome to the World Student Alliance!" }
    else
      @title = "Sign up"
      render 'new'
    end
  end

  def edit
    @title = "Edit user"
  end

  def update
    if @user.update_attributes(params[:user])
      redirect_to @user, :flash => { :success => "Profile updated." }
    else
      @title = "Edit user"
      render 'edit'
    end
  end

  def destroy
    @user.destroy
    redirect_to users_path, :flash => { :success => "User destroyed." }
  end

  private

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end

    def admin_user
      @user = User.find(params[:id])
      redirect_to(root_path) if !current_user.admin? || current_user?(@user)
    end
end
class SessionsController < ApplicationController
  skip_before_filter :check_sign_in, :only => [:new, :create]
  def new
    @title = "Sign in"
  end

  def create
    user = User.authenticate(params[:session][:email],
                             params[:session][:password])
    if user.nil?
      flash.now[:error] = "Invalid email/password combination."
      @title = "Sign in"
      render 'new'
    else
      sign_in user
      redirect_back_or user
    end
  end

  def destroy
    sign_out
    redirect_to root_path
  end
end
class sessioncontroller[:新建,:创建]
def新
@title=“登录”
结束
def创建
user=user.authenticate(参数[:会话][:电子邮件],
参数[:会话][:密码])
如果user.nil?
flash.now[:error]=“无效的电子邮件/密码组合。”
@title=“登录”
呈现“新”
其他的
登录用户
将\u重定向回\u或用户
结束
结束
def销毁
注销
将\重定向到根\路径
结束
结束
每个用户都有许多身份验证,每个身份验证都属于一个用户

class Authentication < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :authentications
.......
类身份验证
以下是我考虑的场景:

  • 用户已登录并希望与其twitter连接。(已完成,无错误)
  • 用户已经是注册用户,每次都想使用twitter登录(完成时没有错误)

  • 用户未注册,希望使用twitter进行身份验证。如果基本身份验证已完成,但我需要让用户输入一些详细信息,如国家、性别、部门等。这就是一切出错的地方。我可以创建身份验证并将其转送到登录表单,但当用户从fb或twitter他不必输入密码字段。这是我的问题。任何人都可以让我知道我应该做什么来克服这个错误。谢谢


  • 我在最近的一个应用程序中做了类似的事情——使用Omniauth进行身份验证,并允许每个用户进行多次身份验证;然后将他们发送到特定的表单(而不是注册表单)以填写其个人资料的其余部分

    仅当必填字段不完整时,此“额外”表单才起作用,否则将重定向到用户显示页面。似乎对我很管用

    编辑:将您当前的用户名/密码gubbins替换为Omniauth标识策略:。这样,您的所有身份验证都由Omniauth处理。相信我,如果你拿出你手工制作的密码认证码,用Omniauth身份替换它,你的生活会更简单


    为免生疑问,Twitter和身份策略都到位,这意味着您让用户选择如何进行身份验证。他们可以使用Twitter或用户名/密码进行身份验证。但是,如果他们都使用Twitter,那么你永远看不到密码信息(这很好)。

    你检查过这些Rails强制转换吗?是的!我用过,但我没有用omniauth。我想你的意思是你没有用Desive。omniauth还有另外一个视频:谢谢你的建议。这也是我想做的。日分