Source: site.view [edit]
Function name: siteRegister
Arguments: site,fullname,phone,email,username,pw,pw2,errCmd,succCmd
Description: Registers a new user for a site. Returns an error code if there is one. If successful, automatically logs a person in.
Page type: webl
Render function:  
Module: siteutil

Page source:

var err = nil;
var data = "NO DATA";


var findUniqueId = fun(db, coll, prefix)
   var found = false;
   var i = coll.count();
   while !found do
      var id = prefix + ToString(i);
      var dbobj = Wub_NewDBObject([. "DT_RowId" = id .]); 
      var res = Wub_QueryDB(coll, dbobj, 20);
      found = (Size(res) == 0);
      if found then
         return id
      else
         i = i + 1
      end
   end;
end;

// password checks
if (pw == nil or Str_Trim(pw) == "") then
   err = "ERR_NO_PASSWORD"
end;
  
if (err == nil and Size(pw) < 6) then
   err = "ERR_INVALID_PASSWORD"
end;
  
if (err == nil and pw != pw2) then
   err = "ERR_PASSWORD_MISMATCH"
end;
  
if (err == nil) then
   site = Str_Trim(site);
   fullname = Str_Trim(fullname);
   phone = Str_Trim(phone);
   email = Str_Trim(email);
   username = Str_Trim(username);

   if (Str_IndexOf("@", email) < 0) then
      err = "ERR_INVALID_EMAIL_FORMAT"
   end;
  
   if (site == "" or fullname == "" or phone == "" or email == "" or username == "") then
      err = "ERR_MISSING_INFO"
   end;

   if (err == nil) then
      var db = Wub_GetDB(site);
      var coll = Wub_GetCollection(db, "login");

      var dbobj = Wub_NewDBObject([. username=username .]); 
      var res = Wub_QueryDB(coll, dbobj, 20);
      if (Size(res) > 0) then
         err = "ERR_USERNAME_ALREADY_REGISTERED"
      end;

      if (err == nil) then
         dbobj = Wub_NewDBObject([. email=email .]); 
         res = Wub_QueryDB(coll, dbobj, 20);
         if (Size(res) > 0) then
            err = "ERR_EMAIL_ALREADY_REGISTERED"
         end;
      end;
      if (err == nil) then
         // Encrypt pw
         var pw2 = Base64_Encode(username + ":" + pw);    

		 data = [. name=fullname, phone=phone, email=email, username=username, pw=pw2 .];

         var count = 0;
         while (count < Size(OPTIONALARGS)) do
            if Str_StartsWith(OPTIONALARGS[count], "DT_RowId") then
			   var getRowId = findUniqueId(db, coll, Select(OPTIONALARGS[count], 8, Size(OPTIONALARGS[count]))); 
			   data["DT_RowId"] := getRowId
            else 
               data["arg" + ToString(count)] := OPTIONALARGS[count];
            end;
            count = count + 1               
         end;
  
        // add user's information  
        dbobj = Wub_NewDBObject(data); 
        Wub_InsertDB(coll, dbobj);
      end
   end
end;

if (err == nil) then
   // WubCall("siteutil.siteLogin", [site, username, pw, errCmd, succCmd]);  
   ""
else 
  WubCall(errCmd, [err]);
  err;
end;