Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. class AuthenticationsController < ApplicationController
  2. skip_before_action :verify_authenticity_token, :only => :steam
  3.  
  4. def index
  5. @authentications = current_user.authentications.all
  6. end
  7. # <%= link_to 'Authentications', authentications_path %>
  8. def home
  9. end
  10.  
  11. def twitter
  12. omni = request.env["omniauth.auth"]
  13. authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
  14.  
  15. if authentication
  16. flash[:notice] = "Logged in Successfully"
  17. sign_in_and_redirect User.find(authentication.user_id)
  18. elsif current_user
  19. token = omni['credentials'].token
  20. token_secret = omni['credentials'].secret
  21.  
  22. current_user.authentications.create!(:provider => omni['provider'],
  23. :uid => omni['uid'],
  24. :token => token,
  25. :token_secret => token_secret)
  26. flash[:notice] = "Authentication successful."
  27. sign_in_and_redirect current_user
  28. else
  29. user = User.new
  30. user.apply_omniauth(omni)
  31.  
  32. if user.save
  33. flash[:notice] = "Logged in."
  34. sign_in_and_redirect User.find(user.id)
  35. else
  36. session[:omniauth] = omni.except('extra')
  37. redirect_to new_user_registration_path
  38. end
  39. end
  40. end
  41.  
  42. def destroy
  43. @authentication = Authentication.find(params[:id])
  44. @authentication.destroy
  45. redirect_to authentications_url, :notice => "Successfully destroyed authentication."
  46. end
  47.  
  48.  
  49. def facebook
  50. omni = request.env["omniauth.auth"]
  51. authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
  52.  
  53. if authentication
  54. flash[:notice] = "Logged in Successfully"
  55. sign_in_and_redirect User.find(authentication.user_id)
  56. elsif current_user
  57. token = omni['credentials'].token
  58. token_secret = ""
  59.  
  60. current_user.authentications.create!(:provider => omni['provider'],
  61. :uid => omni['uid'],
  62. :token => token,
  63. :token_secret => token_secret)
  64.  
  65. flash[:notice] = "Authentication successful."
  66. sign_in_and_redirect current_user
  67. else
  68. user = User.new
  69. user.email = omni['extra']['raw_info'].email
  70.  
  71. user.apply_omniauth(omni)
  72.  
  73. if user.save
  74. flash[:notice] = "Logged in."
  75. sign_in_and_redirect User.find(user.id)
  76. else
  77. session[:omniauth] = omni.except('extra')
  78. redirect_to new_user_registration_path
  79. end
  80. end
  81. end
  82.  
  83. def steam
  84. omni = request.env["omniauth.auth"]
  85. authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
  86.  
  87. if authentication
  88. flash[:notice] = "Logged in Successfully"
  89. sign_in_and_redirect User.find(authentication.user_id)
  90. elsif current_user
  91. token = omni['extra']['raw_info'].steamid
  92. # render :text => request.env["omniauth.auth"].info.to_hash.inspect
  93.  
  94. puts token
  95. token_secret = ""
  96.  
  97. current_user.authentications.create!(:provider => omni['provider'],
  98. :uid => omni['uid'],
  99. :token => token,
  100. :token_secret => token_secret)
  101. flash[:notice] = "Authentication successful."
  102. sign_in_and_redirect current_user
  103. else
  104. user = User.new
  105. user.apply_omniauth(omni)
  106. end
  107.  
  108. if user.save
  109. flash[:notice] = "Logged in."
  110. sign_in_and_redirect User.find(user.id)
  111. else
  112. session[:omniauth] = omni.except('extra')
  113. redirect_to new_user_registration_path
  114. end
  115. end
  116.  
  117.  
  118.  
  119.  
  120. end
  121.  
  122. def apply_omniauth(omni)
  123. authentications.build(:provider => omni['provider'],
  124. :uid => omni['uid'],
  125. :token => omni['credentials'].token,
  126. :token_secret => omni['credentials'].secret)
  127. end
  128.  
  129.  
  130. def password_required?
  131. (authentications.empty? || !password.blank?) && super #&& provider.blank?
  132. end
  133.  
  134. def self.from_omniauth(auth)
  135. where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  136. user.provider = auth.provider
  137. user.uid = auth.uid
  138. user.email = auth.info.email
  139. user.password = Devise.friendly_token[0,20]
  140. end
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement