-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathauthentor.rb
51 lines (40 loc) · 1.51 KB
/
authentor.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
require "auth0"
class Authentor
attr_accessor :auth0, :auth0_users
def initialize
@auth0 = Auth0Client.new(
:api_version => 2,
:token => Rails.application.secrets.auth0_api_token,
:domain => Rails.application.secrets.auth0_api_domain
)
@api_params = {connection: Rails.application.secrets.auth0_connection}
end
def create_user(params)
new_auth0_user = @auth0.create_user(params[:email], @api_params.merge(params))
User.create_from_auth0_user(new_auth0_user)
end
def fetch_user(auth0_id)
auth0_user = @auth0.user(auth0_id)
user = User.create_from_auth0_user auth0_user
Rails.logger.info "Created user from auth0 user #{auth0_user}"
end
def fetch_users
find_user_count = @auth0.get_users({per_page: 0, page: 0, include_totals: true})
total = find_user_count["total"]
#If total is 5400, the page should be 53, because the first page is 0.
go_to_page = (total - 1) / 100
@auth0_users = @auth0.get_users({per_page: 100, page: go_to_page})
puts "GOING TO PAGE #{total}, found users with count #{@auth0_users.count}"
if !@auth0_users || @auth0_users.empty?
Rails.logger.error "Auth0 did not return users when attempting to Sync!"
end
new_users = new_auth0_users
new_users.each do |auth0_user|
user = User.create_from_auth0_user auth0_user
Rails.logger.info "Created user from auth0 user #{auth0_user}"
end
end
def new_auth0_users
@auth0_users.select{|e| !User.exists?(auth0_id: e["user_id"])}
end
end