omniauth を使用して、ユーザーがサインアップ/サインインできるようにしています。これを、既に導入している単純な認証ログイン/ログアウト システムの上に追加しようとしています。私はDeviseを使用していません。omniauth でログインするユーザーに、現在定義されている :signed_in_user と同じステータスを持たせるにはどうすればよいですか?
omniauth を使用してユーザーが実際にログインし、ログインしたページを表示する方法を理解しようとしていることを除いて、ほとんどのコードをセットアップしました。
最初に、これまでのところ動作しているように見える 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
最初の認証システムで使用される 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
これは私の 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
ユーザー_コントローラー
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
現在の認証システム (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