@proveanything/smartlinks-auth-ui
Version:
Lightweight React authentication UI components with bearer token support and Smartlinks SDK integration
21 lines (20 loc) • 797 B
JavaScript
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import React from 'react';
import { useAuth } from '../context/AuthContext';
export const ProtectedRoute = ({ children, fallback, redirectTo, }) => {
const { isAuthenticated, isLoading } = useAuth();
// Show loading state
if (isLoading) {
return _jsx("div", { children: "Loading..." });
}
// If not authenticated, redirect or show fallback
if (!isAuthenticated) {
if (redirectTo) {
window.location.href = redirectTo;
return null;
}
return fallback ? _jsx(_Fragment, { children: fallback }) : _jsx("div", { children: "Access denied. Please log in." });
}
// Render protected content
return _jsx(_Fragment, { children: children });
};