0

こんにちは、アプリケーションで omniauth を使用しています。deviseを使わずにアプリケーションを作成しました。そのため、暗号化のすべてのメソッドを手動で作成しました。アプリケーションに 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

私のユーザーコントローラーは次のとおりです。

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 Authentication < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :authentications
.......

以下は、私が検討したシナリオです。

  1. ユーザーはログインしており、自分の Twitter に接続したいと考えています。(エラーなしで完了)
  2. ユーザーはすでに登録ユーザーであり、毎回サインインに twitter を使用したい (エラーなしで完了)

  3. ユーザーは登録されておらず、認証に Twitter を使用したいと考えています。基本認証が完了したら、国、性別、部門などの詳細をユーザーに入力させる必要があります。ここですべてが間違っています。認証を作成してサインインフォームにエスケープすることはできますが、ユーザーが fb または twitter からサインインしている場合、パスワードフィールドに入力する必要はありません。これは私の問題です。このエラーを乗り越えるために何をすべきか誰か教えてください。ありがとうございました。

4

1 に答える 1

1

私は最近のアプリケーションで同様のことを行います - 認証に Omniauth を使用し、ユーザーごとに複数の認証を許可します。あなたの場合 3.) 最初にユーザーと認証を作成します。次に、プロファイルの残りの部分を記入するために、特定のフォーム (サインアップ フォームではない) に送信します。

この「余分な」フォームは、必須フィールドが完全でない場合にのみ機能します。それ以外の場合は、ユーザーの表示ページにリダイレクトされます。私にはうまくいくようです。

編集: 現在のユーザー名/パスワードのガビンを Omniauth Identiy 戦略に置き換えます: https://github.com/intridea/omniauth-identity。このようにして、すべての認証が Omniauth によって処理されます。信じてください。手作りのパスワード認証コードを取り出して Omniauth ID に置き換えれば、あなたの人生はよりシンプルになります。

誤解を避けるために、Twitter と ID の両方の戦略を導入するということは、ユーザーが認証方法を選択できるようにすることを意味します。認証には、Twitter またはユーザー名/パスワードのいずれかを使用できます。どちらも同じ仕事をしますが、Twitter を使用している場合は、パスワード情報が表示されることはありません (これは問題ありません)。

于 2012-05-02T11:54:34.133 に答える