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

AuthSafe

Product

HighlightFeatureIntegrationPricingFAQ

Company

AboutBlogContactSitemap

Developer

DashboardDocumentation

Legal

Terms & ConditionsPrivacyComplianceShippingCancellation

© 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.

useAuthCallback()

A React hook that handles the OAuth callback flow after authentication. This hook processes the authorization code, exchanges it for tokens, and updates the authentication state.

Basic Usage

import { useAuthCallback } from 'authsafe-react';

function CallbackPage() {
  const { isLoading, error } = useAuthCallback();

  if (isLoading) {
    return <div>Processing login...</div>;
  }

  if (error) {
    return <div>Error: {error.message}</div>;
  }

  return <div>Login successful! Redirecting...</div>;
}

Return Values

PropertyTypeDescription
isLoadingbooleanTrue while processing the OAuth callback
errorError | nullAny error that occurred during the callback process
isSuccessbooleanTrue when authentication is successfully completed

With Post-Authentication Redirect

import { useAuthCallback } from 'authsafe-react';
import { useNavigate } from 'react-router-dom';
import { useEffect } from 'react';

function CallbackPage() {
  const { isLoading, error, isSuccess } = useAuthCallback();
  const navigate = useNavigate();

  useEffect(() => {
    if (isSuccess) {
      // Redirect to dashboard after successful authentication
      navigate('/dashboard');
    }
  }, [isSuccess, navigate]);

  if (isLoading) {
    return (
      <div>
        <h2>Completing login...</h2>
        <p>Please wait while we verify your credentials.</p>
      </div>
    );
  }

  if (error) {
    return (
      <div>
        <h2>Authentication Failed</h2>
        <p>Error: {error.message}</p>
        <button onClick={() => navigate('/login')}>
          Try Again
        </button>
      </div>
    );
  }

  return <div>Success! Redirecting to dashboard...</div>;
}

Route Setup

Create a dedicated callback route in your application:
// React Router setup
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import CallbackPage from './pages/CallbackPage';
import Dashboard from './pages/Dashboard';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/callback" element={<CallbackPage />} />
        <Route path="/dashboard" element={<Dashboard />} />
        {/* other routes */}
      </Routes>
    </BrowserRouter>
  );
}

Type Definition

interface useAuthCallbackReturn {
  isLoading: boolean;
  error: Error | null;
  isSuccess: boolean;
}

How It Works

OAuth Callback Flow
  1. User is redirected back to your app with an authorization code
  2. The hook extracts the code from the URL parameters
  3. The code is exchanged for access and ID tokens
  4. Tokens are securely stored
  5. User information is fetched and cached
  6. Authentication state is updated

Important
Make sure the callback route matches the redirectUri configured in your AuthProvider.
Best Practices
  • Always show a loading state while processing the callback
  • Provide clear error messages and recovery options
  • Redirect users to an appropriate page after successful authentication
  • Ensure your callback route is publicly accessible (no auth required)
  • Handle errors gracefully with retry options