Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
authorizations controller
#app/controllers/authorizations_controller.rb
class AuthorizationsController < ApplicationController
before_filter :require_user, :only => [:destroy]
def create
omniauth = request.env['rack.auth'] #this is where you get all the data from your provider through omniauth
@auth = Authorization.find_from_hash(omniauth)
if current_user
flash[:notice] = "Successfully added #{omniauth['provider']} authentication"
current_user.authorizations.create(:provider => omniauth['provider'], :uid => omniauth['uid']) #Add an auth to existing user
elsif @auth
flash[:notice] = "Welcome back #{omniauth['provider']} user"
#create session
#UserSession.create(@auth.user, true) # if you are using authlogic, this will create the session
#User is present. Login the user with his social account
else
@new_auth = Authorization.create_from_hash(omniauth, current_user) #Create a new user
flash[:notice] = "Welcome #{omniauth['provider']} user. Your account has been created."
#create session
#UserSession.create(@auth.user, true) # if you are using authlogic, this will create the session
end
redirect_to root_url
end
def failure
flash[:notice] = "Sorry, You din't authorize"
redirect_to root_url
end
def destroy
@authorization = current_user.authorizations.find(params[:id])
flash[:notice] = "Successfully deleted #{@authorization.provider} authentication."
@authorization.destroy
redirect_to profile_path(current_user.username)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.