Flutter
...
User Authentication
Authentication

User Login

15min

User Login and Logout for Flutter using Parse Server

Introduction

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.

Goal

To build a User Login/Logout feature using Parse for a Flutter App.

Prerequisites

To complete this tutorial, you will need:

Understanding the Login/Logout App

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.

Let’s get started!

In the following steps, you will be able to build a Login/Logout App.

1 - Create the Login/Logout App Template

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:

Dart


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.

2 - Connect Template to Back4app Project

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.

Document image


3 - Code for Login User

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:

Dart


To build this function, follow these steps:

  1. 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.
  2. Call thelogin function, which will create a Session in your database in the Parse Dashboard and save the token to local storage
  3. Check if the user login was successful. If it wasn’t successful, show the error description message.

The complete function should look like this:

Dart


To test it, click on the Run button in Android Studio/VSCode.

Document image


After providing the desired user credentials, you will see this message after pressing on Login if everything was successful:

Document image


Error handling can be tested if you try to login a user with invalid credentials:

Document image


You will get another error if you try to login with no password:

Document image


4 - Code for Logout User

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:

Dart


To build this function, follow these steps:

  1. Get the current logged user using functionParseUser.currentUser().
  2. Call thelogout function for ParseUser object, which will delete Session in your database and clean the token in the local storage.
  3. Check if the user logout was successfull. If it wasn’t successful, show the error description message.

The complete code should look like this:

Dart


To test it, click on the Run button in Android Studio/VSCode.

Document image


After providing the desired user credentials, you will see this message after pressing on Login if everything was successful:

Document image


Click the “Logout” button:

Document image


It’s done!

At the end of this guide, you can login and logout Parse Users of your app using Parse Server core features through Back4App!