All logic for CloudBase anonymous login can be actively executed by client code without requiring manual user operations. In an anonymous login state, CloudBase resources can be normally called. Developers can also configure corresponding access restrictions for anonymous users in conjunction with security rules.
Quantity Limit
The number of anonymous users in each CloudBase environment is no more than 10 million.
Activation Process
Enabling Anonymous Login
2. Go to the Identity Verification > Login Methods page.
3. In the login methods list, select Anonymous Login and click Enable.
Adding Secure Domain Name (Optional)
The domain name is to be added to the secure origin list of the CloudBase Platform for web applications; otherwise, it will be identified as an illegal origin:
Login Process
Execute the following code in a cloud function:
import cloudbase from '@cloudbase/js-sdk';
const app = cloudbase.init({
env: 'xxxx-yyy';
});
const auth = app.auth();
async function login(){
await auth.signInAnonymously();
const loginScope = await auth.loginScope();
console.log(loginScope === 'anonymous');
}
await login();
Security Rules
The auth.loginType value for anonymous users in security rules is ANONYMOUS. Combined with security rules, you can restrict anonymous users' access permissions for cloud database and cloud storage. For example, the security rules are shown in the following code: Anonymous users cannot access cloud databases in read-write mode.
{
"read": "auth.loginType != 'ANONYMOUS'",
"write": "auth.loginType != 'ANONYMOUS'"
}
Cloud storage is readable to all users but not writable to anonymous users.
{
"read": "auth != null",
"write": "auth.loginType != 'ANONYMOUS'"
}
Converting to a Formal User
If users generate some private data in an anonymous state (such as personal achievements and equipment obtained in games), and want to convert this anonymous account into a formal account for long-term retention.
For this requirement, you can perform anonymous user conversion registration to transfer the private data of this anonymous account to a formal account.
Example of converting to a formal user:
1. Anonymous login.
const auth = app.auth();
await auth.signInAnonymously();
2. Log in using a specific method.
3. Obtain the access_token and complete the conversion.
const { accessToken } = await auth.getAccessToken();
await auth.signUp({
anonymous_token: access_token
});