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.

Valoramos su privacidad

Este sitio web utiliza cookies para análisis anónimos que nos ayudan a mejorar su experiencia. No se almacena ni comparte información personal. Puede permitir o rechazar el seguimiento analítico en cualquier momento. Consulte nuestra Política de Privacidad.

Usamos cookies para análisis anónimos. No se almacena información personal. Consulte nuestra Política de Privacidad.