Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AuthenticationsController < ApplicationController
- skip_before_action :verify_authenticity_token, :only => :steam
- def index
- @authentications = current_user.authentications.all
- end
- # <%= link_to 'Authentications', authentications_path %>
- def home
- end
- def twitter
- omni = request.env["omniauth.auth"]
- authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
- if authentication
- flash[:notice] = "Logged in Successfully"
- sign_in_and_redirect User.find(authentication.user_id)
- elsif current_user
- token = omni['credentials'].token
- token_secret = omni['credentials'].secret
- current_user.authentications.create!(:provider => omni['provider'],
- :uid => omni['uid'],
- :token => token,
- :token_secret => token_secret)
- flash[:notice] = "Authentication successful."
- sign_in_and_redirect current_user
- else
- user = User.new
- user.apply_omniauth(omni)
- if user.save
- flash[:notice] = "Logged in."
- sign_in_and_redirect User.find(user.id)
- else
- session[:omniauth] = omni.except('extra')
- redirect_to new_user_registration_path
- end
- end
- end
- def destroy
- @authentication = Authentication.find(params[:id])
- @authentication.destroy
- redirect_to authentications_url, :notice => "Successfully destroyed authentication."
- end
- def facebook
- omni = request.env["omniauth.auth"]
- authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
- if authentication
- flash[:notice] = "Logged in Successfully"
- sign_in_and_redirect User.find(authentication.user_id)
- elsif current_user
- token = omni['credentials'].token
- token_secret = ""
- current_user.authentications.create!(:provider => omni['provider'],
- :uid => omni['uid'],
- :token => token,
- :token_secret => token_secret)
- flash[:notice] = "Authentication successful."
- sign_in_and_redirect current_user
- else
- user = User.new
- user.email = omni['extra']['raw_info'].email
- user.apply_omniauth(omni)
- if user.save
- flash[:notice] = "Logged in."
- sign_in_and_redirect User.find(user.id)
- else
- session[:omniauth] = omni.except('extra')
- redirect_to new_user_registration_path
- end
- end
- end
- def steam
- omni = request.env["omniauth.auth"]
- authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])
- if authentication
- flash[:notice] = "Logged in Successfully"
- sign_in_and_redirect User.find(authentication.user_id)
- elsif current_user
- token = omni['extra']['raw_info'].steamid
- # render :text => request.env["omniauth.auth"].info.to_hash.inspect
- puts token
- token_secret = ""
- current_user.authentications.create!(:provider => omni['provider'],
- :uid => omni['uid'],
- :token => token,
- :token_secret => token_secret)
- flash[:notice] = "Authentication successful."
- sign_in_and_redirect current_user
- else
- user = User.new
- user.apply_omniauth(omni)
- end
- if user.save
- flash[:notice] = "Logged in."
- sign_in_and_redirect User.find(user.id)
- else
- session[:omniauth] = omni.except('extra')
- redirect_to new_user_registration_path
- end
- end
- end
- def apply_omniauth(omni)
- authentications.build(:provider => omni['provider'],
- :uid => omni['uid'],
- :token => omni['credentials'].token,
- :token_secret => omni['credentials'].secret)
- end
- def password_required?
- (authentications.empty? || !password.blank?) && super #&& provider.blank?
- end
- def self.from_omniauth(auth)
- where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
- user.provider = auth.provider
- user.uid = auth.uid
- user.email = auth.info.email
- user.password = Devise.friendly_token[0,20]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement