This episode is for members only

Sign up to access "Passwordless Authentication with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
07. Registering users

Transcript

00:00
You have a couple of options when it comes to registering users. You can either go through the standard registration flow, allowing the user to choose a password and all of that stuff, or you could just allow the users to register with their email address, name
00:12
and any other details you need and then send them a magic login link. Now, we're going to do that just to see the process. But of course, in your app, it's going to be completely different. You might want to change this around and that should be pretty easy to do.
00:25
OK, so we're going to start off with creating out a register template so we can get started on this. We're pretty much going to copy the login template and just create out a register.blade.php file over in here
00:38
and then just modify this. So let's hook this up to a route really quickly. It's going to be very similar to the login route just here. So let's go ahead and say auth register and switch the template over as well.
00:50
So over in the register template, let's have a look at this in the browser real quick. We probably signed it. Yeah, we're signed in. So let's just delete the session here and let's head over to auth and register.
01:03
OK, great. So that, of course, needs to change. So we're going to go ahead and say register and we do need the email address. We also need the user's name as well.
01:16
So let's say name. We will switch the type to text, the name and email to name, and we can get rid of the placeholder or change that. We keep required, but we'll get autofocus because we have
01:31
that focused up here and we'll just switch that to name as well. We should be good. We didn't actually fill that in. Or that in before, so let's do that now.
01:41
OK, so let's go ahead and change the button here to register. And we've got our success here, we should be good. So there we go. That's how we're going to create an account.
01:52
Now, since we've got the Laravel actions package already installed, we may as well create an action which creates a user, registers a user. And then in the next episode, we'll look at how we can use actions within actions to reuse
02:05
that magic login link action that we already created. So let's go ahead and create out an action here called create user. And we're going to do exactly the same thing as we did before, we're going to put everything we need to actually get this to work inside of
02:20
handle, which, of course, is going to be just to create the user in the database, but then we can add our rules in here as well. And we can also implement out as controller. So in here, let's just die dump create user
02:33
and let's bring in that as controller method and bring in. The action request that we saw before, and then that's pretty much it. So we're just going to say this handle and how are we going to get that data? Well, it's just going to be the email address and the name from the request.
02:54
So request email and request name, and then we can accept these in here. So string email, string name. And that's what we're going to use to create the user in the database. So with this, we can return back and we can redirect with a message.
03:11
So with success and let's say registered, check your email address for a login link, whatever you want to say. OK, so let's just check that this works. We obviously need to hook this up to the correct form.
03:28
So over in Roots and Web, let's just again grab this, put that down there, auth register. We're posting through to that route. This is now create user and that's now auth register.
03:41
And of course, we need to be a guest as well. So if we just head back over to the register template, let's hook that up to register and we should be good. Let's try it out.
03:53
So I'm going to go ahead and register with a new email address and not a password, the name, hit register. And there we go. Great. So next step, of course,
04:04
is just going to be implementing some validation rules so we can do pretty much the same thing as we did over on the send magic link action. Let's just grab these just save a bit of time and put these over in here. And let's put in our rule because we are going to need that.
04:20
This time, though, the rule is that this is unique to the users table. We don't want the same email address to be used. And we can just add some rules for the name as well. So let's just say required and we'll say max two five five.
04:35
And of course, you can implement any other rules you need there as well. So now we should see this working nicely. If we just say Mabel at CodeCourse.com, let's go ahead. And we've got some front end validation.
04:48
So let's just roughly test this out. We are going to be adding tests, but let's get rid of the required field here. So let's go ahead and hit register after we enter that email address.
05:00
And yeah, OK, we just need to put in that namespace again. That's fine. There we go, and we'll just resubmit this and then the name field is required. So we've got the rough validation rules in there working and we know that's good.
05:14
So now we need to do is actually create the user out. Let's just bring back that required first. And now let's create the user. This is, of course, as easy as just creating out a user in here.
05:28
With the email address. And with the name. Now, the only issue we've got at the moment is by default, Laravel's migrations come with the password in here.
05:40
Like I said, depending on how you want this to work, you may wish to just get rid of this altogether, which I wouldn't recommend, or go ahead and modify the flow of this based on what you've learned. So just for now, what I'm going to do is head over to the structure here.
05:54
I'm going to get rid of the password field. So let's get rid of that. And then when we first install Laravel, we have a create users table. I'm going to go ahead and get rid of the password from that as well.
06:06
But of course, feel free to leave that in and customize the whole flow here to how you need it. Now, within the handle method, it's always good practice to return. If you're creating anything and then if we do use this anywhere else,
06:18
for example, in an admin panel to create users out, we can get that data back to do something with it. So we're just going to return that in there. So now that we're done with this,
06:27
we've got our handle method, which does one job, creates the user, our controller handles that, but of course, validates as well and redirects back. Let's see if this works. So we're going to go ahead and enter in our email address and our name.
06:42
Hit register. And there we go. Register. Check your email address for a login link. Now, we aren't sending the email at the moment,
06:50
but we do have the user in there, which is a good sign. I'm going to go ahead and delete that for now, because in the next episode, we're going to look at how we can use the previous action that we created for logging in inside of here so we don't duplicate any code.
10 episodes 58 mins

Overview

Say goodbye to the traditional email/password flow and implement passwordless authentication with Laravel! In this course, we'll cover sending a secure link via email to allow users to sign in seamlessly. Oh, and we'll cover the entire registration process too.

Use it on its own, or combine it with the standard email/password flow to give your users even more flexibility.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.