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
This is the basic way to register a user. For advanced options, see Advanced.
MMUser *user = [[MMUser alloc] init];
user.firstName = @"Jane";
user.lastName = @"Doe";
user.email = @"
[email protected]";
user.userName = @"jane.doe";//user.email;
user.password = @"password";
[user register:^(MMUser *user) {
NSLog(@"user = %@", user);
} failure:^(NSError *error) {
NSLog(@"error = %@", error.localizedDescription);
}];
User Login
The following shows the basic way to log in a user. For advanced options, see Advanced.
NSString *username = @"jane.doe";
NSString *password = @"password";
NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
[MMUser login:credential success:^{
NSLog(@"currentUser = %@", [MMUser currentUser]);
} failure:^(NSError *error) {
NSLog(@"Login error = %@", error.localizedDescription);
}];
User Logout
The following shows how to log out a user.
[MMUser logout:^{
NSLog(@"logged out");
} failure: ^(NSError *error) {
NSLog(@"%@", error.localizedDescription);
}];
User Search
User search uses elastic search.
It can perform queries to find a list of users:
[MMUser searchUsers:@"userName:*john* OR firstName:*John*" limit:0 offset:0 sort: @"lastName:asc" success:^(NSArray<MMUser *> * users) {
NSLog(@"users %@", users);
} failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];
Find users by username:
[MMUser usersWithUserNames:@[@"UserName1", @"UserName2"] success:^(NSArray<MMUser *> * users) {
NSLog(@"users %@", users);
} failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];
And find users by userId:
[MMUser usersWithUserIDs:@[@"UserId1", @"UserId2"] success:^(NSArray<MMUser *> * users) {
NSLog(@"users %@", users);
} failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];
User Avatar
To set an avatar for a user, refer to the code below.
[myUser setAvatarWithData:imageData
success:^(NSURL * url) {
NSLog(@"%@", [myUser avatarURL]);
}
failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];
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:
[MMUser requestPasswordResetFor:@"USERNAME"
success:^{
NSLog(@"Success");
} failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];
NSString *otpCode = ...;
[MMUser resetPasswordWithOtpCode:otpCode
with:@"MyNewPassword"
for:@"USERNAME"
success:^{
NSLog(@"Success");
} failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];
To use a user's old password to authorize a password change:
[MMUser resetPasswordWithOld:@"OldPassword"
with:@"NewPassword"
for:@"USERNAME"
success:^{
NSLog(@"Success");
} failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];
Advanced
The following sections describe some advanced user management options available in Magnet Message.
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.
//Using the default SAML view controller. MagnetMax will handle the presentation of the view controller.
[MMUser loginWithViewController:[[MMXSAMLViewController alloc] init]
rememberMe: YES
success:^{
NSLog(@"Success");
} failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];
//providing your own webview from a custom viewcontroller. You will be responisible for presenting and dissmising the view controller
UIWebView *webview = ...;
[MMUser loginSAML:webview
rememberMe:NO
success:^{
NSLog(@"success");
} failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];
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.
MMUser *user = ...;
[user registerPendingUser:^(MMUser * user) {
NSLog(@"Success! User cant log in until approved on console");
} failure:^(NSError * error) {
NSLog(@"%@", error.localizedDescription);
}];