sync_attr_with_auth0 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d47864e5208a3f13c3ff36733e1ee8dcac70253
4
- data.tar.gz: 83cb612d3328cb3edbf86573b81bc1e7aa64cce5
3
+ metadata.gz: 1349703e7bb7429f17470b43f1bb8d9f8991f59f
4
+ data.tar.gz: 0a2de574896e0cd1505c1fcc9641fdedc906d2e2
5
5
  SHA512:
6
- metadata.gz: 03e2982ecd4fbea464189dcec8b2f92c0307c702f44e44657f7e0d3bce085c69441ebd7bfe6470dca9b41a5f2ef781710c91d5f7dd4a33c0cb08b96c27c56f54
7
- data.tar.gz: 9fdfc779cac98b76990d802917661e172cc729a22b5d1f688e47b66255084f53f3a51552a86010396663496018e740bc2a61cd489c4728a0e50cc0fd9a5b908f
6
+ metadata.gz: 1ac3a2fdf6a6530d79668202a629ad60cabdd44a290c4067b4a039f576c2a354eff6fb1c6b66b5420e52fee91d3c3cb97d83d525b63d7a0d06a59853f294d0d9
7
+ data.tar.gz: e620c9046049335bc937e3deb0a5f0d457a3a1331a2c9558c2a47d361231237ddf2c06cb0670065c37d54784e268a4f230b9aae6c3602ea288f0498b179cff25
@@ -13,8 +13,8 @@ module SyncAttrWithAuth0
13
13
  merge_default_options(options)
14
14
 
15
15
  after_validation :validate_email_with_auth0
16
- after_create :create_user_in_auth0
17
- after_update :sync_attr_with_auth0
16
+ after_create :auth0_create
17
+ after_update :auth0_update
18
18
  end
19
19
 
20
20
  private
@@ -70,7 +70,7 @@ module SyncAttrWithAuth0
70
70
  return true
71
71
  end
72
72
 
73
- def create_user_in_auth0
73
+ def auth0_create
74
74
  # When creating a new user, create the user in auth0.
75
75
 
76
76
  ok_to_sync = (self.respond_to?(:sync_with_auth0_on_create) and !self.sync_with_auth0_on_create.nil? ? self.sync_with_auth0_on_create : true)
@@ -83,31 +83,13 @@ module SyncAttrWithAuth0
83
83
  end
84
84
 
85
85
  if ok_to_sync
86
- user_metadata = auth0_user_metadata
87
-
88
- password = auth0_user_password
89
- email_verified = auth0_email_verified?
90
- args = {
91
- 'email' => self.send(auth0_sync_options[:email_att]),
92
- 'password' => password,
93
- 'connection' => auth0_sync_options[:connection_name],
94
- 'email_verified' => email_verified,
95
- 'user_metadata' => user_metadata
96
- }
97
-
98
- auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client
99
-
100
- response = auth0.create_user(self.send(auth0_sync_options[:name_att]), args)
101
-
102
- # Update the record with the uid
103
- self.send("#{auth0_sync_options[:uid_att]}=", response['user_id'])
104
- self.save
86
+ create_user_in_auth0
105
87
  end
106
88
 
107
89
  true # don't abort the callback chain
108
90
  end
109
91
 
110
- def sync_attr_with_auth0
92
+ def auth0_update
111
93
  ok_to_sync = (self.respond_to?(:sync_with_auth0_on_update) and !self.sync_with_auth0_on_update.nil? ? self.sync_with_auth0_on_update : true)
112
94
 
113
95
  if ok_to_sync
@@ -115,38 +97,12 @@ module SyncAttrWithAuth0
115
97
  # Get the auth0 uid
116
98
  uid = self.send(auth0_sync_options[:uid_att])
117
99
 
