User Management
Magnet Max offers a user management system that is easy to use. The MMUser
class allows for user registration, login, logout, and search. An MMUser has properties that describe a user (e.g., userID, userName, firstName, lastName, email, etc.) and it also provides an extras (String, String) dictionary that allows you to associate extra data to a user.
User Registration
The following shows the basic way to register a user. For advanced options, see Advanced.
UserRegistrationInfo.Builder builder = new UserRegistrationInfo.Builder();
builder.firstName("John")
.lastName("Doe")
.userName("
[email protected]")
.password("magnet");
User.register(builder.build(), new ApiCallback<User>() {
@Override
public void success(User user) {
// register user successfully
}
@Override
public void failure(ApiError error) {
// fail to register user, show error
}
});
User Login
The following shows the basic way to log in a user. For advanced options, see Advanced.
String userName = "
[email protected]";
String password = "magnet";
boolean isShouldRemember = true;
User.login(userName, password, isShouldRemember, new ApiCallback<Boolean>() {
@Override
public void success(Boolean aBoolean) {
// user login successfully
}
@Override
public void failure(ApiError error) {
// fail to login user, show error
}
});
User Logout
The following shows how to log out a user.
User.logout(new ApiCallback<Boolean>() {
@Override
public void success(Boolean aBoolean) {
// user logout successfully
}
@Override
public void failure(ApiError error) {
// fail to user logout, show error
}
});
User Search
User search uses elastic search.
It can perform queries to find a list of users:
int pageSize = 50;
int offset = 0;
String DEFAULT_USER_ORDER = "firstName:asc";
User.search(String.format("firstName:%s* OR lastName:%s*", "John", "John"), pageSize, offset, DEFAULT_USER_ORDER, new ApiCallback<List<User>>() {
@Override
public void success(List<User> users) {
// search user successfully, show result
}
@Override
public void failure(ApiError apiError) {
// fail to search user, show error
}
});
Find users by username:
List<String> userNames = Arrays.asList("name1", "name2");
User.getUsersByUserNames(userNames, new ApiCallback<List<User>>() {
@Override
public void success(List<User> users) {
// search user successfully, show result
}
@Override
public void failure(ApiError error) {
// fail to search user, show error
}
});
And find users by userId:
List<String> userIds= Arrays.asList("userdId1", "userdId2");
User.getUsersByUserIds(userIds, new ApiCallback<List<User>>() {
@Override
public void success(List<User> users) {
// search user successfully, show result
}
@Override
public void failure(ApiError error) {
// fail to search user, show error
}
});
User Avatar
To set an avatar for a user, refer to the code below.
Bitmap imageData = ... //set bitmap here
String mimeType = ... //get mime type here
User user = User.getCurrentUser();
user.updateAvatar(imageData, mimeType, callback, MaxCoreUserHelper.UpdateAvatarMethod.ALL);
MaxCoreActionCallback<User> callback = new MaxCoreActionCallback<User>() {
@Override
public void onResult(boolean isSuccess, @Nullable Throwable error, @Nullable User result) {
// callback after API call, isSuccess is true for successfully, otherwise, it's failure
}
};
User Passwords
User passwords can be changed in two ways: have the server generate and email a One-time password (OTP) or use an old password to authorize a change.
To have the server generate and email a user an OTP code:
User.requestOtpCode("USERNAME", new MaxCoreActionCallback<Boolean>() {
@Override
public void onResult(boolean isSuccess, Throwable error, Boolean result) {
// callback after API call, isSuccess is true for successfully, otherwise, it's failure
}
});
String otpCode = ... ;
User.resetPasswordByOtp("USERNAME", otpCode , "NEW_PASSWORD", new MaxCoreActionCallback<Boolean>() {
@Override
public void onResult(boolean isSuccess, @Nullable Throwable error, @Nullable Boolean result) {
// callback after API call, isSuccess is true for successfully, otherwise, it's failure
}
});
To use a user’s old password to authorize a password change:
User.updateProfile(
new UpdateProfileRequest.Builder().password("NEW_PASSWORD").build(), new ApiCallback<User>() {
@Override
public void success(User user) {
// change password successfully, show result
}
@Override
public void failure(ApiError error) {
// fail to change password
}
});
Advanced
SAML Authentication
MMUser provides more than one way to authenticate users. If your server implements SAML, you can log in with MMUser using SAML instead of the basic login.
To log in with SAML:
boolean rememberMe = false;
User.samlLogin("REQUEST_ID", rememberMe, new ApiCallback<Boolean>() {
@Override
public void success(Boolean aBoolean) {
// samlLogin successfully
}
@Override
public void failure(ApiError error) {
// fail to samlLogin
}
});
Register Pending Users
MMUser provides a way to register a user with a pending state. The user must be approved on the Magnet Max console before they can successfully log in.
UserRegistrationInfo.Builder builder = new UserRegistrationInfo.Builder();
builder.firstName("John")
.lastName("Doe")
.userName("
[email protected]")
.password("magnet");
User.registerPending(builder.build() , new MaxCoreActionCallback<User>() {
@Override
public void onResult(boolean isSuccess, @Nullable Throwable error, @Nullable User result) {
// callback after API call, isSuccess is true for successfully, otherwise, it's failure
}
});