Laravel Authentication Tutorial #5 - Logging Users Out

Net Ninja · Beginner ·🔧 Backend Engineering ·1y ago

Key Takeaways

This video tutorial demonstrates how to implement a logout feature in a Laravel application, including creating routes, controller actions, and handling session data.

Full Transcript

all right then gang so in the last lesson we were able to register for a new account and when we did that larel created that new user for us in the users table but also behind the scenes it updated the session ID for us and we see that session cook it right here so now on every subsequent request that we send while we're logged into the application this session cookie gets sent to the server and using that session cookie LEL can identify whether we're authenticated or not currently we are because we just registered and use the login method if we have a log out then Lara will update the session cookie so that it knows that we are not logged in now we're logged in currently because we just registered so I think the next logical step would be to log users back out okay so the first thing we need to do is make a routes and a controller action for the logout request now when we send a logout request it's always a bit more secure to use a post request so I'm just going to copy and paste the login route right here which already uses the post method and then we just need to change the path to forward slash logout we also need to update the control action name as well which is going to be logout and finally we can call this route just log out all right so now let's go and make the logout action in the OR controller so down at the bottom below the login action we're going to make a new public function and that's going to be called logout and then we need a set of curly braces down here as well now we're going to come back to this action and flesh it out later but to begin with I want to create a button in the Navar which is going to trigger this post request to the login uh the logout rather route so let's open up the layout View and just add that in first of all now like I said a moment ago when we click on the button to log out it should be sending a post request to the server to that forward SL logout route and for that to happen we need to place the button inside a form and specify the method to be post so let's do that then let's make a new form tag down here at the end and for the action we're going to use the route function first of all and we're going to direct this towards the logout route we also need a method attribute which we're going to set to be post also I want to just add a Tailwind class to this form to strip out any margin just for stylistic purposes so let's add the M hyphen Zer class to do that all right so inside this form then we need the csrf directive to prevent cross site request forgery and then below that we need a button to submit the form and that's going to trigger the post request to the logout route and I'm going to give that a class of BTN as well just to style it for the text we can just say log out all right so now we have a button in the the nav bar which a user can click to log out and that sends a post request to the logout route which in turn calls the logout action inside the or controller so now then we just need to head to that action inside the controller and actually log the user out so then the first thing we're going to do in here is use the or class like we did before to log in but this time we're going to use the logout method instead to log the user out and this time we don't need to pass a user in as an argument so this function just logs out the current user in a sense that it breaks the connection between the current session and the authenticated user so it removes all user data like the user ID from the current session however it doesn't completely remove all the data from the session only the user data so any other the data in the session will remain for example if you were browsing a shopping website and you had a cart full of products then any products that were stored in the session well they would still remain even after logging out so to completely remove all the data from a session after a log out we can first of all accept a request argument up here which we automatically get access to in the action then we can use that request object and on that use a method called session and then on that session we can tack on another method called invalidate and this invalidate method will remove any other data associated with the session completely so it's often a good practice to do this kind of thing after a user logs out to clear the rest of the session data we're also going to use another session method after this which is recommended to do after you log a user out so let me just copy this down onto the next line as well and then we're going to replace the invalidate method with one called regenerate token so what this does is regenerate the csrf token for the next session and it means any forms which get submitted with the previous old token from the previous session they're just going to be flat out rejected so this is just an added layer of security and it's generally recommended that we do this after we log a user out you don't have to do both of these things but I wanted to mention them because it is a good practice in a lot of cases okay so now finally we want to redirect the user once they've logged out back to the login page so let's return a redirect here then tack on the route method and then as an argument to that we'll use the show. login route name to redirect back to the login view once you user logs out all right so now let's save this and we can see if this all works in the browser all right so I'm currently still authenticated from the previous lesson where we registered and logged the user in now I'm going to log out and if this works it should redirect me then back to the login page so let's click on this oops not loging sorry let's go back to the homepage we want to click on log out and when we do that yeah we get redirected now back to the login page cool

Original Description

In this Laravel authentication course, you'll learn how to set-up and implement an authentication flow (sign up, log in & sign out) into a Laravel application. 🔥🔥 Get (early) access to the entire Net Ninja Pro library, including exclusive courses and masterclasses, here - https://netninja.dev/p/net-ninja-pro/ 📂🥷🏼 Access the course files on GitHub: https://github.com/iamshaunjp/laravel-auth 📂🥷🏼 Starter project on GitHub: https://github.com/iamshaunjp/laravel-auth/tree/starter-project 🧠🥷🏼 Laravel 11 for Beginners: https://www.youtube.com/watch?v=DKnn8TlJ4MA&list=PL4cUxeGkcC9gF5Gez17eHcDIxrpVSBuVt 🧠🥷🏼 PHP & MySQL Crash Course: https://www.youtube.com/watch?v=pWG7ajC_OVo&list=PL4cUxeGkcC9gksOX3Kd9KPo-O68ncT05o 🔗🐄 Download & install Herd: https://herd.laravel.com/ 🔗👇 Laravel docs: https://laravel.com/docs/11.x/readme
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

This video tutorial shows how to implement a logout feature in a Laravel application, including creating routes, controller actions, and handling session data. The tutorial covers how to use the logout method to log out the current user, invalidate the session, and regenerate the CSRF token for added security.

Key Takeaways
  1. Create a route for the logout request using the POST method
  2. Create a controller action for the logout request
  3. Add a button to the Navar to trigger the logout request
  4. Use the logout method to log out the current user
  5. Invalidate the session to remove user data
  6. Regenerate the CSRF token for added security
  7. Redirect the user to the login page after logout
💡 It's a good practice to invalidate the session and regenerate the CSRF token after a user logs out to ensure security and prevent unauthorized access.

Related Reads

📰
Why Check Then Act Is the Silent Killer in Banking Backends, and How NestJS Prevents It
Learn how 'Check Then Act' can cause issues in banking backends and how NestJS helps prevent it
Dev.to · Peace Melodi
📰
PDF Tamper Detection API for Laravel and PHP: Integration Guide
Learn to integrate a PDF tamper detection API into Laravel and PHP applications to ensure document integrity
Dev.to · Iurii Rogulia
📰
How I Built a Concurrency-Safe Reservation System
Learn how to build a concurrency-safe reservation system using FastAPI, PostgreSQL, Redis, and SQLAlchemy to prevent race conditions
Dev.to · Rahul Chaduvula
📰
The Object in Your Field Is Not the Object You Wrote
Understand how @Transactional works and why it may silently fail due to proxy classes, and learn how to troubleshoot and resolve these issues
Medium · Programming
Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →