Skip to content

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:

    javascript
    interface User {
      id: number;
      email: string;
      name: string;
    }
    
    const user = useCurrentUser<User>();
  • Reactive User Data:
    The user property is reactive, meaning that any changes to the authenticated user will automatically update wherever useCurrentUser() is used in your application.

  • Returns null if Unauthenticated:
    If no user is authenticated, useCurrentUser() returns null, making it easy to conditionally render content based on the user's authentication status.