ruby on rails 4 - Omniauth-facebook & Devise: if email exists -


I am using omniauth-facebook and am ready to authenticate in my Rail 4 app. I would like a user who has already been authenticated through the scheme, if they choose, then Facebook can add it later.
I would like to know how to check whether the user's email account matches the Facebook user, and if so, add that UID and the provider to the registered user.

Currently, I get a divisive 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 If the session ["devise.user_attributes"] is new (session ["devise.user_attributes"], without protection: true) user.oauth_expires_at = time.at (auth.credentials.expires_at) end-end def self.new_with_session (parameter, Session) | User | User.attributes = params user.valid? End and Super End End  

Controller:

  Class OmniauthClallbacksController & lt; Devise :: OmniauthClallbacksController def All #render: text = & gt; "& Lt; ago" + Env ["omniauth.auth"]. To_yaml and user = user.from_omniauth (request.env ["omniauth.auth"]) if user.persisted? # Session [: user_id] = user.id # current_user for flash.notice = "sign in!" Sign_in_and_redirect user and session ["devise.user_attributes"] = user.attributes redirect_to new_user_registration_url end-end alias_method: facebook ,: all end  

thanks

< Div class = "post-text" itemprop = "text">

I had the same problem and this code was used to solve it.

It first checks whether email exists or not, and if it does, uid and provider fields, if I do not create a new user and with end parameters I only return the variable return_user and the stream works normally is.

  def self.from_omniauth (auth) if self where (email: auth.info.email) .exists? Return_user = self.where (email: auth.info.email) .first return_user.provider = auth.provider return_user.uid = auth.uid and return_user = self.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.et (auth.credentials.expires_at) end-end return_excessive termination  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -