User Service

Last Updated on : 2024-05-28 03:21:47download

This topic describes user services, allowing you to access login information and log in or out of a user account.

Common properties

IUser methods

Method Description
getUserId(): String The unique identifier of the user.
getUserName(): String The name of the user.
getSpaceType(): Int The current space system.
  • 1: The space system.
  • 2: The asset system.

Get the user logged in

Example

let user = UserService.shared().user()

User login

- (void)loginWithParams:(NSDictionary *)params
                success:(void(^)(void))success
                failure:(void(^)(NSError *))failure;

Parameters

Parameter Type Required Description
params NSDictionary Yes Parameters
  • username: The username.
  • password: The password.
  • projectCode: The project code.
success (() -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

        let params = [
            "username": "your account",
            "password": "your password",
            "projectCode": AppKey.projectCode,
        ]

        UserService.shared().login(withParams: params) {
            printf("success")
        } failure: { error in
            printf("error")
        }

User logout

- (void)logoutSuccess:(void(^)(void))success
              failure:(void(^)(NSError *))failure;

Parameters

Parameter Type Required Description
success (() -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

UserService.shared().logoutSuccess {
    printf("success")
} failure: { error in
    printf("error")
}

Query user information

- (void)getUserDetailWithParams:(NSDictionary *_Nullable)params
                        success:(void(^)(NSDictionary *))success
                        failure:(void(^)(NSError *))failure

Parameters

Parameter Type Required Description
params NSDictionary No Parameters
success (() -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

    UserService.shared().getUserDetail(withParams: nil, success: { userDetails in
        print("User details: \(userDetails)")
    } failure: { error in
        print("Error: \(error.localizedDescription)")
    })

Check whether the user is logged in

Example

if (let isLogin = UserService.shared().user()?.isLogin()) {
    print("login state")
} else {
    print("no login")
}

Register a listener for login session expiration

After you register a listener for login session expiration, this callback will be triggered when the user changes their password or remains inactive for a period. A successful callback indicates that the login has expired and prompts the user to log in again.

Example

import IndustryAuthKit

// Adding an observer for the login notification
NotificationCenter.default.addObserver(
    self,
    selector: #selector(loginAction),
    name: NSNotification.Name("industryAuthKitDidAuth"),
    object: nil)

// Adding an observer for the logout notification
NotificationCenter.default.addObserver(
    self,
    selector: #selector(logoutAction),
    name: NSNotification.Name("industryAuthKitDidUnauth"),
    object: nil)

// Adding an observer for the logout notification due to invalid authentication
NotificationCenter.default.addObserver(
    self,
    selector: #selector(logoutAction),
    name: NSNotification.Name("industryAuthKitAuthDidInvalid"),
    object: nil)

@objc func loginAction() {
    // Handle login
    print("Logged in successfully")
}

@objc func logoutAction() {
    // Handle logout
    print("Logged out successfully or session invalidated")
}