MERN Authentication Tutorial #11 - Making a useLogout Hook

Net Ninja · Intermediate ·🔧 Backend Engineering ·3y ago

Key Takeaways

The video demonstrates creating a custom useLogout hook in a MERN authentication application, allowing users to log out by updating the global state and deleting the JSON Web Token from local storage. The hook is then used in a logout button within the navigation bar.

Full Transcript

all right then so in the last video we finished this use sign up hawk and basically we created a sign up function to sign the user up and then when we got this json response we did a couple of things first of all we stored that in local storage under a user property so this includes this response remember a json web token and the email so we stored those two things in json format in local storage so we have that json web token stored now and the reason we do that remember is because if the user crosses off our website and returns a couple of hours later our global state for that user will have reset to null however we can still detect that a user is logged in with the presence of that json web token in local storage this user property right here because that stays so we can detect that and update our state dependent on the presence of that and we'll do that later but after we do that we also update our auth context global states by dispatching this login action right here where the payload is json again this response right here where we have an email property and the json web token the token property so what i'd like to do now is show you how we can log out of the application now i did say at the end of the last video i was going to show you how to create a use login hook but i thought seeing as though we now have that json web token stored in local storage and we're technically logged in the next logical step would be to log out of the application so we're actually going to create a use logout hook now so let's call this use logout.js and then inside here i want to create this and export it so export const use log out and we set that equal to a function and it doesn't need any arguments or anything like that and inside here we want a function called log out and inside that function we don't actually need to send a request to the back end because think about it how do we log out of the application well we have two things in our react application saying that we're logged in the global state and we could easily change that by just dispatching a logout action and also the json web token in local storage and we can delete that as well so if we do both of those things then technically we're logged out because then if we send a request to the back end we're not going to be sending that token or the information from that token to the back end because it no longer exists so we don't need to send a request to the server in order to log out we just need to do those two things update the global state and also delete the token from the local storage so let's do that first of all remove user from storage all right so we can say local storage dots remove item and the item we want to remove is the user item that's what we called it over here so that's the first thing the next thing we want to do is dispatch a logout action now to do that we need to import the use auth context hook so i'm going to do that at the top of the file paste in import use auth context from use auth context and we can now invoke that hook down here and grab the dispatch function from it so that's equal to use auth context all right so now down here we can do another comment to say dispatch logout action and then we can use that dispatch function and the type this time is going to be log out now there's no payload in this case because if we go to the auth context for the case log out we don't need the payload we just reset the user to be null all right so we're doing those two things now and all we need to do is return this function at the bottom over here so we can use it so return oops not in capitals return and we want to return log out all right so now we can use this somewhere in our application but where do we want to use it well i want to have a logout button inside the nav bar so what i'm going to do is come above this div right here and i'm going to create another div and inside here we'll create a button and we'll say log out inside this button and we also need a click handler for this one click set that equal to something and it's going to be called handle click and let's create that function now so const handle click is equal to a function and inside this function we just want to call the logout function so we need to import use logout at the top first import use logouts and this is going to come from dot dot forward slash to come out of this components folder then into the hooks folder and then we want use logout and now we can invoke it down here so we can say const logout is equal to use logout all right so now all we need to do is call this logout function right here so now when we click on this button it should call this logout function that should delete the item from local storage the token and also our global state it should update that so the user becomes null again so that's pretty much it and by the way the reason i've done this in a separate div to this over here is just so that later we're either going to show this the log out button if someone's logged in or these two links if they're not logged in not both at the same time but we'll do that later what i do want to quickly do is style this button inside the css so it looks a bit nicer so let's do it below here i'm going to paste it in the background is white the color is the primary color that's a variable up here this green color scoot this right back down alright so the border two pixel solid primary color again green padding border radius the font families pop ins cursor pointer font size one m so dead simple style so let's save this and preview in the browser all right then so now we can see in our local storage we have this user property so we're kind of logged in where the email is told at netninja.dev and we have the token as well we can see that token over there and down here now if i log out it should delete that but also we should see in the console an update of the auth context state so the user becomes null again so let's log out do that and yep this goes awesome in the console we can see now as well the user is null so now my friends we have successfully logged out of the application so next up we are going to look at logging back in by creating a use login hook

Original Description

In this MERN auth tutorial, we'll make a custom useLogout hook to log users out of the application. ⭐⭐ Get the full course now (without ads) on the Net Ninja Pro site: https://netninja.dev/p/mern-auth-tutorial/ ⭐⭐ Get access to all free & PREMIUM courses on Net Ninja Pro: https://net-ninja-pro.teachable.com/p/net-ninja-pro/ 🐱‍💻 Access the course files on GitHub: https://github.com/iamshaunjp/MERN-Auth-Tutorial 🐱‍💻 MERN Stack Tutorial: On Net Ninja Pro - https://www.youtube.com/watch?v=2liZ3uvO9bs On YouTube - https://www.youtube.com/watch?v=98BzS5Oz5E4&list=PL4cUxeGkcC9iJ_KkrkBZWZRHVwnzLIoUE 🐱‍💻 React Tutorial: On Net Ninja Pro - https://codinginpublic.dev/projects/parallax-landing-page/ On YouTube - https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d 🔥🔥🔥 Other resources: VS Code - https://code.visualstudio.com/ MongoDB Atlas - https://code.visualstudio.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Create a custom useLogout hook to log users out of a MERN application by updating the global state and deleting the JSON Web Token from local storage. Use the hook in a logout button within the navigation bar.

Key Takeaways
  1. Create a new file for the useLogout hook
  2. Import the useAuthContext hook
  3. Define the logout function to update the global state and delete the JSON Web Token
  4. Return the logout function from the useLogout hook
  5. Use the useLogout hook in a logout button within the navigation bar
  6. Style the logout button using CSS
💡 The useLogout hook updates the global state and deletes the JSON Web Token from local storage to log users out of the application.

Related Reads

Up next
/dev/push: An Open Vercel Alternative to Ship Your Apps Quickly
Ian Wootten
Watch →