useCurrentUser()
useCurrentUser()
is a dedicated composable for accessing the current authenticated user. It is handy when you need to access user data across various components without needing the full functionality of useSanctum()
.
Features
Typed User Support:
The composable supports generic types, allowing you to define the shape of the user object as needed. This is particularly beneficial in TypeScript projects where you want strong typing for user data.Example:
javascriptinterface User { id: number; email: string; name: string; } const user = useCurrentUser<User>();
Reactive User Data:
Theuser
property is reactive, meaning that any changes to the authenticated user will automatically update whereveruseCurrentUser()
is used in your application.Returns
null
if Unauthenticated:
If no user is authenticated,useCurrentUser()
returnsnull
, making it easy to conditionally render content based on the user's authentication status.