I'm using omniauth-facebook and devise for authentication in my rails 4 app.
I would like for a user, that already has authenticated through devise, also be able to add facebook auth later if they choose.
I would like to figure out how to check if the email of the devise logged user matches that of a facebook user, and if yes, add the uid and provider to that registered user.
Currently, I am getting the devise error message: "Email has already been taken"
User.rb
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.username = auth.info.username
user.email = auth.info.email
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new(session["devise.user_attributes"], without_protection: true) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
Controller:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
#render :text => "<pre>" + env["omniauth.auth"].to_yaml and return
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
#session[:user_id] = user.id # for current_user
flash.notice = "Signed in!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
alias_method :facebook, :all
end
Thanks!!!!