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.

let user = MMUser()! user.firstName = "John" user.lastName = "Doe" user.userName = "[email protected]" user.password = "magnet" user.register({ user in print(user.userName ?? "") }, failure: { error in print(error.localizedDescription) })

User Login

The following shows the basic way to log in a user. For advanced options, see Advanced.

let username = "[email protected]" let password = "magnet" let credential = URLCredential(user: username, password: password, persistence: .none) MMUser.login(credential, success: { print(MMUser.currentUser()?.userName ?? "") }, failure: { error in print(error.localizedDescription) })

User Logout

The following shows how to log out a user.

MMUser.logout({ print("logged out") }, failure: { error in print(error.localizedDescription) })

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: { users in print(users) }, failure: { error in print(error.localizedDescription) })

Find users by username:

MMUser.usersWithUserNames(["UserName1", "UserName2"], success: { users in print(users) }, failure: { error in print(error.localizedDescription) })

And find users by userId:

MMUser.usersWithUserIDs(["UserId1", "UserId2"], success: { users in print(users) }, failure: { error in print(error.localizedDescription) })

User Avatar

To set an avatar for a user, refer to the code below.

myUser.setAvatarWithData(imageData, success: { url in print(myUser.avatarURL()?.absoluteString ?? "") }, failure: { error in print(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.requestPasswordReset(for: "USERNAME", success: { print("Success!") }, failure: { error in print(error.localizedDescription) }) let usersOTPCode = … MMUser.resetPassword(otpCode: usersOTPCode, with: "MyNewPassword", for: "USERNAME", success: { print("Success!") }, failure: { error in print(error.localizedDescription) })

To use a user’s old password to authorize a password change:

MMUser.resetPassword(old: "OldPassword", with: "NewPassword", for: "USERNAME", success: { print("Success!") }, failure: { error in print(error.localizedDescription) })

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:

//Using the default SAML view controller. MagnetMax will handle the presentation of the view controller. MMUser.loginWithViewController(MMXSAMLViewController(), rememberMe: true, success: { print("Success!") }, failure: { error in print(error.localizedDescription) }) //providing your own webview from a custom viewcontroller. You will be responisible for presenting and dissmising the view controller let webView = … MMUser.loginSAML(webView, rememberMe:false, success: { print("Success!") }, failure: { error in print(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.

let user = … user.registerPendingUser({ user in print("Success! User cant log in until approved on console") }, failure: { error in print(error.localizedDescription) })