mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Add OpenRouter OAuth
This commit is contained in:
7 files changed
+403
-8
No files matched your search
@@ -16,6 +16,7 @@ import { CreditsBadge } from '@/components/CreditsBadge';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useAppContext } from '@/hooks/useAppContext';
|
||||
import { useOpenRouterOAuth } from '@/hooks/useOpenRouterOAuth';
|
||||
import { SettingsPageLayout } from '@/components/SettingsPageLayout';
|
||||
import type { AIProvider } from '@/contexts/AISettingsContext';
|
||||
import { AI_PROVIDER_PRESETS, type PresetProvider } from '@/lib/aiProviderPresets';
|
||||
@@ -45,6 +46,7 @@ export function AISettings() {
|
||||
const { settings, updateSettings, setProvider, removeProvider, setProviders, isLoading } = useAISettings();
|
||||
const { user } = useCurrentUser();
|
||||
const { config, defaultConfig, updateConfig } = useAppContext();
|
||||
const openRouterOAuth = useOpenRouterOAuth();
|
||||
const [showAdvanced, setShowAdvanced] = useState(false);
|
||||
const [systemPromptInput, setSystemPromptInput] = useState(config.systemPrompt || defaultSystemPrompt);
|
||||
|
||||
@@ -54,6 +56,7 @@ export function AISettings() {
|
||||
const [selectedPreset, setSelectedPreset] = useState<PresetProvider | null>(null);
|
||||
const [addDialogOpen, setAddDialogOpen] = useState(false);
|
||||
const [customProviderDialogOpen, setCustomProviderDialogOpen] = useState(false);
|
||||
const [forceManualEntry, setForceManualEntry] = useState(false);
|
||||
|
||||
// Drag and drop sensors
|
||||
const sensors = useSensors(
|
||||
@@ -256,9 +259,18 @@ export function AISettings() {
|
||||
{selectedPreset && (
|
||||
<AddAIProviderDialog
|
||||
open={addDialogOpen}
|
||||
onOpenChange={setAddDialogOpen}
|
||||
onOpenChange={(open) => {
|
||||
setAddDialogOpen(open);
|
||||
if (!open) {
|
||||
// Reset force manual entry when dialog closes
|
||||
setForceManualEntry(false);
|
||||
}
|
||||
}}
|
||||
preset={selectedPreset}
|
||||
isLoggedIntoNostr={!!user}
|
||||
oauthHook={selectedPreset.id === 'openrouter' ? openRouterOAuth : null}
|
||||
forceManualEntry={forceManualEntry}
|
||||
onSetForceManualEntry={setForceManualEntry}
|
||||
onAdd={(apiKey) => handleAddPresetProvider(selectedPreset, apiKey)}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useSearchParams, useNavigate } from 'react-router-dom';
|
||||
import { useOpenRouterOAuth } from '@/hooks/useOpenRouterOAuth';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { Bot } from 'lucide-react';
|
||||
|
||||
export default function OpenRouterOAuth() {
|
||||
const [searchParams] = useSearchParams();
|
||||
const navigate = useNavigate();
|
||||
const { handleCallback } = useOpenRouterOAuth();
|
||||
const { isLoading: isSettingsLoading } = useAISettings();
|
||||
const { toast } = useToast();
|
||||
|
||||
useEffect(() => {
|
||||
// Wait for AI settings to be initialized
|
||||
if (isSettingsLoading) {
|
||||
return;
|
||||
}
|
||||
|
||||
const code = searchParams.get('code');
|
||||
const state = searchParams.get('state');
|
||||
const error = searchParams.get('error');
|
||||
const errorDescription = searchParams.get('error_description');
|
||||
const storedCodeVerifier = localStorage.getItem('openrouter_oauth_code_verifier');
|
||||
|
||||
// Handle OAuth errors
|
||||
if (error) {
|
||||
toast({
|
||||
title: "OpenRouter OAuth Error",
|
||||
description: errorDescription || error,
|
||||
variant: "destructive",
|
||||
});
|
||||
navigate('/settings/ai');
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle successful OAuth callback
|
||||
if (code && storedCodeVerifier) {
|
||||
handleCallback(code, state || '').then((success) => {
|
||||
if (success) {
|
||||
toast({
|
||||
title: "OpenRouter Connected",
|
||||
description: "Successfully connected to OpenRouter. You can now use OpenRouter models.",
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: "Connection Failed",
|
||||
description: "Failed to connect to OpenRouter. Please try again.",
|
||||
variant: "destructive",
|
||||
});
|
||||
}
|
||||
// Always redirect to AI settings after handling the callback
|
||||
navigate('/settings/ai');
|
||||
});
|
||||
} else if (code && !storedCodeVerifier) {
|
||||
// Missing code verifier - likely an incomplete PKCE flow
|
||||
toast({
|
||||
title: "Authentication Error",
|
||||
description: "OAuth flow is incomplete. Please try connecting to OpenRouter again.",
|
||||
variant: "destructive",
|
||||
});
|
||||
// Clean up any remaining OAuth data
|
||||
localStorage.removeItem('openrouter_oauth_state');
|
||||
localStorage.removeItem('openrouter_oauth_code_verifier');
|
||||
navigate('/settings/ai');
|
||||
} else {
|
||||
// No code parameter - redirect to AI settings
|
||||
navigate('/settings/ai');
|
||||
}
|
||||
}, [searchParams, handleCallback, toast, navigate, isSettingsLoading]);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center p-4">
|
||||
<Card className="w-full max-w-md">
|
||||
<CardContent className="py-12 px-8 text-center">
|
||||
<div className="space-y-6">
|
||||
<div className="flex justify-center">
|
||||
<Bot className="h-12 w-12 text-primary" />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<h1 className="text-xl font-semibold">Connecting to OpenRouter</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Processing your OpenRouter authentication...
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex justify-center">
|
||||
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-primary"></div>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user