2017-06-08 7 views
0

私はレールのアプリでFacebookのログインを実装しようとしていると私は、次の例のコードを使用:メソッドを何https://github.com/rails-camp/facebook-omniauth-demoが、この場合に行う

# app/models/user.rb 

def self.new_with_session(params, session) 
    super.tap do |user| 
    if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] 
     user.email = data["email"] if user.email.blank? 
    end 
    end 
end 

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
    user.email = auth.info.email 
    user.password = Devise.friendly_token[0,20] 
    user.name = auth.info.name # assuming the user model has a name 
    user.image = auth.info.image # assuming the user model has an image 
    end 
end 

self.new_with_session(params, session)この場合は具体的には何ですか?

答えて

0

あなたは、new_with_sessionの機能を上書きしています。

def self.new_with_session(params, session) 
    # call super(the overwritten function), it will return a resource and in this case you are 
    # naming it 'user' in the block 
    super.tap do |user| 
    # if extra information was provided by facebook when user logged in, assign whatever comes in 
    # session["devise.facebook_data"]["extra"]["raw_info"] to 'data' variable 
    if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] 
     # assign 'email' given by facebook in case the 'user' object doesn't have one yet 
     user.email = data["email"] if user.email.blank? 
    end 
    end 
end 
+0

大変感謝しました! –

関連する問題