Module: SyncAttrWithAuth0::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/sync_attr_with_auth0/model.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
Instance Method Details
#create_user_in_auth0 ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/sync_attr_with_auth0/model.rb', line 69 def create_user_in_auth0 # When creating a new user, create the user in auth0. 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) if ok_to_sync # Get an access token access_token = SyncAttrWithAuth0::Auth0.get_access_token # Look for matches between what's changing # and what needs to be transmitted to Auth0 matches = ( (self.class.sync_atts || []) & (self.changes.keys || []) ) # Figure out what needs to be sent to Auth0 changes = {} matches.each do |m| changes[m] = self.send(m) end unless changes['email'].nil? # Email is already being sent changes.delete('email') end unless changes['password'].nil? # Password is already being sent changes.delete('password') end response = SyncAttrWithAuth0::Auth0.make_request( access_token, 'post', "/api/users", { 'email' => self.send(email_att), 'password' => self.send(password_att), 'connection' => connection_name, 'email_verified' => self.send(email_verified_att) }.merge(changes)) response = JSON.parse(response) # Update the record with the uid self.send("#{uid_att}=", response['user_id']) self.save end true # don't abort the callback chain end |
#sync_attr_with_auth0 ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/sync_attr_with_auth0/model.rb', line 119 def sync_attr_with_auth0 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) if ok_to_sync # Look for matches between what's changing # and what needs to be transmitted to Auth0 matches = ( (self.class.sync_atts || []) & (self.changes.keys || []) ) # If we find matches unless matches.empty? # Get an access token access_token = SyncAttrWithAuth0::Auth0.get_access_token # Figure out what needs to be sent to Auth0 changes = {} matches.each do |m| changes[m] = self.send(m) end # If we actually have changes unless changes.empty? # Get the auth0 uid uid = self.send(uid_att) # Don't try to update auth0 if the user doesn't have a uid unless uid.nil? # Determine if the email was changed unless changes['email'].nil? email = changes.delete('email') response = SyncAttrWithAuth0::Auth0.make_request( access_token, 'put', "/api/users/#{::URI.escape(uid)}/email", { 'email' => email, 'verify' => false # If the user were to fail to verify it would create a discrepency between auth0 and the local database }) end # Determine if the password was changed unless changes['password'].nil? password = changes.delete('password') response = SyncAttrWithAuth0::Auth0.make_request( access_token, 'put', "/api/users/#{::URI.escape(uid)}/password", { 'password' => password, 'verify' => true }) end # Patch the changes response = SyncAttrWithAuth0::Auth0.make_request( access_token, 'patch', "/api/users/#{::URI.escape(uid)}/metadata", changes) end end end end true # don't abort the callback chain end |
#validate_email_with_auth0 ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sync_attr_with_auth0/model.rb', line 48 def validate_email_with_auth0 # If the email is being modified, verify the new email does not already # exist in auth0. ok_to_validate = (self.respond_to?(:validate_with_auth0) and !self.validate_with_auth0.nil? ? self.validate_with_auth0 : true) if ok_to_validate and self.email_changed? # Get an access token access_token = SyncAttrWithAuth0::Auth0.get_access_token response = SyncAttrWithAuth0::Auth0.make_request( access_token, 'get', "/api/users?search=email:#{self.email}") return JSON.parse(response).empty? end return true end |