User Login
After implementing the User Registration for Flutter on Parse in the last guide, you will learn how to login and logout users using the same ParseUser class. After a Signup, the login operation is performed automatically, and a new user session is created. The Logout operation deletes the active Session object for the logged user.
In this guide, you will learn how to use the Flutter plugin for Parse Server to perform login/logout using ParseUser class for your Flutter App.
To build a User Login/Logout feature using Parse for a Flutter App.
To complete this tutorial, you will need:
- An Flutter app connected to Back4app.
- Note: Follow the Install Parse SDK on Flutter project to create an Flutter Project connected to Back4App.
- Complete the previous guide so you can have a better understanding of the ParseUser class.
- A device (or virtual device) running Android or iOS.
To better understand the Login/SingOut process, we will create an app to login e logout user on your account.
We won’t explain the Flutter application code once this guide’s primary focus is using the Flutter with Parse. Following the next steps, you will build a Login e Logout App at Back4App Database.
In the following steps, you will be able to build a Login/Logout App.
Open your Flutter project from the previous guide Flutter plugin for Parse Server. Go to the main.dart file, clean up all the code, and replace it with:
When debug parameter in function Parse().initialize is true, allows displaying Parse API calls on the console. This configuration can assist in debugging the code. It is advisable to disable debug in the release version.
Find your Application Id and Client Key credentials navigating to your app Dashboard at Back4App Website.
Update your code in main.dart with the values of your project’s ApplicationId and ClientKey in Back4app.
- keyApplicationId = App Id
- keyClientKey = Client Key
Run the project, and the app will load as shown in the image.
The User Login function creates a Session object, which points to the User logged in and stores in your local storage a valid user session.
Future calls to methods like currentUser will successfully retrieve your User data and sessionToken for Session object which created in the Dashboard.
Search for the function doUserLogin in the file main.dart. Replace the code inside doUserLogin with:
To build this function, follow these steps:
- Create a newParseUser class instance with the command ParseUser(username, password, null); using the data entered in the app. The e-mail field is not necessary and must be informed with null.
- Call thelogin function, which will create a Session in your database in the Parse Dashboard and save the token to local storage
- Check if the user login was successful. If it wasn’t successful, show the error description message.
The complete function should look like this:
To test it, click on the Run button in Android Studio/VSCode.
After providing the desired user credentials, you will see this message after pressing on Login if everything was successful:
Error handling can be tested if you try to login a user with invalid credentials:
You will get another error if you try to login with no password:
The User Logout function deletes the Session object, which was created in the login function. It will clear this session on the device and log out of any linked services in your Parse server.
Search for the function doUserLogout in the file main.dart. Replace the code inside doUserLogout with:
To build this function, follow these steps:
- Get the current logged user using functionParseUser.currentUser().
- Call thelogout function for ParseUser object, which will delete Session in your database and clean the token in the local storage.
- Check if the user logout was successfull. If it wasn’t successful, show the error description message.
The complete code should look like this:
To test it, click on the Run button in Android Studio/VSCode.
After providing the desired user credentials, you will see this message after pressing on Login if everything was successful:
Click the “Logout” button:
At the end of this guide, you can login and logout Parse Users of your app using Parse Server core features through Back4App!