React Native
...
Relay (GraphQL)
Users

User Logout

10min

React Native User Logout

Introduction

In this step you will create the user Logout for React Native using Relay, the last implementation for this User’s section. This step is simple and we will follow our GraphQL Logout Guide from our GraphQL Cookbook. You will implement the Logout mutation and call it using a button into the React Native Application.

At any time, you can access this project via our GitHub repositories to checkout the styles and complete code.

Goal

To implement the Logout feature to our React Native App using Relay and the Back4App GraphQL API.

Prerequisites

1 - Creating the Logout Mutation

Back again to the SignIn folder, into mutations folder create a new file and call it LogOutMutation.js.

Copy and paste the following code inside:

JS


Run yarn relay into your terminal to update the relay types:

Document image


2 - Creating logout button

Now, open the FormSignIn.js component. Let’s add the Logout Button and call the Relay Mutation.

Import the new Relay Mutation on begin of file:

JS


Then, create the function responsible to call the LogOut Mutation:

JS


What is happening into onCompleted:

  • it is removing the session token from Async Storage because it is not valid anymore.
  • is cleaning the local useState that retrieves the session token for the same reason above.
  • an alert saying the user it was logged out with success.

After, right below the UserLoggedRenderer, let’s implement the button responsible to call the logout:

From:

JS


To

JS


Import the Button from react-native lib import { Button, Text, TextInput, View, TouchableOpacity } from "react-native";

The application screen should look like this:

Document image


3 - Testing

Testing the button, when click should be made the logout and appears an alert:

Document image


And, after should return to Login page:

Document image


Conclusion

Now you’ve implemented the main user authentication features necessary to any App. Your users can now SignUp, Login, navigate on authenticated area and LogOut from your React Native App using Relay and Back4App GraphQL API.