Anonymous Users
Allow guest users to use your apps and website and promote to regular users later.

Overview

You can use the create an Anonymous User account for the guests in your apps, so they can carry out interactions just like a normal user. For example, guests can post comments and save preference in your social platform before setting email and password. The user session will persist even the app has been closed.
This improves the app experience because the user do not need to set up authenticators until further down the user journey, while still enjoying most of the app features. For app developers, the ability to create and assign Anonymous User also makes it easier to link the activities of an individual before and after sign-up.

Enable Anonymous User in your application

  1. 1.
    In the Portal , go to the Anonymous Users page
  2. 2.
    Turn on the Enable anonymous users? toggle in the page and click Save

Using the SDK

Sign up as an Anonymous User

This will create an Anonymous User for the session. Subsequent requests from the end-user in the session can be identify by the same sub
React Native
Flutter
iOS
Android
Web
1
authgear
2
.authenticateAnonymously()
3
.then(({userInfo}) => {
4
// Logged in as anonymous user successfully
5
})
6
.catch((err) => {
7
// Handle the error
8
});
Copied!
1
try {
2
final userInfo = await authgear.authenticateAnonymously();
3
// Logged in as anonymous user successfully
4
} catch (e) {
5
// Handle the error
6
}
Copied!
1
authgear.authenticateAnonymously { result in
2
switch result {
3
case let .success(authResult):
4
let userInfo = authResult.userInfo
5
// Logged in as anonymous user successfully
6
case let .failure(error):
7
// Handle the error
8
}
9
}
Copied!
1
mAuthgear.authenticateAnonymously(new OnAuthenticateAnonymouslyListener() {
2
@Override
3
public void onAuthenticated(@NonNull UserInfo userInfo) {
4
// Logged in as anonymous user successfully
5
}
6
7
@Override
8
public void onAuthenticationFailed(@NonNull Throwable throwable) {
9
// Handle the error
10
}
11
});
Copied!
1
authgear
2
.authenticateAnonymously()
3
.then(({userInfo}) => {
4
// Logged in as anonymous user successfully
5
})
6
.catch((err) => {
7
// Handle the error
8
});
Copied!

Check the UserInfo object

After "signing up" as an anonymous user, you can retrieve the "UserInfo" object and see the sub of the end-user.
UserInfo
1
{
2
"sub": "...",
3
"isVerified": false,
4
"isAnonymous": true
5
}
Copied!

Promotion of an Anonymous User

promoteAnonymousUser function can be called to promote an anonymous user to a regular user with login ID (e.g. email, phone number) and authenticators (e.g. password). The end-user will be prompted a sign up page to complete the promotion. The sub of an end-user will remain the same after promotion.
React Native
Flutter
iOS
Android
Web
1
authgear
2
.promoteAnonymousUser({
3
redirectURI: THE_REDIRECT_URI,
4
})
5
.then(({userInfo}) => {
6
// Promote anonymous user successfully
7
})
8
.catch((e) => {
9
// Handle the error
10
});
Copied!
1
try {
2
final userInfo = await authgear.promoteAnonymousUser(redirectURI: THE_REDIRECT_URI);
3
// Promote anonymous user successfully
4
} catch (e) {
5
// Handle the error
6
}
Copied!
1
authgear.promoteAnonymousUser(
2
redirectURI: THE_REDIRECT_URI
3
) { result in
4
switch result {
5
case let .success(authResult):
6
let userInfo = authResult.userInfo
7
// Promote anonymous user successfully
8
case let .failure(error):
9
// Handle the error
10
}
11
}
Copied!
1
PromoteOptions options = new PromoteOptions(THE_REDIRECT_URI);
2
authgear.promoteAnonymousUser(options, new OnPromoteAnonymousUserListener() {
3
@Override
4
public void onPromoted(@NonNull AuthorizeResult result) {
5
// Promote anonymous user successfully
6
}
7
@Override
8
public void onPromotionFailed(@NonNull Throwable throwable) {
9
// Handle the error
10
}
11
});
Copied!
Step 1: Start the promotion flow
When the user clicks promote on your website, make a start promotion call to redirect them to the promotion page.
1
authgear
2
.startPromoteAnonymousUser({
3
// Configure redirectURI which users will be redirected to
4
// after they have promoted with Authgear.
5
// You can use any path in your website.
6
// Make sure it is in the "Redirect URIs" list of the Application.
7
// The redirect uri for anonymous user promotion should be
8
// different from the one for normal user authentication.
9
// e.g. "https://yourdomain.com/promote-redirect"
10
redirectURI: THE_REDIRECT_URI,
11
})
12
.then(({userInfo}) => {
13
// Started the promotion flow
14
})
15
.catch((err) => {
16
// Failed to start the promotion flow
17
});
Copied!
Step 2: Handle the promotion result
After the user promotes on the promotion page, the user will be redirected to the redirectURL with a code parameter in the URL query. In the redirectURI of your application, make a finish promotion call to handle the promotion result.
1
authgear
2
.finishPromoteAnonymousUser()
3
.then(({userInfo}) => {
4
// Promoted successfully
5
// You should redirect the user to another path
6
})
7
.catch((err) => {
8
// Failed to finish promotion
9
});
10
);
Copied!

User Lifetime

Mobile apps

On Mobile SDKs, creating an anonymous user will create a key-pair. The key-pair is stored in the native encrypted store on the mobile device. The end-user can always re-login to the same anonymous user with the key-pair. Such anonymous user will become inaccessible when the encrypted store is removed.

Web apps and websites

On the Web SDK, there will be no key-pair created. Therefore the end-user will not be able to login to the same Anonymous User after the their session become invalid. For cookie-based authentication, it is controlled by the "idle timeout" and "session lifetime" of the Cookie. For token-based authentication, it is controlled by the "idle timeout" and "token lifetime" of the Refresh Token.
In other words, The anonymous user account lifetime is the same as the logged in session lifetime.
To adjust the lifetime settings, change the timeouts and lifetimes in Portal > Applications accordingly.

Caution for high traffic websites

You should create anonymous users only when necessary in the user journey to prevent creating excessive orphan accounts in your tenant.