omniauth_callbacks_controller.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # frozen_string_literal: true
  2. class OmniauthCallbacksController < ApplicationController
  3. include CookieBasedCsrf
  4. include UserAuthentication
  5. def create
  6. case params[:provider]
  7. when 'google_oauth2'
  8. google_oauth2
  9. when 'reddit'
  10. reddit
  11. when 'discord'
  12. discord
  13. else
  14. head :not_found
  15. end
  16. end
  17. private
  18. def google_oauth2
  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: 'Google')
  25. # end
  26. else
  27. redirect_to new_user_registration_url
  28. end
  29. end
  30. def reddit
  31. @user = User.from_omniauth(request.env['omniauth.auth'])
  32. if @user.persisted?
  33. # this will throw if @user is not activated
  34. sign_in_and_redirect @user, event: :authentication
  35. # if is_navigational_format?
  36. # set_flash_message(:notice, :success, kind: 'Reddit')
  37. # end
  38. else
  39. redirect_to new_user_registration_url
  40. end
  41. end
  42. def discord
  43. @user = User.from_omniauth(request.env['omniauth.auth'])
  44. if @user.persisted?
  45. # this will throw if @user is not activated
  46. sign_in_and_redirect @user, event: :authentication
  47. # if is_navigational_format?
  48. # set_flash_message(:notice, :success, kind: 'Discord')
  49. # end
  50. else
  51. redirect_to new_user_registration_url
  52. end
  53. end
  54. def sign_in_and_redirect(user, *_args)
  55. # Ensure we have a new CSRF token now that user is signed in
  56. cookies.delete(:_csrf_token)
  57. self.current_user = user
  58. cookies['x-csrf-token'] = {
  59. value: form_authenticity_token,
  60. httponly: false,
  61. secure: !(Rails.env.development? || Rails.env.test?),
  62. }
  63. redirect_to root_path
  64. end
  65. # More info at:
  66. # https://github.com/plataformatec/devise#omniauth
  67. # GET|POST /resource/auth/twitter
  68. # def passthru
  69. # super
  70. # end
  71. # GET|POST /users/auth/twitter/callback
  72. # def failure
  73. # super
  74. # end
  75. # protected
  76. # The path used when OmniAuth fails
  77. # def after_omniauth_failure_path_for(scope)
  78. # super(scope)
  79. # end
  80. end