In this episode, we focus on making the authenticated user's info available to the entire app, so we can show things like their name in the navigation when they're signed in.
We start by looking at the middleware where we can share data with all pages, and decide to make the current user globally available. We whip up an auth
wrapper and grab the current user from the request. Then we test things out in the navigation to make sure the user's details show up when they're signed in.
But we run into the issue that we're exposing a little too much info about the user to the client. So, we fix this by making a User API Resource in Laravel, which lets us customize exactly what data we share (like just the id, name, and email for now).
To make things tidier, we globally remove the default 'data' wrapper from our resources. After that, we update our navigation so menu items change based on the user's authentication state—for example, showing "log out" only if someone is signed in.
We don't have sign out functionality yet, so we play with the browser cookies to test logged-out states. Finally, we test by creating a new account and confirm that the navigation correctly reflects who is signed in, and display their name dynamically.
With this all set up, we're in a great place to move on to login, logout, and wrapping up authentication in the next episode!