Sometimes you do not want to force your application users to sign up with their usernames and passwords. Kii Cloud provides a "pseudo user" feature to accommodate such a need.
Using the pseudo user feature
A pseudo user is a feature to use a Kii Cloud user without a username and password. When a pseudo user is created, an access token is generated. A pseudo user is identified with an access token that is retained on the mobile app, instead of a pair of usernamne and password.
This feature will request a user creation without a username and password to Kii Cloud. When a pseudo user is created, Kii Cloud returns the access token for this user.
When a normal user logs in with its username and password, Kii Cloud issues an access token that indicates the user is logged in. The same effect is achieved when a pseudo user is created.
For a pseudo user, it is essential to keep the access token in the device's local storage until the user no longer needs it. When a user wants to login again, you can do so with the access token by following the steps described in Logging in and Using an Access Token. Please note that if the access token is lost, the user will no longer be able to login as the same user.
A pseudo user can leverage almost all Kii Cloud features like normal users; the only feature not achievable with a pseudo user is getting a new access token. This is because a pseudo user has no associated username and password and thus he cannot login with them so as to get his access token.
A pseudo user can become a normal user by registering his identifier (i.e. username, email address and/or phone number) and password. To allow a pseudo user to access from another device, for example, you might want to first ask the user to become a normal user and then ask him to login as a normal user from another device.
Creating a pseudo user
Here is an example of creating a pseudo user. In this example, the access token issued for the created pseudo user is saved in the device.
Here is what is happening in the sample code:
Defines a user attribute (optional).
Executes the registerAsPseudoUserWithUserFields:block method to create and register a pseudo user.
Gets the access token of the pseudo user from the KiiUser.accessToken property. Then saves this token in the device.
When a pseudo user is created, the user will be automatically logged in as the pseudo user; the user can use Kii Cloud just like a normal user.
Please note that a pseudo user will lose a mean to login if their access token is lost (e.g. by the application uninstallation). If you can identify the pseudo user, for example by their display name, you can delete this user on the User Console.
Logging in as a pseudo user
Since a pseudo user has no username or password, you need to log in with the access token, like the following example:
Changing to a normal user
A pseudo user can be changed to a normal user by setting a username and a password.
Here is what is happening in the sample code:
Checks the [KiiUser currentUser].isPseudoUser property to see if the current user is a pseudo user.
Creates a KiiIdentityDataBuilder instance.
Sets a username, email address and phone number in the KiiIdentityDataBuilder instance.
Executes the build method to create a KiiIdentityData instance.
Executes the putIdentityData:userFields:password:block method with the KiiIdentityData and password.
With the above steps, a pseudo user becomes a normal user. The user will be able to authenticate and get the access token with the username and password from that point forward.
Using Kii Cloud without explicit login
Besides using the pseudo user feature, you can use the following methods to use Kii Cloud without explicit login. Kii recommends using the pseudo user feature because these methods can make the implementation complex or limited.
Creating a user in the mobile app
Your application can automatically create arbitrary username and password, register them to Kii Cloud on behalf of the user and use them every time the user tries to use your mobile app. For example, your mobile app can create a random username and password in background, use them to proceed with the user sign up & sign in, and then store them in the device storage (e.g., SharedPreference on Android and NSUserDefaults on iOS). The user will be able to automatically log in with these username and password until the mobile app is uninstalled or the stored data is explicitly erased.
In this method, your mobile app must assign a unique username to each user.
Once the username and password are erased from the device storage, the user will no longer be able to sign in. Using an immutable value like a device ID for the username and/or password, however, is strongly discouraged so as to prevent the possible privacy infringement.
To provide a support for device migration or back up, simply implement the export and import feature of the username and password stored in the device storage.
Kii Cloud does support an anonymous user (i.e., a user without any sign in), but the features allowed to the anonymous user are very limited. Most features, like accessing a user-scope data, require the user to sign in.