Last updated:
0 purchases
nui auth
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 #
Method
Remark
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();
copied to clipboard
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");
copied to clipboard
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");
copied to clipboard
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
});
copied to clipboard
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();
copied to clipboard
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();
copied to clipboard
Available Methods for NUIAuth #
Method
Remark
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();
copied to clipboard
Sign Up #
This can be used to sign up a new user into the system.
final signUpResult = await NUIAuth.get().signup(
username: "[email protected]",
password: "Testing123",
email: "[email protected]");
copied to clipboard
Log In #
This can be used to login/authenticate user to successfully retrieve an access token
final result = await NUIAuth.get().login(username: "[email protected]", password: "Testing123");
copied to clipboard
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");
copied to clipboard
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();
copied to clipboard
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");
copied to clipboard
Forgot Password #
This can be used to send a reset password email to the specified email
final result = await NUIAuth.get().forgotPassword(email: "[email protected]");
copied to clipboard
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();
copied to clipboard
Log Out #
This can be used to logout the currently logged in user
final result = await NUIAuth.get().logout();
copied to clipboard
Is Logged In #
This can be used to check if there is a logged in user
final result = await NUIAuth.get().isLoggedIn();
copied to clipboard
Get User ID #
This can be used to get the currently logged in user ID
final result = await NUIAuth.get().getUserId();
copied to clipboard
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);
copied to clipboard
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);
copied to clipboard
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"));
});
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.