RegisterLogin
DocsPricing
RegisterLogin
  • Getting Started
  • Introduction
  • Quick Start
  • SDKs
  • React
  • TypeScript
  • Next.js
  • Express
  • NestJS
  • Python
  • API Reference
  • Support and Resources
  • FAQ
  • Contact

useAuth

The useAuth hook provides access to the current authentication state including the user object and loading status.


Basic Usage

import { useAuth } from 'authsafe-react';

function UserProfile() {
  const { user, isAuthenticated, isLoading } = useAuth();

  if (isLoading) return <div>Loading...</div>;
  if (!isAuthenticated) return <div>Please log in</div>;

  return (
    <div>
      <h2>Welcome, {user?.name}!</h2>
      <p>Email: {user?.email}</p>
    </div>
  );
}

Return Values

PropertyTypeDescription
userUser | nullCurrent user object (null if not authenticated)
isAuthenticatedbooleanWhether the user is currently authenticated
isLoadingbooleanWhether authentication state is being loaded
errorError | nullAuthentication error if any

User Object Properties

PropertyTypeDescription
substringUnique user identifier
namestringUser's display name
emailstringUser's email address
picturestring | undefinedURL to user's profile picture
email_verifiedboolean | undefinedWhether email has been verified

Protecting Routes

Use the hook to conditionally render content based on authentication status:

import { useAuth } from 'authsafe-react';
import { Navigate } from 'react-router-dom';

function ProtectedRoute({ children }) {
  const { isAuthenticated, isLoading } = useAuth();

  if (isLoading) return <div>Loading...</div>;
  if (!isAuthenticated) return <Navigate to="/login" />;

  return children;
}

Type Definitions

export interface User {
  sub: string;
  name?: string;
  email?: string;
  picture?: string;
  email_verified?: boolean;
  [key: string]: any;
}

export interface IUseAuth {
  user: User | null;
  isAuthenticated: boolean;
  isLoading: boolean;
  error: Error | null;
}

Best Practices
  • Always check isLoading before rendering authentication-dependent UI
  • Use isAuthenticated to conditionally render protected content
  • Handle error states gracefully with fallback UI
  • Access user properties safely with optional chaining (user?.email)
  • Combine with route guards for complete protection

AuthSafe

Product

HighlightFeatureIntegrationPricingFAQ

Company

AboutBlogContact

Developer

DashboardDocumentation

Legal

Terms & ConditionsPrivacyComplianceShippingCancellationAI

© 2026 AuthSafe. All rights reserved.

We value your privacy

This website uses cookies for anonymous analytics to help us improve your experience. No personal information is stored or shared. You can allow or reject analytics tracking at any time. See our Privacy Policy.

We use cookies for anonymous analytics. No personal info is stored. See our Privacy Policy.