Signing up

The most basic method to create a user in Kii Cloud is specifying an identifier such as a username and a password. This topic explains how to create a user with a username, an email address, or a phone number.

Identifying a user

When a user is created, it can be identified with any of a username, email address, phone number, and a combination of them.

For example, a user becomes available by taking the following steps.

  1. Create and register a user by specifying the username "id123456" and password "123ABC".
  2. Authenticate a user by specifying the username "id123456" and password "123ABC".

Once the user registration or user authentication is done, this user is treated as logged in (the user will be automatically logged in after the registration).

You can also identify users with their email addresses and phone numbers (check the table below).

Information to be provided on user registration Information to be provided on user authentication
Username Email address Phone number Password Identifier accepted Password
Yes - - Required Username Required
Yes - Yes Required Username or phone number Required
Yes Yes - Required Username or email address Required
Yes Yes Yes Required Username or email address or phone number Required
- - Yes *1 Required Phone number Required
- Yes *2 - Required Email address Required
- Yes *3 Yes *3 Required Email address or phone number Required

*1 You need to disable the phone verification; you will get an error otherwise.
*2 You need to disable the email verification; you will get an error otherwise.
*3 You need to disable either the phone verification or the email verification; you will get an error otherwise.

For example, suppose that a user registers with the username "id123456", the email address "user@mydomain.com" and password "123ABC". In this case, the user can login with one of the two ways:

  • Login with the username "id123456" and password "123ABC".
  • Login with the username "user@mydomain.com" and password "123ABC".

Please note that a user can set their email address or phone number instead of their username upon the registration. If you do not want to let them use these values for the authentication purpose (but still want to save them), consider storing the values in custom fields (See User Attributes) or storing them in a dedicated user scope bucket.

When logging in, you can specify a username, or email address or (international) phone number instead of a username in the same API. The SDK will interpret the identifier starting with "@" as an email address, the one starting with "+" as a phone number and all others as a username.

A user can change his email address and phone number after the registration, but he cannot change the username.

