id: flutter-sdk-nuiauth title: NUIAuth Overview sidebar_label: Overview

IAuth is part of the Flutter SDK that allows you to integrate a built-ready user authentication features into the app in a more convenient and simpler way:

Builder Method for NUIAuth

MethodRemark
baseUrl()Specify the base url for the NUIAuth api end point
appVersion()Specifying the current app version
tokenExpiredListener()Add a callback to know when the the access token has expired
build()Build the NUIAuth instance with the builder.

Initialize NUIAuth with NUIAuthBuilder.

Create a NUIAuthBuilder instance to build the NUIAuth instance.

var builder = NUIAuthBuilder();

Specifying the base URL for NUIAuth.

On the builder method, users are required to specify the base URL, as NUIAuth works together with a backend framework.

builder.baseUrl("https://inglabcloud.hopto.org");

Specifying the app version for NUIAuth.

On the builder method, users are required to specify the current version of the app as well for logging purpose.

builder.appVersion("1.0.2");

Adding a callback for token expired.

On the builder method, users can add a callback or listener to get notified when the access token has expired and a re-login is required.

builder.tokenExpiredListner(() {
    //TODO: Do something here, such as redirecting the user to login again
});

Build the NUIAuth instance

Once all the configurations are done for the NUIAuthBuilder, building the instance will return the instance of the NUIAuth.

NUIAuth auth = builder.build();

A Complete Example for Building A NUIAuth Instance

final auth = NUIAuthBuilder()
        .appVersion("1.0.1"))
        .baseUrl("https://inglabhopto.org")
        .tokenExpiredListener(() {
            //Redirect to login screen
          })
        .build();

Available Methods for NUIAuth

MethodRemark
get()Get the instance of the built NUIAuth instance
signup()Sign up a new user using a username, password and email
login()Login a user using the username and password
verifyEmail()Verify the email used in registration using a digit code sent to the email
resendEmailVerificationCode()Resend an email verification code to the registered email
changePassword()Change the user's password by giving the previous password and a new password
forgotPassword()Send an email to the email specified to reset the user's password
renewAccessToken()Renew the access token if the refresh token has not expired
logout()Log the current user out for a new login
isLoggedIn()Check if a user has been logged in
getUserId()Get the current logged in user ID
logUserActivity()Log an activity under the logged in user for logging purpose or audit trails
updateUserInfo()Update or save some user related information to database
loginWithApple()Login a user with Google Sign In
loginWithGoogle()Login a user with Apple ID

Getting the NUIAuth Instance

Users can get the NUIAuth instance that was previously built with NUIAuthBuilder.

final auth = NUIAuth.get();

Sign Up

This can be used to sign up a new user into the system.

final signUpResult = await NUIAuth.get().signup(
          username: "testing@hotmail.com",
          password: "Testing123",
          email: "testing@hotmail.com");

Log In

This can be used to login/authenticate user to successfully retrieve an access token

final result = await NUIAuth.get().login(username: "testing@hotmail.com", password: "Testing123");

Verify Email

This can be used to verify a newly registered email to validate that the user is the owner of the email.

final result = await NUIAuth.get().verifyEmail(code: "123321");

Resend Email Verification Code

This can be used to resent an email verification code if the user failed to verify the email due to the expiry of the current verification code

final result = await NUIAuth.get().resendEmailVerificationCode();

Change Password

This can be used to change the password of the currently logged in user

final result = await NUIAuth.get().changePassword(oldPassword: "Testing123", newPassword: "Testing@123", confirmPassword: "Testing@123");

Forgot Password

This can be used to send a reset password email to the specified email

final result = await NUIAuth.get().forgotPassword(email: "testing@hotmail.com");

Renew Access Token

This can be used to renew the expired access token if the refresh token has not expired

final result = await NUIAuth.get().renewAccessToken();

Log Out

This can be used to logout the currently logged in user

final result = await NUIAuth.get().logout();

Is Logged In

This can be used to check if there is a logged in user

final result = await NUIAuth.get().isLoggedIn();

Get User ID

This can be used to get the currently logged in user ID

final result = await NUIAuth.get().getUserId();

Log User Activity

This can be used to log user activities to record the actions by the currently logged in user

final userAct = NUIUserActivity(
    module: "My Profile",
    action: "Launch",
    time: DateTime.now()
);

final result = await NUIAuth.get().logUserActivity(userActivity: userAct);

Update User Info

This can be used to update user related information such as date of birth, name, address or any other info that is tied to the user

final userInfo = NUIUserInfo(
    gender: "M",
    dateOfBirth: "03/03/1997",
    others: {
        "city": "Bangsar",
        "state": "WP Kuala Lumpur",
        "country": "Malaysia"
    }
);

final result = await NUIAuth.get().updateUserInfo(userInfo: userInfo);

Log In With Apple

This can be used to authenticate the user using Apple Sign In. In Progress

Log In With Google

This can be used to authenticate the user using Google Sign In. In Progress

NUIAuthInterceptor

As the access token will expire, programmers can either renew the access token manually through renewAccessToken() or add the NUIAuthInterceptor into the NUIWeb or IngDao frameworks to renew the token automatically if possible or return 401 if the refresh token has also expired. The example below shows an interceptor that when received a 401, which means the refresh token has expired, will send a bus event to notify the receiver that the session has expired and a new login session is required.

NUIAuthWebInterceptor(onTokenExpired: () async {
            logNUI("DataFactory", "Token has expired and failed to refresh, go back to login screen");
            NUIBusEvent.get().send(NUIBusEventData<String>(
                data: "Session expired",
                sender: "Web interceptor",
                code: "401"));
          });

Libraries

nui_apple_login_result
nui_auth
nui_auth_activity_screen
nui_auth_api
nui_auth_result
nui_auth_token
nui_auth_web_json
nui_google_login_result
nui_user_activity
nui_user_info