save apikey
This commit is contained in:
parent
0c0907ef7e
commit
86de9e8270
|
|
@ -1,12 +1,13 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState, useRef, KeyboardEvent } from 'react';
|
import { useState, useRef, useEffect, KeyboardEvent } from 'react';
|
||||||
import { MinimizedApiKey } from '@/components/demo/MinimizedApiKey';
|
import { MinimizedApiKey } from '@/components/demo/MinimizedApiKey';
|
||||||
import { GenerationTimer } from '@/components/demo/GenerationTimer';
|
import { GenerationTimer } from '@/components/demo/GenerationTimer';
|
||||||
import { ResultCard } from '@/components/demo/ResultCard';
|
import { ResultCard } from '@/components/demo/ResultCard';
|
||||||
import { AdvancedOptionsModal, AdvancedOptionsData } from '@/components/demo/AdvancedOptionsModal';
|
import { AdvancedOptionsModal, AdvancedOptionsData } from '@/components/demo/AdvancedOptionsModal';
|
||||||
|
|
||||||
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000';
|
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000';
|
||||||
|
const API_KEY_STORAGE_KEY = 'banatie_demo_api_key';
|
||||||
|
|
||||||
interface GenerationResult {
|
interface GenerationResult {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
@ -76,6 +77,56 @@ export default function DemoTTIPage() {
|
||||||
|
|
||||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||||
|
|
||||||
|
// Load API key from localStorage on mount
|
||||||
|
useEffect(() => {
|
||||||
|
const storedApiKey = localStorage.getItem(API_KEY_STORAGE_KEY);
|
||||||
|
if (storedApiKey) {
|
||||||
|
setApiKey(storedApiKey);
|
||||||
|
// Auto-validate the stored key
|
||||||
|
validateStoredApiKey(storedApiKey);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Validate stored API key (without user interaction)
|
||||||
|
const validateStoredApiKey = async (keyToValidate: string) => {
|
||||||
|
setValidatingKey(true);
|
||||||
|
setApiKeyError('');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE_URL}/api/info`, {
|
||||||
|
headers: {
|
||||||
|
'X-API-Key': keyToValidate,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
setApiKeyValidated(true);
|
||||||
|
if (data.keyInfo) {
|
||||||
|
setApiKeyInfo({
|
||||||
|
organizationSlug: data.keyInfo.organizationSlug || data.keyInfo.organizationId,
|
||||||
|
projectSlug: data.keyInfo.projectSlug || data.keyInfo.projectId,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setApiKeyInfo({
|
||||||
|
organizationSlug: 'Unknown',
|
||||||
|
projectSlug: 'Unknown',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Stored key is invalid, clear it
|
||||||
|
localStorage.removeItem(API_KEY_STORAGE_KEY);
|
||||||
|
setApiKeyError('Stored API key is invalid or expired');
|
||||||
|
setApiKeyValidated(false);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
setApiKeyError('Failed to validate stored API key');
|
||||||
|
setApiKeyValidated(false);
|
||||||
|
} finally {
|
||||||
|
setValidatingKey(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Validate API Key
|
// Validate API Key
|
||||||
const validateApiKey = async () => {
|
const validateApiKey = async () => {
|
||||||
if (!apiKey.trim()) {
|
if (!apiKey.trim()) {
|
||||||
|
|
@ -97,6 +148,10 @@ export default function DemoTTIPage() {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setApiKeyValidated(true);
|
setApiKeyValidated(true);
|
||||||
|
|
||||||
|
// Save to localStorage on successful validation
|
||||||
|
localStorage.setItem(API_KEY_STORAGE_KEY, apiKey);
|
||||||
|
|
||||||
// Extract org/project info from API response
|
// Extract org/project info from API response
|
||||||
if (data.keyInfo) {
|
if (data.keyInfo) {
|
||||||
setApiKeyInfo({
|
setApiKeyInfo({
|
||||||
|
|
@ -124,6 +179,10 @@ export default function DemoTTIPage() {
|
||||||
|
|
||||||
// Revoke API Key
|
// Revoke API Key
|
||||||
const revokeApiKey = () => {
|
const revokeApiKey = () => {
|
||||||
|
// Clear localStorage
|
||||||
|
localStorage.removeItem(API_KEY_STORAGE_KEY);
|
||||||
|
|
||||||
|
// Clear state
|
||||||
setApiKey('');
|
setApiKey('');
|
||||||
setApiKeyValidated(false);
|
setApiKeyValidated(false);
|
||||||
setApiKeyInfo(null);
|
setApiKeyInfo(null);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue