React
Users

User LogIn

10min

User LogIn and LogOut for React

Introduction

After implementing a component that handles user registration in Parse in the last guide, you will now learn how to log in and log out users using the same Parse.User class.

The Parse.User.logIn method stores in your local storage a valid user session, so future calls to methods like current will successfully retrieve your User data. On the other hand, logOut will clear this session from the disk and log out of any linked services in your Parse server.

Prerequisites

To complete this tutorial, you will need:

Goal

To build a User LogIn and LogOut feature using Parse for a React App.

1 - Understanding the logIn and logOut methods

Parse User management uses the Parse.User object type, which extends the default ParseObject type while containing unique helper methods, such as current and getUsername, that will help you retrieve user data throughout your app. You can read more about the Parse.User object here at the official documentation.

In this guide, you will learn how to use the logIn and logOut methods that will handle the user login process, saving the user data locally.

2 - Creating a login component

Let’s now build the UserLogIn component in your App. Create a new file in your src directory called UserLogIn.js (UserLogIn.tsx if you are using TypeScript) and add the input elements using state hooks to manage their data. Note that there is also a function responsible for updating a state variable with the current logged in user, that you will use later:

UserLogIn.js
UserLogIn.tsx


You can now implement the function that will call the Parse.User.logIn method, using state variables:

JavaScript
TypeScript


Insert this function inside the UserLogIn component, just before the return call, to be called and tested. Remember to update the form’s login button onClick action to () => doUserLogIn(). If you want to fully render this screen layout, add these classes to your App.css file:

App.css


Go ahead and run your application, importing this login component into your main application file. You should see a screen just like this and also a success message after logging in with the right credentials:

Document image


3 - Checking logged-in user

After your user successfully logs in to the application, he should be able to know about that and also be able to log out of the application. First, let’s hide the login form and render a welcome message containing the username. Note the usage of the conditional operator in conjunction with the state variable holding the current username.

UserLogIn.js
UserLogIn.tsx


Go ahead and run your app again. Notice that your app will now show the user screen and welcome message just like this:

Document image


4 - Creating a log out function

The logOut method is simpler than the login since the Parse.User.logOut method takes no arguments and clears the currentUser data stored locally automatically. Create this function in the UserLogin component and call it in the onClick attribute of the logout button:

UserLogOut.js
UserLogOut.tsx


If you perform a successful log out, you will see a message like this:

Document image


Here is the full component code containing all the functions shown before:

UserLogIn.js
UserLogIn.tsx


Conclusion

At the end of this guide, you learned how to log in and log out Parse users on React. In the next guide, we will show you how to perform useful user queries.