This repository has been archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.rb
78 lines (71 loc) · 2.92 KB
/
users.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#coding: utf-8
module NoiteHoje
module Users
def create_new_account user_hash
new_user = api_helper.new_user(
first_name: user_hash[:name].split.first,
last_name: user_hash[:name].split[1..-1].join(' '),
email: user_hash[:email],
provider: 'facebook',
uid: user_hash[:uid],
access_token: user_hash[:token],
secret: user_hash[:secret],
image: user_hash[:image])
if new_user['error'].present?
flash[:error] = 'Desculpe, ocorreu um erro ao criar a sua conta.'
#TODO: Log the error
return
end
# signin existing user
# in the session his user id and the service id used for signing in is stored
session[:user_id] = new_user['id']
session[:service] = 'facebook'
end
def create_authhash omniauth, service
# create a new hash
@authhash = Hash.new
if service == 'facebook'
set_authhash_value :email, omniauth['extra']['raw_info']['email']
set_authhash_value :name, omniauth['extra']['raw_info']['name']
set_authhash_value :uid, omniauth['extra']['raw_info']['id']
set_authhash_value :image, omniauth['extra']['raw_info']['image']
set_authhash_value :provider, omniauth['provider']
set_authhash_value :token, omniauth['credentials']['token']
set_authhash_value :secret, omniauth['credentials']['secret']
elsif service == 'foursquare'
set_authhash_value :email, omniauth['extra']['raw_info']['contact']['email']
set_authhash_value :name, omniauth['info']['name']
set_authhash_value :uid, omniauth['extra']['raw_info']['id']
set_authhash_value :phone, omniauth['extra']['raw_info']['phone']
set_authhash_value :provider, omniauth['provider']
# Skittles.configure do |config|
# config.access_token = omniauth['credentials']['token'] if ENV["RACK_ENV"] != "test"
# end
elsif service == 'twitter'
set_authhash_value :email, omniauth['info']['email']
set_authhash_value :name, omniauth['info']['name']
set_authhash_value :uid, omniauth['uid']
set_authhash_value :provider, omniauth['provider']
# Twitter.configure do |config|
# config.oauth_token = omniauth['credentials']['token']
# config.oauth_token_secret = omniauth['credentials']['secret']
# end
else
# debug to output the hash that has been returned when adding new services
puts "Invalid service in hash received: #{omniauth.to_yaml}"
end
end
def set_authhash_value key, value
@authhash[key] = value.present? ? value.to_s : ""
end
def user_signed_in?
return 1 if current_user
end
def authenticate_user!
if !current_user
flash[:error] = 'You need to sign in before accessing this page!'
redirect '/signin'
end
end
end
end