This episode is for members only

Sign up to access "Nuxt 3 Authentication with Laravel Sanctum" right now.

Get started
Already a member? Sign in to continue
Playing
09. Fetching the authenticated user

Transcript

00:00
From any component in our application, we can continue to use the useSanctumAuth composable that we saw earlier to log in to actually fetch user details and also check if the user is authenticated. A good place to start would be our app navigation.
00:14
So let's go ahead and pull this in first of all. Let's import isAuthenticated from that useSanctumAuth composable that we saw earlier. Let's get rid of that import once again. So this is now going to contain Boolean of whether we are authenticated or not.
00:33
Let's just dump this out on the page and have a look. And you can see that we get true. Back over to our application. Let's get rid of both of these just to make sure that this is working.
00:43
And when we refresh, we get false gray. Okay. So we'll go back over to auth and login. In fact, let's go ahead and add in a sign in link just in here, just
00:52
to make this a little bit easier. And let's go ahead and hit that. Okay. So when I sign in now, this will immediately change to true.
01:01
So this will react to whether the user is authenticated or not. We can use this to adjust our template. So for example, we could say that if the user is not authenticated, then we want to show the sign in and register links.
01:14
Otherwise we want to show an entirely different thing. So let's switch this over to show a different set of data if we are authenticated. For now, let's go ahead and create out a font of semi bold in here and let's output the user information.
01:29
Where do we get that? Well, we get it directly from the use sanctum auth composable. So in here, I can just now output user dot and then choose any of the data that we send down from our API.
01:42
A little bit later, we're going to create an interface for this to make it a little bit easier in terms of TypeScript, but let's just go ahead and output the user's name. Okay. If we come over, sure enough, you can see Alex is authenticated.
17 episodes1 hr 1 min

Overview

Learn how to build a full authentication boilerplate with Nuxt, Sanctum and Fortify.

This course covers everything you need to get started with authentication with Nuxt and Laravel. We’ll start by setting up a Nuxt project and configuring Laravel Sanctum, then add the ability to register, sign in and sign out.

We’ll also cover some TypeScript tips, protecting pages with middleware, and using Vue composables to keep our code tidy.

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

Comments

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