-
Notifications
You must be signed in to change notification settings - Fork 54
Open
Description
Problem
Currently, Fireproof's authentication strategies require either:
- SimpleTokenStrategy - Pre-configured tokens (no user auth flow)
- RedirectStrategy - Popup windows that can be blocked and redirect to external dashboard
- IframeStrategy - Incomplete implementation with broken
waitForToken()
This creates friction for React applications that want to:
- Integrate authentication naturally into their existing UI
- Use their preferred auth providers (Clerk, Auth0, Firebase, etc.)
- Avoid popup blockers and external redirects
- Maintain consistent UX within their application
Proposed Solution
Create an InPageReactStrategy that allows consumers to provide their own React authentication component while maintaining the TokenStrategie interface.
Design Overview
export class InPageReactStrategy implements TokenStrategie {
constructor(AuthComponent: React.ComponentType<AuthComponentProps>) {
this.authComponent = AuthComponent;
}
open() {
// Render consumer's auth component into DOM
ReactDOM.render(createElement(this.authComponent, {
deviceId,
onToken: (tokenAndClaims) => this.handleToken(tokenAndClaims),
onClose: () => this.cleanup()
}), containerElement);
}
// ... implement other TokenStrategie methods
}Consumer Usage
// Consumer provides their own auth component
function MyAuthComponent({ deviceId, onToken, onClose }: AuthComponentProps) {
const { getToken } = useAuth(); // Clerk, Auth0, etc.
const handleAuth = async () => {
const providerToken = await getToken();
// Exchange via consumer's backend
const response = await fetch('/api/fireproof-token', {
headers: { Authorization: `Bearer ${providerToken}` }
});
const { token } = await response.json();
onToken({ token, claims: decodeJwt(token) });
};
return (
<div className="my-auth-modal">
<h2>Sign in to continue</h2>
<button onClick={handleAuth}>Authenticate</button>
</div>
);
}
// Use with Fireproof
const strategy = new InPageReactStrategy(MyAuthComponent);
const { database } = useFireproof("mydb", { strategy });Benefits
- Framework Native - Works naturally with React patterns
- Provider Agnostic - Consumer chooses any auth provider
- No Popup Blockers - Renders inline components
- Consistent UX - Matches application's design system
- Clean Separation - Fireproof handles token interface, consumer handles auth provider
- Backend Token Exchange - Secure server-side token exchange pattern
Implementation Details
Interface Definition
interface AuthComponentProps {
deviceId: string;
onToken: (token: TokenAndClaims) => void;
onClose: () => void;
ledger?: string;
tenant?: string;
}Strategy Implementation
open()- Render React component into DOM containertryToken()- Return cached token if availablewaitForToken()- Return Promise that resolves when component callsonTokenstop()- Cleanup DOM and unmount component
Token Exchange Flow
- Consumer's React component handles auth provider integration
- Component gets provider token (Clerk, Auth0, etc.)
- Component calls consumer's API endpoint with provider token
- Backend verifies provider token and exchanges for Fireproof token
- Component calls
onToken()with Fireproof token - Strategy resolves
waitForToken()Promise
Migration Path
This approach moves auth provider integration (like Clerk) from the Fireproof dashboard to the consumer side, providing:
- Better separation of concerns
- More flexibility for consumers
- Reduced complexity in Fireproof core
- Provider-agnostic authentication strategy
Files to Modify
use-fireproof/in-page-react-strategy.ts- New strategy implementationuse-fireproof/index.ts- Export new strategy- Documentation and examples
Related
This addresses authentication UX issues while maintaining the existing TokenStrategie interface established in core/types/protocols/cloud/gateway-control.ts:19.
Metadata
Metadata
Assignees
Labels
No labels