The Database Hub | Back4App
The Database Hub | Back4App
How it works
  • Log In
  • Sign Up

  1. Back4App
  2. Complete User Registration
Public

38
61
38
  • DatabaseDatabase
  • GraphQL API PlaygroundAPI Playground
  • Get Started GuideGet Started
  • IssuesIssues
  • ContributorsContributors
  • GraphQL API Playground
    API Playground
    Get Started Guide
    Get Started
    Issues
    Issues
    Contributors
    Contributors

  1. Complete User Registration

This is a User Registration template that is customizable. You can set which properties you need and specify validations for those properties such as length, regex and if a property is or is not mandatory.

  • App Templates

Get StartedFetch Data
ClassClassesDescription
ClassGender
The Gender class contains information about the gender of the user so the user can pick the one which identifies to.
ClassMandatoryFields
The MandatoryFields class holds the values of Classes/Properties pairs that will be validated using Cloud Code to ensure only the correct data will be stored.
ClassUser
The class User will a detailed view of each user stored in the application. It contains all the user's information.
NotesNotes

Customizable User Registration Template

Sometimes you need to collect some information about the users of your Application, which can very depending on the application itself.

While some Apps only need a username and password, other Apps might need the Geo Location of the user, phone number, gender, among others. And to ensure a sanitized database, it would be convenient to have those properties validated, ensuring a Date string is in the right format, a Zip code matches a certain format for a Country, an email is actually an email and a URL contains a valid URL format.

To make this task easier, we at Back4app created this template that you can use for easy "turn on/turn off" of properties and support multiple validations.

Cloud Code

This template relies on Cloud Code, which is the serverless functions in Back4app coded using NodeJS. We will use basically the same code, a function called validateData which will receive the Class name and the request for validation:

sync function validateData(className, request){
  // Retrieve the parameters for the Class from the MandatoryFields class so we can validate each one
  const query = new Parse.Query("MandatoryFields");
  query.equalTo("class", className);

  let results = await query.find();
  if (results){

    // Loop through the results array
    for (let i = 0; i < results.length; i ++){
      // pick each field and its properties
      let field = results[i].get("field")
      let isMandatory = results[i].get("isMandatory")
      let minLength = results[i].get("minLength")
      let maxLength = results[i].get("maxLength")
      let regexValidation = results[i].get("regexValidation")

      // If you need to debug values, uncomment the line below
      //console.log("PROPERTY: " + field + " ISMANDATORY: " + isMandatory + " MINLENGTH: " + minLength + " MAXLENGTH: " + maxLength + " REGEX: " + regexValidation)

      // if a field is mandatory but was not passed in the request
      let value = request.object.get(field)
      if (value){
        if (isMandatory && ! value) {
          throw "The field " + field  + " is mandatory.";
        }

        // if a field does not meet the required length
        if (value.length < minLength || value.length > maxLength) {
          throw "The field " + field  + " must have a length between " + minLength + " and " + maxLength + ".";
        }

        // if a field does not match the Regex validation
        if (regexValidation && regexValidation.length > 0){
          let regex = new RegExp(regexValidation);
          if (! regex.test(value)) {
            throw "The field " + field  + " is did not match the Regex validation.";
          }
        }
      }      
    }
  } 
}

That function will then be called on two BeforeSave triggers for the classes delivered with the class:

For the User class:

Parse.Cloud.beforeSave("User", async (request) => {
  await validateData("User", request) 
});

For the Gender class:

Parse.Cloud.beforeSave("Gender", async (request) => {
  await validateData("Gender", request)
});

If you need assistance with Cloud Code, please check our Docs and the Parse Guide for reference.

What will this App validate?

This App is composed of three classes:

User The User class is the regular User class from Parse, tuned to contain these custom properties along with the regular ones:

  • name (String): the first name of the User
  • lastName (String): the last name of the User
  • phoneNumber (String): the telephone number of the User
  • location (GeoPoint): the geographic location of the User
  • picture (File): the picture of the User
  • birthDate (Date): the birth date of the User
  • age (Number): the age of the user
  • country (String): the Country in which the User lives in
  • city (String): the City in which the User lives in
  • district (String): the District in which the User lives in
  • address (String): the Address in which the User lives in
  • zipCode (String): the Zip Code in which the User lives in as a formatted string
  • website (String): the website URL for the User as a formatted string
  • gender (Pointer to the Gender class): the Gender of the User

Gender The Gender class contains information about the gender of the user so the user can pick the one which identifies to. It contains the following properties:

  • code (String): a one character code for the Gender
  • gender (String): a string describing the full Gender name

MandatoryFields The MandatoryFields class holds the values of Classes/Properties pairs that will be validated using Cloud Code to ensure only the correct data will be stored. It contains the following properties:

  • field (String): the Property name inside a Class that you want to validate
  • class (String): the Class name that you want to validate
  • isMandatory (Boolean): if True, a request must contain the property to be valid. If False, requests without the property will be considered valid as well
  • minLength (Number): the mininum length for a String property to be considered valid
  • maxLength (Number): the maximum length for a String property to be considered valid
  • regexValidation (String): a scaped regular expression that will be run against a String property. If the regex returns true, the property will be considered valid. If false, it will be considered invalid.
  • comments (String): a place for you to comment out details about the validation, specially about the Regex so other users can understand what you did