|
| 1 | +/*function create(user, callback) { |
| 2 | + // This script should create a user entry in your existing database. It will |
| 3 | + // be executed when a user attempts to sign up, or when a user is created |
| 4 | + // through the Auth0 dashboard or API. |
| 5 | + // When this script has finished executing, the Login script will be |
| 6 | + // executed immediately afterwards, to verify that the user was created |
| 7 | + // successfully. |
| 8 | + // |
| 9 | + // The user object will always contain the following properties: |
| 10 | + // * email: the user's email |
| 11 | + // * password: the password entered by the user, in plain text |
| 12 | + // * tenant: the name of this Auth0 account |
| 13 | + // * client_id: the client ID of the application where the user signed up, or |
| 14 | + // API key if created through the API or Auth0 dashboard |
| 15 | + // * connection: the name of this database connection |
| 16 | + // |
| 17 | + // There are three ways this script can finish: |
| 18 | + // 1. A user was successfully created |
| 19 | + // callback(null); |
| 20 | + // 2. This user already exists in your database |
| 21 | + // callback(new ValidationError("user_exists", "my error message")); |
| 22 | + // 3. Something went wrong while trying to reach your database |
| 23 | + // callback(new Error("my error message")); |
| 24 | +
|
| 25 | + const msg = 'Please implement the Create script for this database connection ' + |
| 26 | + 'at https://manage.auth0.com/#/connections/database'; |
| 27 | + return callback(new Error(msg)); */ |
| 28 | +function create(user, callback) { |
| 29 | + //console.log("landed here..................................."); |
| 30 | + var countryObj = JSON.parse(user.user_metadata.country); |
| 31 | + var regSource = user.user_metadata.regSource; |
| 32 | + var utmSource = user.user_metadata.utmSource; |
| 33 | + var utmMedium = user.user_metadata.utmMedium; |
| 34 | + var utmCampaign = user.user_metadata.utmCampaign; |
| 35 | + var retUrl = user.user_metadata.returnUrl; |
| 36 | + var afterActivationURL = retUrl != null ? retUrl : "https://" + configuration.DOMAIN + "/home"; |
| 37 | + if (regSource === configuration.REG_BUSINESS) { |
| 38 | + afterActivationURL = "https://connect." + configuration.DOMAIN; |
| 39 | + } |
| 40 | + var data = { |
| 41 | + "param": { |
| 42 | + "handle": user.username, |
| 43 | + "email": user.email, |
| 44 | + "credential": { |
| 45 | + "password": user.password |
| 46 | + }, |
| 47 | + "firstName": user.user_metadata.firstName, |
| 48 | + "lastName": user.user_metadata.lastName, |
| 49 | + "country": { |
| 50 | + "code": countryObj.code, |
| 51 | + "isoAlpha3Code": countryObj.alpha3, |
| 52 | + "isoAlpha2Code": countryObj.alpha2 |
| 53 | + }, |
| 54 | + "regSource": regSource, |
| 55 | + "utmSource": utmSource, |
| 56 | + "utmMedium": utmMedium, |
| 57 | + "utmCampaign": utmCampaign, |
| 58 | + }, |
| 59 | + "options": { |
| 60 | + "afterActivationURL": encodeURIComponent(afterActivationURL) |
| 61 | + } |
| 62 | + }; |
| 63 | + //console.log("SignUp....", user, data); |
| 64 | + request.post({ |
| 65 | + url: "https://api." + configuration.DOMAIN + "/v3/users", |
| 66 | + json: data |
| 67 | + //for more options check: |
| 68 | + //https://github.com/mikeal/request#requestoptions-callback |
| 69 | + }, function (err, response, body) { |
| 70 | + |
| 71 | + // console.log(err); |
| 72 | + // console.log(response.statusCode); |
| 73 | + // console.log(body.result.content); |
| 74 | + |
| 75 | + if (err) return callback(err); |
| 76 | + console.log(body.result.content); |
| 77 | + if (response.statusCode !== 200) { |
| 78 | + //return callback(new ValidationError("lock.fallback",body.result.content)); |
| 79 | + const error_message = body.result.content; |
| 80 | + let code = "lock.fallback"; |
| 81 | + |
| 82 | + if (error_message.search("Handle may not contain a space") !== -1) { |
| 83 | + code = "handle_invalid_space"; |
| 84 | + } else if (error_message.search("Length of Handle in character should be between 2 and 15") !== -1) { |
| 85 | + code = "handle_invalid_length"; |
| 86 | + } else if (error_message.search("Please choose another handle, not starting with admin") !== -1) { |
| 87 | + code = "handle_invalid_startwith_admin"; |
| 88 | + } else if (error_message.search('Handle may contain only letters, numbers and') !== -1) { |
| 89 | + code = "handle_invalid_constains_forbidden_char"; |
| 90 | + } else if (error_message.search("Handle may not contain only punctuation") !== -1) { |
| 91 | + code = "handle_invalid_conatins_only_punctuation"; |
| 92 | + } else if (error_message.search("The user already exists") !== -1) { |
| 93 | + code = "user_exists"; |
| 94 | + } else if (error_message.search("has already been taken") !== -1) { |
| 95 | + code = "user_exists"; |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + return callback(new ValidationError(code, error_message)); |
| 101 | + |
| 102 | + //return callback(new Error(body.result.content)); |
| 103 | + } |
| 104 | + //if (response.statusCode === 401) return callback(); |
| 105 | + /* const Analytics = require('analytics-node'); |
| 106 | + const _ = require('lodash'); |
| 107 | + var analytics = new Analytics('bkPtWMUMTYDhww2zsJluzxtdhtmSsyd9'); |
| 108 | + analytics.identify({ |
| 109 | + anonymousId: 'signup', |
| 110 | + traits: { |
| 111 | + user: _.omit(user, ['credential', 'password']) |
| 112 | + } |
| 113 | + }); |
| 114 | + analytics.track({ |
| 115 | + anonymousId: 'BXWXUWnilVUPdN01t2Se29Tw2ZYNGZvH', |
| 116 | + event: 'signUp', |
| 117 | + properties: _.omit(user, ['credential', 'password']) |
| 118 | + });*/ |
| 119 | + callback(null); |
| 120 | + }); //end post request |
| 121 | + //callback(null); |
| 122 | +} |
| 123 | + |
| 124 | +//} |
0 commit comments