Created
July 23, 2012 16:52
-
-
Save warmwaffles/3164683 to your computer and use it in GitHub Desktop.
Omniaaaaaaaomg this is a mess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
# ... Initialize Devise and other crap .... | |
def self.authorize_omniauth(options={}, signed_in_resource=nil) | |
auth = Authentication.authorize(options[:auth][:provider], options[:auth][:uid]) | |
if auth | |
auth.set_omniauth_credentials(options[:auth]) | |
auth.save! | |
return auth.user | |
end | |
if signed_in_resource != nil && signed_in_resource.id? | |
auth = Authentication.new(options[:auth]) | |
auth.user = signed_in_resource | |
auth.save! | |
return signed_in_resource | |
end | |
user = nil | |
User.transaction do | |
auth = Authentication.new(options[:auth]) | |
unless options[:auth][:provider] == 'twitter' | |
user = User.find_by_email(options[:user][:email]) | |
end | |
if user | |
auth.user = user | |
auth.save! | |
else | |
options[:user][:password] = Devise.friendly_token[0,20] | |
options[:user][:password_confirmation] = options[:auth][:password] | |
user = User.create(options[:user]) | |
auth.user = user | |
auth.save! | |
end | |
return user | |
end | |
false | |
end | |
def self.authorize_facebook(access_token, signed_in_resource=nil) | |
info = access_token.info | |
options = { | |
user: { | |
email: info.email, | |
first_name: info.first_name, | |
last_name: info.last_name | |
}, | |
auth: { | |
provider: 'facebook', | |
uid: access_token.uid, | |
uemail: info.email, | |
uname: info.name, | |
image_url: info.image, | |
token: access_token.credentials.token | |
} | |
} | |
return User.authorize_omniauth(options, signed_in_resource) | |
end | |
def self.authorize_twitter(access_token, signed_in_resource=nil) | |
info = access_token.info | |
options = { | |
user: { | |
email: nil, | |
first_name: nil, | |
last_name: nil | |
}, | |
auth: { | |
provider: 'twitter', | |
uid: access_token.uid, | |
uemail: nil, | |
uname: info.name, | |
image_url: info.image, | |
token: access_token.credentials.token | |
} | |
} | |
return User.authorize_omniauth(options, signed_in_resource) | |
end | |
def self.authorize_google(access_token, signed_in_resource=nil) | |
info = access_token.info | |
options = { | |
user: { | |
email: info.email, | |
first_name: info.first_name, | |
last_name: info.last_name | |
}, | |
auth: { | |
provider: 'google', | |
uid: access_token.uid, | |
uemail: info.email, | |
uname: info.name, | |
image_url: nil, | |
token: nil # TODO need to find a use for this | |
} | |
} | |
return User.authorize_omniauth(options, signed_in_resource) | |
end | |
def self.authorize_google_oauth2(access_token, signed_in_resource=nil) | |
info = access_token.info | |
options = { | |
user: { | |
email: info.email, | |
first_name: info.first_name, | |
last_name: info.last_name | |
}, | |
auth: { | |
provider: 'google-oauth2', | |
uid: access_token.uid, | |
uemail: info.email, | |
uname: info.name, | |
image_url: info.image, | |
token: access_token.credentials.token | |
} | |
} | |
return User.authorize_omniauth(options, signed_in_resource) | |
end | |
def self.authorize_github(access_token, signed_in_resource=nil) | |
info = access_token.info | |
options = { | |
user: { | |
email: info.email, | |
first_name: info.name.split(' ').first, | |
last_name: info.name.split(' ').last | |
}, | |
auth: { | |
provider: 'github', | |
uid: access_token.uid, | |
uemail: info.email, | |
uname: info.name, | |
image_url: nil, | |
token: access_token.credentials.token | |
} | |
} | |
return User.authorize_omniauth(options, signed_in_resource) | |
end | |
# ... more stuff later ... | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment