Flutter
...
User Authentication
Authentication

Current User

13min

Get current User on session

Introduction

It would not be enjoyable if the user had to log in every time they open your app. You can avoid this by using the cached current ParseUser object. Whenever you use any signup or login methods, the user is cached locally. You can manage this cache as a session, and automatically assume the user is logged in. In this guide, you will learn how to use the Flutter plugin for Parse Server to get current User on session using ParseUser class for your Flutter App.

Goal

Get current User on session using Parse for a Flutter App.

Prerequisites

To complete this tutorial, you will need:

Understanding the Get Current User App

To better understand the get current User on session process, we will create an app to signUp, login e logout a user. The application is similar to the previous guide, where we perform the signup, login, and logout. As we’re going to use the same project in the following guides, you can find some not yet available functions. 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 app a get current user.

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 help you debug the code. It is prudent 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 get current user on Session

The User Login or SignUp function creates a Session object, which points to the User logged in and stores in your local storage a valid user session.

Calls to methods currentUser will successfully retrieve your ParseUser data and sessionToken for Session object.

Search for the function hasUserLogged in the file main.dart.

Replace the code inside hasUserLogged with:

JS


To build this function, follow these steps:

  1. Call the ParseUser.currentUser() function, which will return a ParseUser object from local storage.
  2. IfParseUser is null, we do not have any users with an active session in the app.
  3. IfParseUser is not null, we have a user with an active session in our app.
  4. The user’s session needs to be validated on the Parse Server, as it has a lifetime.
  5. If the token is not valid, it is necessary to call the logout function to clear the current session and the user needs to log in again.

The complete function should look like this:

JS


Look for the function getUser in the file main.dart.

Replace the code inside getUser with:

Dart


To build this function, follow these steps:

  1. Call the ParseUser.currentUser() function, which will return a ParseUser object from local storage.

The complete function should look like this:

Dart


To test it, click on theRunbutton in Android Studio/VSCode.

Document image


SignUp or Login and the next screen will display the username of the logged in user.

Document image


Quit the application and run again.

If a valid user session is identified, the screen with the user’s username will be displayed.

Document image


It’s done!

At the end of this guide, you can get current user on Session of your app using Parse Server core features through Back4App!

Updated 24 Jul 2024
Did this page help you?