Using Fortify as API0:00
So far, we've been using Fortify as a traditional server-rendered app, where we return the Vue as the response. In this video, we'll explore how to use it as an API for things like single-page apps or mobile apps. So if you're just building an API, you can turn off the Vues entirely in the Fortify config. So before I turn that off, let me just show you the routes here. So let me just do php artisan route:list. And you can see a bunch of GET requests, which correspond to the Vues that we've been working with. Now, if I turn this off and switch it to false and run that again, you'll see that most of the GET requests are gone. Some of them still remain because those are required. So yeah, if you're building a single-page app where the Vues live, then you no longer need the Vues in Laravel. So just turn them off. But I'm just gonna leave them on here. Now, Fortify actually supports API requests. So let's take a look at an
How JSON responses work0:48
Vues live, then you no longer need the Vues in Laravel. So just turn them off. But I'm just gonna leave them on here. Now, Fortify actually supports API requests. So let's take a look at an example. So let me look for the register route here. So grep register. Okay. And let's look at the post register route. So that would be in Laravel Fortify RegisteredUserController store. So let's go to the vendor folder Fortify Controllers RegisteredUserController store. And as you see, it's returning this RegisterResponse class, which in this case, it's an interface. So if I go into it, you'll see it's an interface. But you can actually see a class that implements that interface down here in the responses folder. So here we go. And we have this toResponse method. And you can see that Laravel checks each request. And if it wants JSON, then it responds with JSON. So this is the case when you're using it as an API. But if not,
to response method. And you can see that Laravel checks each request. And if it wants JSON, then it responds with JSON. So this is the case when you're using it as an API. But if not, just respond the traditional way. And in this case, it just redirects to the logged in homepage. Now the other auth features work the same way where it checks for JSON. So let's look at another one. For example, this one. Okay, maybe not this one, but Oh, sorry, these ones on here. So any one of these ones will work the same way checks of JSON. And if it's not just respond the same way, or the traditional way. So in theory, we can actually make API requests to these endpoints and it should work. So let's go ahead and try with the register route. So before I do that, let me just turn off the feature that verifies email. So let me just comment this out. And we also have to change the User model and remove mustVerifyEmail. So okay. Now let's go into our rest
Testing API registration2:30
let me just turn off the feature that verifies email. So let me just comment this out. And we also have to change the User model and remove mustVerifyEmail. So okay. Now let's go into our REST client. And let's make a new endpoint or new request for this register round, let's call it register. And it's a POST request. Okay. And let's create this. The endpoint is /register. Okay, register. And let's make sure to request JSON. So accept application/json. Okay. And let's set a body here, we have to pass in the fields required to register. So name. Okay, let's try this out. And we get a CSRF token mismatch. So we're getting this error because this route is within the web middleware. And there is a CSRF check for that middleware. So let's go ahead and disable that for now. So verify CSRF token. Let's put the register route in here. Okay, that should be a
Handling CSRF for APIs3:37
middleware. And there is a CSRF check for that middleware. So let's go ahead and disable that for now. So verify CSRF token. Let's put the register route in here. Okay, that should be a string. And now this should work. So if I hit this endpoint, there we go, it's created. And just to make sure let's check our database. And there it is. Now, if you're just using Laravel as an API, then it's fine to disable the CSRF checks on these routes. But if you're still using it for traditional server rendered views, then it's probably not a good idea to bypass the CSRF. So one option is to have separate routes for your API's. So let me show you that. So I'm going to put this back. So now we should get that CSRF mismatch again. But now I want to define new routes for the endpoints we want an API for. So let me show you what I mean. So let's look for the routes within Fortify. So again, back to the Fortify folder. And there is
Moving routes to api.php4:30
new routes for the endpoints we want an API for. So let me show you what I mean. So let's look for the routes within Fortify. So again, back to the Fortify folder. And there is a folder for routes and a file for routes. And here is the endpoint for register. So I'm going to grab this. And let's go into our routes/api.php file. And I'm going to put it in here instead. And we can make use of this one for any API calls. So let's grab that import this. And now instead of hitting this endpoint, because this endpoint is now only for the traditional web views, we can hit the api/register endpoint. And this should work. So let me just make another User here. Okay. And this hopefully should work. And it does check our database. And there's the new User. So in the next video, we'll take a look at logging in with single page apps, because that requires the sanctum package.
new User. So in the next video, we'll take a look at logging in with single page apps, because that requires the sanctum package.
