Laravel Authentication Tutorial #4 - Registering New Users
Key Takeaways
The video tutorial covers Laravel authentication, specifically registering new users, using tools like Laravel, AuthController, and Tailwind for styling validation errors. It demonstrates how to validate user input, hash passwords, and log users in using Laravel's authentication system.
Full Transcript
all right then so now we've hooked the registration and login forms up to a couple of routes to handle post requests from those forms and in turn those routes are hooked up to the or controller where we can write the logic to actually handle those requests so we've got an action called login to handle requests from the login form and we've got one called register to handle requests from the register form now in this lesson we're just going to be focused on the register action to register new users so then inside this action we get access to the request object as an argument which is of type request okay and on this request object we're able to access and validate the user input values from the form and that's the first thing we want to do right we want to validate the user input to make sure they're registering with maybe a unique email a password that's long enough Etc so the way we can do this is by making a new variable which I'm going to call validated call it what you want and we can set that equal to the request object and then we'll use a method on that called validate now this method takes an array as an argument and inside this array we can use key value pairs to specify validation rules for each input field so the key would be the input name attribute from the form and the value would be the validation criteria for that input for example we've got an input with a name attribute value of name and we could use that as the first key right here and then as an argument we'll have a string of different validation criteria the first one could be that it's required so we type out required then to add another we use a pipe and then the next one which would be that it must be maybe a string value okay then we can add a pipe to specify another after this which could be that the max number of characters for the name is 255 and then the value we submit for the name field must pass all these validation checks okay okay so the next input field had the name attribute of email so we'll use that as the next key and then as a value we'll use a string again and also say that this field is required then we'll say it must be an email and larel checks this according to its own allowed email patterns for us so we don't have to worry about making any kind of regular expressions or anything like that ourselves and finally we'll say the email must be unique within the users table so we can say unique then a colon and then users to specify the table name to do that cool so now we just need to validate the password so let's make another key with the name of password and for the value of that we'll do another string and we'll say it's required again then a pipe then we'll say it must be a string then another pipe to say that the minimum length is eight characters long and then finally I'll add another pipe and say the password must be confirmed now this last check is really useful and it saves us a lot of time so in the register form we had two inputs for the password right we had the first one with the name attribute of password and the second one with a name attribute of password underscore confirmation and when we created this form I said the name value of the second password input was very important and that it must be called password uncore confirmation and this is why because when we specify in the validate method that the password must be confirmed laravel automatically looks for a second password input where the name value is password uncore confirmation and it compares the password input value to that to make sure they're both the same but for that to work the name attribute value must be called password uncore confirmation so that's nice larel automatically checks the password Fields match for us and I think we've done all the valid a checks here all we have to do now is create the new user to do that we'll use the user model and we'll also make sure we're using the correct usern name space and then we'll use the create method on that user model and by the way when we create a new larl application larl automatically creates this user model for us along with a user table okay so as an argument we just need to pass in the validated variable which contains all the validated fields that a user needs and when we do this laral saves that new user to the user's table now by the way this validated method right here it actually returns an array of key value pairs where the key is the name of the field like name password Etc and the value is the input value of that so that's what we're passing into the create method and that's what it expects all right so now we've validated the user input oh one other thing sorry we um saved the password which is a string right but a nice little feature of more recent versions of LEL is that it automatically hashes the password for us so it's not going to be storing those passwords as plain text in the table which is good right so we've validated the user input and we've created the new user now what well even though we've saved that user to the database that's pretty much all we've done right we've not authenticated or logged that user in yet we've not created a session cookie for them either so we need to do that next and larl makes this really simple for us because it comes fully baked with its own authentication Service that we can use now technically this or service is called a facad which is basically a fancy way of saying it's a programmatic interface for some other complex system and in Lal there's a few different facades for different things and they're essentially just classes that we can use to do a bunch of stuff easily so we'll be using the or faad to log users in really easily and then LL does all of the heaven lifting in the background for us it generates a session token and a cookie for the user and it keeps track of whether a user is currently authenticated or not to use it we can start typing or and then you'll see this name space with the word facade in it so make sure you select that one to use it and then on this or class we can then use a method called log in and we can invoke that and pass a user model instance into it now we don't actually have a user instance anywhere here but this create method returns one for US based on the user it's creating so we can just store the return of that in a variable called user and then we can take that and we can pass it into the login method and that's it my friends that's all we need to do laravel then takes care of everything else for us now there is just one more thing I want to do in this action and that is to redirect the user back to the index page so we can say down here return then use the redirect function and invoke it and we'll tack on the route method to specify the name of the route we want to redirect to and in our case that is the ninjas do index route okay so we'll try this out in a moment but I want to handle one more thing before we do and that is any validation errors if there are any so when we use the validate method up here if some of the validation fails then larl redirects us back to the page that we were on the register page in this case and it sends those errors back to so that we can use them within that page view to display them to the user so I'm going to head to that register page view and I'm going to scroll right to the bottom of the form and I'm going to paste in a little snippet of code now if you've taken my Lal beginners course this should look very familiar because we did something very similar what we're doing is a little of check to see if we have any errors and if we do then we output a UL tag with a few Tailwind classes so when I said that the validation method sends back some errors into the view if there's any errors present then this is going to be true right here we'll have errors okay so inside here we Loop through all those validation errors that we have and we output each one as its own Li tag so now if say the passwords don't match for example or the email Isn't unique then we're going to list those ER provided by LL right here at the bottom of the form all right so I'm on the register page and we're going to try registering a new account so I'm going to sign up with Yoshi the email is Yoshi net Ninja dodev for the password I'm going to say pw12 3 and then down here PW now I wanted to do this to show you that we are going to get validation errors and we'll see those at the bottom so the first one is that the password isn't 8 characters long and secondly they don't match let's try then pw1 2 3 45 6 which is eight characters pw1 2 3 4 56 and then register hopefully we will get redirected to the homepage now and this will work awesome okay and then just back over here I wanted to open up the database so let's go and have a look at that just so we can see that new user and in fact I created two users one off screen as well so we can see now we have this one Mario and also Yosi awesome and we can see that these passwords are hashed cool
Original Description
🔥🔥 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
More on: Prompt Craft
View skill →Related Reads
📰
📰
📰
📰
What Is a Simple Request and When Does the Browser Send a Preflight Request?
Dev.to · Alireza Hassankhani
How to Prevent Double Bookings in Next.js (Race Conditions & DB Constraints)
Dev.to · Usama Habib
Evaluation Order of Java Operands
Dev.to · Rajesh Bhola
How to learn the basics of c++?
Reddit r/learnprogramming
🎓
Tutor Explanation
DeepCamp AI