Reputation: 203
If I have omniauth to allow users to log in using a provider such as facebook/twitter, how can I check to see if the current_user is logged in using omniauth?
I want to allow a code to run only if the current_user is logged in through omniauth.
Here's how my schema looks
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.timestamp "created_at", :null => false
t.timestamp "updated_at", :null => false
t.string "password_digest"
t.string "remember_token"
end
create_table "authentications", :force => true do |t|
t.integer "user_id"
t.string "provider"
t.string "uid"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "secret"
t.string "token"
end
Controller
class PostsController < ApplicationController
def create
@post = current_user.posts.build(params[:post])
if @post.save
if @post.check
UserMailer.check_email(@user).deliver
else
flash[:success] = "Posted"
Twitter::Client.new.update(@post.content) if logged_using_omniauth? request != nil
end
redirect_to root_path
else
@feed_items = []
render 'static_pages/home'
end
end
Upvotes: 1
Views: 3433
Reputation: 6786
I would recommend to maintain a session to check if its logged in through omniauth.
def method_name
.......
#if logged in set the session
session[:logged_in_using_omniauth] = true
.......
end
def logged_in_using_omniauth?
session[:logged_in_using_omniauth].present?
end
And expose that method as helper method by:
helper_method :logged_in_using_omniauth?
Now use logged_in_using_omniauth?
anywhere in controllers or views
Upvotes: 6
Reputation: 7338
You can verify it the following way:
def logged_using_omniauth? request
res = nil
omniauth = request.env["omniauth.auth"]
res = Authentication.find_by_provider_and_uid ↵
(omniauth['provider'], omniauth['uid']) if omniauth
res
end
If the returned authentication
is not nil
then they logged in using omniauth.
Upvotes: 2