Advertisement
Guest User

Untitled

a guest
Mar 21st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. def google_oauth2
  2. user = User.find_for_google_oauth2(request.env['omniauth.auth'])
  3.  
  4. if user.persisted?
  5. sign_in_and_redirect user, event: :authentication
  6. set_flash_message(:notice, :success, kind: 'GoogleOauth2') if is_navigational_format?
  7. else
  8. session['devise.google_oauth2_data'] = request.env['omniauth.auth']
  9. redirect_to new_user_registration_url
  10. end
  11. end
  12.  
  13. devise :database_authenticatable, :registerable,
  14. :recoverable, :rememberable, :trackable, :validatable,
  15. :omniauthable, omniauth_providers: [:facebook, :google_oauth2]
  16.  
  17. has_and_belongs_to_many :oauth_credentials
  18.  
  19. def self.find_for_google_oauth2(auth)
  20. where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  21. user.provider = auth.provider
  22. user.uid = auth.uid
  23. user.email = auth.info.email
  24. user.password = Devise.friendly_token[0,20] # Fake password for validation
  25. user.first_name = auth.info.name
  26. user.last_name = auth.info.nickname
  27. user.picture = auth.info.image
  28. user.token = auth.credentials.token
  29. end
  30. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement