Skip to content

Instantly share code, notes, and snippets.

@madhums
Created November 16, 2010 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madhums/701671 to your computer and use it in GitHub Desktop.
Save madhums/701671 to your computer and use it in GitHub Desktop.
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