Laravel Authentication Tutorial #5 - Logging Users Out
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
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
Related Reads
📰
📰
📰
📰
Why Check Then Act Is the Silent Killer in Banking Backends, and How NestJS Prevents It
Dev.to · Peace Melodi
PDF Tamper Detection API for Laravel and PHP: Integration Guide
Dev.to · Iurii Rogulia
How I Built a Concurrency-Safe Reservation System
Dev.to · Rahul Chaduvula
The Object in Your Field Is Not the Object You Wrote
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI