Advertisement
Guest User

Untitled

a guest
May 13th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. module ApplicationHelper
  2. def avatar_url(user)
  3. if user.avatar
  4. user.avatar
  5. else
  6. "/images/missing.png"
  7. end
  8. end
  9.  
  10. class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  11.  
  12. def facebook
  13. @user = User.from_omniauth(request.env["omniauth.auth"])
  14.  
  15. if @user.persisted?
  16. sign_in_and_redirect @user, :event => :authentication
  17. set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
  18. else
  19. session["devise.facebook_data"] = request.env["omniauth.auth"]
  20. redirect_to new_user_registration_url
  21. end
  22. end
  23.  
  24. def google_oauth2
  25. @user = User.from_omniauth(request.env['omniauth.auth'])
  26.  
  27. if @user.persisted?
  28. sign_in_and_redirect @user, event: :authentication
  29. set_flash_message(:notice, :success, :kind => "Google") if is_navigational_format?
  30. else
  31. redirect_to root_path, flash: { error: 'Authentication failed!' }
  32. end
  33.  
  34. class User < ActiveRecord::Base
  35.  
  36. def self.from_omniauth(auth)
  37. where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  38. user.fullname = auth.info.name
  39. user.provider = auth.provider
  40. user.uid = auth.uid
  41. user.email = auth.info.email
  42. user.avatar = auth.info.avatar
  43. user.password = Devise.friendly_token[0,20]
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement