omniauth_callbacks_controller.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # frozen_string_literal: true
  2. class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  3. # You should configure your model like this:
  4. # devise :omniauthable, omniauth_providers: [:twitter]
  5. def google_oauth2
  6. @user = User.from_omniauth(request.env['omniauth.auth'])
  7. if @user.persisted?
  8. # this will throw if @user is not activated
  9. sign_in_and_redirect @user, event: :authentication
  10. if is_navigational_format?
  11. set_flash_message(:notice, :success, kind: 'Google')
  12. end
  13. else
  14. session['devise.google_oauth2_data'] = request.env['omniauth.auth']
  15. redirect_to new_user_registration_url
  16. end
  17. end
  18. def reddit
  19. @user = User.from_omniauth(request.env['omniauth.auth'])
  20. if @user.persisted?
  21. # this will throw if @user is not activated
  22. sign_in_and_redirect @user, event: :authentication
  23. if is_navigational_format?
  24. set_flash_message(:notice, :success, kind: 'Reddit')
  25. end
  26. else
  27. session['devise.reddit_data'] = request.env['omniauth.auth']
  28. redirect_to new_user_registration_url
  29. end
  30. end
  31. def discord
  32. @user = User.from_omniauth(request.env['omniauth.auth'])
  33. if @user.persisted?
  34. # this will throw if @user is not activated
  35. sign_in_and_redirect @user, event: :authentication
  36. if is_navigational_format?
  37. set_flash_message(:notice, :success, kind: 'Discord')
  38. end
  39. else
  40. session['devise.discord_data'] = request.env['omniauth.auth']
  41. redirect_to new_user_registration_url
  42. end
  43. end
  44. # You should also create an action method in this controller like this:
  45. # def twitter
  46. # end
  47. # More info at:
  48. # https://github.com/plataformatec/devise#omniauth
  49. # GET|POST /resource/auth/twitter
  50. # def passthru
  51. # super
  52. # end
  53. # GET|POST /users/auth/twitter/callback
  54. # def failure
  55. # super
  56. # end
  57. # protected
  58. # The path used when OmniAuth fails
  59. # def after_omniauth_failure_path_for(scope)
  60. # super(scope)
  61. # end
  62. end