118
- # Don't try to update auth0 if the user doesn't have a uid
119
- unless uid.nil?
120
- user_metadata = auth0_user_metadata
121
-
122
- auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client
123
-
124
- args = {
125
- 'app_metadata' => {
126
- 'name' => self.send(auth0_sync_options[:name_att]),
127
- 'nickname' => self.send(auth0_sync_options[:name_att]),
128
- 'given_name' => self.send(auth0_sync_options[:given_name_att]),
129
- 'family_name' => self.send(auth0_sync_options[:family_name_att])
130
- }
131
- }
132
-
133
- if (
134
- auth0_sync_options[:sync_atts].index(auth0_sync_options[:password_att]) and
135
- # Because the password being passed to auth0 probably is not a real
136
- # field (and if it is it needs to be the unencrypted value), we
137
- # can't rely on checking if the password attribute changed (chances
138
- # are, that method does not exist). So assume the password attribute
139
- # is only set if it's being changed.
140
- !self.send(auth0_sync_options[:password_att]).nil?
141
- )
142
- # The password should be sync'd and was changed
143
- args['password'] = self.send(auth0_sync_options[:password_att])
144
- args['verify_password'] = auth0_verify_password?
145
- end
146
-
147
- args['user_metadata'] = user_metadata
148
-
149
- response = auth0.patch_user(uid, args)
100
+ if uid.nil?
101
+ # Create the user in auth0
102
+ create_user_in_auth0
103
+ else
104
+ # Update the user in auth0
105
+ update_user_in_auth0
150
106
  end
151
107
 
152
108
  end
@@ -154,6 +110,65 @@ module SyncAttrWithAuth0
154
110
  true # don't abort the callback chain
155
111
  end
156
112
 
113
+ def create_user_in_auth0
114
+ user_metadata = auth0_user_metadata
115
+
116
+ password = auth0_user_password
117
+ email_verified = auth0_email_verified?
118
+ args = {
119
+ 'email' => self.send(auth0_sync_options[:email_att]),
120
+ 'password' => password,
121
+ 'connection' => auth0_sync_options[:connection_name],
122
+ 'email_verified' => email_verified,
123
+ 'user_metadata' => user_metadata
124
+ }
125
+
126
+ auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client
127
+
128
+ response = auth0.create_user(self.send(auth0_sync_options[:name_att]), args)
129
+
130
+ # Update the record with the uid
131
+ self.send("#{auth0_sync_options[:uid_att]}=", response['user_id'])
132
+ self.save
133
+ end
134
+
135
+ def update_user_in_auth0
136
+ user_metadata = auth0_user_metadata
137
+
138
+ auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client
139
+
140
+ args = {
141
+ 'app_metadata' => {
142
+ 'name' => self.send(auth0_sync_options[:name_att]),
143
+ 'nickname' => self.send(auth0_sync_options[:name_att]),
144
+ 'given_name' => self.send(auth0_sync_options[:given_name_att]),
145
+ 'family_name' => self.send(auth0_sync_options[:family_name_att])
146
+ }
147
+ }
148
+
149
+ if (
150
+ auth0_sync_options[:sync_atts].index(auth0_sync_options[:password_att]) and
151
+ # Because the password being passed to auth0 probably is not a real
152
+ # field (and if it is it needs to be the unencrypted value), we
153
+ # can't rely on checking if the password attribute changed (chances
154
+ # are, that method does not exist). So assume the password attribute
155
+ # is only set if it's being changed.
156
+ !self.send(auth0_sync_options[:password_att]).nil?
157
+ )
158
+ # The password should be sync'd and was changed
159
+ args['password'] = self.send(auth0_sync_options[:password_att])
160
+ args['verify_password'] = auth0_verify_password?
161
+ end
162
+
163
+ args['user_metadata'] = user_metadata
164
+
165
+ response = auth0.patch_user(uid, args)
166
+
167
+ if response.code == 404
168
+ create_user_in_auth0
169
+ end
170
+ end
171
+
157
172
  def auth0_user_password
158
173
  self.respond_to?(auth0_sync_options[:password_att]) ? self.send(auth0_sync_options[:password_att]) : auth0_default_password
159
174
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sync_attr_with_auth0
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick McGraw