MERN Authentication Tutorial #11 - Making a useLogout Hook
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
Additive column can 500 every login (Drizzle + Better Auth + preview deploys)
Dev.to · Omar Bni
Why Auth0 E2E Tests Fail After Login: The localStorage Race Condition
Dev.to · Anand Rathnas
Basic Git Command and What is Git
Medium · Programming
When to Use an ORM vs SQL and Query Builders
Dev.to · Doogal Simpson
🎓
Tutor Explanation
DeepCamp AI