Search code examples
iosswiftbackendless

Prevent users from registering with the same username in Backendless


I'm creating a Swift app using Backendless. The app automatically creates the user's username attribute, though this may be changed in the future. Either way, how can I get Backendless to prevent a user from registering if they have the same username as another?

I have added an event handler in Backendless (Business Logic section) to run before allowing a user to register:

/* global Backendless */

/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains an information about application, current user and event
* @param {Object} req.user 
*/
Backendless.ServerCode.User.beforeRegister(function(req) {
  //add your code here
}, true);

How can I set it up to check if a username value is the same as an existing one and, if so, throw an error?


Solution

  • Here is the query to know whether user exists or not

    let query = BackendlessDataQuery()
    let userEmail = "foo@foo.com"
    query.whereClause = "email = '\(userEmail)'"
    let users = backendless.data.of(BackendlessUser.ofClass()).find(query)
    if( users.data.count > 0 )
    {
    // user exists
    }
    

    referenced from Here