This tutorial shows how to use PlusAuth with Flutter Application. If you do not have a PlusAuth account, register from here.
Create PlusAuth Client
After you sign up or log in to PlusAuth, you need to create a client to get the necessary configuration keys in the dashboard. Go to Clients and create a client with the type of Native Application
Configure Client
Get Client Properties
You will need your Client Id
for interacting with PlusAuth. You can retrieve them from the created client's details.
Configure Redirect and Logout URIs
When PlusAuth authenticates a user, it needs a URI to redirect back. That URI must be in your client's Redirect URI
list. If your application uses a redirect URI that is not white-listed in your PlusAuth Client, you will receive an error.
The same thing applies to the logout URIs. After the user logs out, you need a URI to be redirected.
Configure Flutter to use PlusAuth
Create a Flutter application or download the sample project from the link on the top of the page.
Add Dependencies
To get started, install the following dependencies.
- flutter_appauth - a Flutter bridge for AppAuth used authenticating and authorizing users
- http - a composable library for making HTTP requests.
- flutter_secure_storage - a Flutter plugin to store data in secure storage
Android Setup
Add redirect scheme to your Android app module's build.gradle
file.
Add the following queries to your AndroidManifest.xml
file a level underneath the <manifest>
element.
iOS Setup
Go to the Info.plist
for your iOS app and add a redirect scheme like the following.
Configure FlutterAppAuth Instance
We need to initialize our Flutter AppAuth library to handle authentication-related operations. Create a new FlutterAppAuth
instance in main.dart
under lib
folder.
Replace _clientId
value with your Client Id
defined in the Configure Client section. Also change the _issuer
value with your Tenant Id
.
Implement login, user profile, and logout
Add the following sections to your main.dart
file under the lib
folder. We will store the retrieved refresh_token
in secure storage to exchange with new tokens.
Add Login
We will be using the authorizeAndExchangeCode
method to authenticate the user. The application redirects you to the PlusAuth
login page using the browser.
Add the following section to your main.dart
file to start an authentication process.
After successful authentication, extract the tokens from response data. We will be storing refresh_token
in local storage to retrieve new tokens later.
Add Logout
Add the following section to log out from the application and PlusAuth:
Clear all the tokens and storage after the successful logout process.
Refreshing Tokens
Add the following section to renew tokens using refresh_token
that we stored in local storage.
Extract new tokens from TokenResponse
and store renewed Refresh Token
in local storage.
Add View
Finally, add the following section to your Widget
to interact with the user.
See it in action
That's it. Start your app and point to your device or emulator. Follow the Log In link to log in or sign up to your PlusAuth tenant. After you click the Login
button, the browser launches and shows the PlusAuth Login page. Upon successful login or signup, you should be redirected back to the application.