When using an email address and phone number for authentication, you can also verify them (See Verifying the User's Email Address and Phone Number). By enabling this feature, you can let users log in only after these identifiers are verified.

Signing up a user

The following sample code shows how to sign up a user with the username user_123456, the e-mail address user_123456@example.com, a phone number +819012345678 and a password 123ABC.

// Create a KiiUser builder.
KiiUser.Builder builder;
builder = KiiUser.BuilderWithName("user_123456");
builder.SetEmail("user_123456@example.com");
builder.SetGlobalPhone("+819012345678");

// Create a user.
KiiUser user = builder.Build();

// Register the user.
user.Register("123ABC", (KiiUser registeredUser, Exception e) => {
  if (e != null)
  {
    // Handle the error.
    return;
  }
});

The basic steps are as follows:

  1. Create a KiiUser instance by calling the BuilderWithName method with a username, optionally setting an email address and phone number with the SetEmail and SetGlobalPhone methods. Then call the Build method.
  2. Register this KiiUser instance with the Register method passing the password as the parameter. At this point, a new account will be created and the user will be signed-in.

If the user with the same name already exists, you will get the ConflictException. See Types of errors for the details on how you can handle the exception in your application.

Signing up without a username

You can also create a new account without a username. To create an account with an e-mail address or a phone number, use the BuilderWithEmail and BuilderWithPhone methods respectively. Note that in this case, the email and phone number (SMS) verification must be disabled (See the next section for more details on the verification mechanism).

Signing up with a local phone number

When you want to specify a local phone number, also set a valid 2-letter country code using the Country property like in the following example:

string phoneNumber = "09051903944";
string country = "JP";
string password = "123456";

// Create a user.
KiiUser user = KiiUser.BuilderWithPhone(phoneNumber).Build();

// Set the two-letter country code.
user.Country = country;

// Register the user.
user.Register(password, (KiiUser registeredUser, Exception e) => {
  if (e != null)
  {
    // Handle the error.
    return;
  }
});

Detecting the user identifier automatically

If your application provides a user registration UI with an input field that accepts multiple identifier types, you can use the KiiUser.BuilderWithIdentifier(string). This method accepts one of email address, phone number or username, determines which identifier type is specified, and create a new user.

// Create a KiiUser builder.
KiiUser.Builder builder = null;

try {
  string identifier = "09012345678";

  // Set the indentifier to the KiiUser builder.
  builder = KiiUser.BuilderWithIdentifier(identifier);
} catch (ArgumentException e) {
  // Handle the error.
  return;
}

try {
  // You can set a username, email address, global phone number, and
  // local phone number to a builder with a corresponding setter.
  string username = "user_123456";
  builder.SetName(username);
} catch (ArgumentException e) {
  // Handle the error.
  return;
}

// Create a user.
KiiUser user = builder.Build();

// If you set a local phone number as an identifier,
// set a country code to the KiiUser instance.
try {
  string country = "JP";
  user.Country = country;
} catch (ArgumentException e) {
  // Handle the error.
  return;
}

string password = "123ABC";

// Register the user.
user.Register(password, (KiiUser registeredUser, Exception e) => {
  if (e != null)
  {
    // Handle the error.
    return;
  }
});

The KiiUser.BuilderWithIdentifier(string) accepts a username, email address, or phone number. The method identifies which identifier type is specified and creates a KiiUser instance accordingly with the KiiUser.Builder.Build() method. If you pass a local phone number, set a 2-letter country code in the KiiUser instance.

Each identifier can be set with the setter methods of the KiiUser.Builder instance. The set method validates the input. If the input is invalid, the setter method throws an exception. Please read the Limitations for more details.

Limitations

Some limitations (i.e., number and type of characters) apply to the user identifiers.

Some of the limits applied to identifiers are slightly different among SDKs. In this section, we will explain the strictest one in among the SDKs.

Username

A username must be 3 to 64 characters. The accepted characters are alphanumeric, "_" (underscore), "-" (hyphen) and "." (period).

It must be unique; a user will get an error when he tries to register the username that is already registered in the application.

A username is also used when a user queries other users (e.g. executing the findUserByUserName in the Kii Cloud SDK for Android).

Upper case alphabets in a username are converted to lower case when the username is registered.

Email address

An email address must be in a form of "username@domain".

  • The username part is composed of alphanumeric, ".", "_", "%", "+" and "-".
  • The domain part is composed of alphanumeric and ".".

Please refer to References for more information.

It must be unique; a user will get an error when he tries to register or change his email address to the email address that is already registered in the application.

Phone number

You can specify a phone number in two ways (note that some SDK only accept the international phone number format):

  • International phone number format: This format will have a country code and local phone number after a "+" (plus) sign. The phone number is composed of 10 to 15 numbers (no hyphen or period). For example, the phone number "123-456-7890" in the United States is represented as "+11234567890".

  • Local phone number format: This format will use the domestic phone number. The country is expressed in two capital letters defined in libphonenumber. For example, the Android SDK asks you to specify the domestic phone number "1234567890" and the 2-letter country code "US". The REST API asks you to use the format "US-1234567890".

The international and local phone number formats are interoperable. The phone number "+11234567890" and "US-1234567890" identify the same user.

The phone number must be unique; a user will get an error when he tries to register or change his phone number to the number that is already registered in the application.

Only a mobile phone number is accepted. If a landline phone number is specified, an error will be returned. A library in the server determines the specified number is for a mobile phone or a landline.

Password

A password must be 4 to 50 characters. The accepted characters are alphanumeric and symbol (\u0020 to \u007e in Unicode).

Hint for creating a user list

Kii Cloud currently does not provide an API for getting a list of users; you need to implement the feature in your application if you want to get the list (Note: if you just need to get a single user, you might be able to implement it with Retrieving Other User's Data feature).

You can, for example, prepare an application scope bucket. When a new user is created, your application can register an object with the user ID (or URI) of this user in the application scope bucket. Later, your application can get a list of users by getting the KiiObjects in the bucket. By registering user attributes such as username, you can also query for users with these attributes.

Please note that the overall performance of this method will degrade when the number of users increases dramatically. You will need to implement the feature so as to avoid querying the massive amount of users. See Performance for the related discussion.

Also, note that any users can access the application scope bucket. Registering user's private information such as email addresses in the bucket make them vulnerable (See Security for the related discussion). Consider setting some access controls and accessing the bucket only via server code with the app administrator privilege to protect the data.