// src/components/DirectKeyAuth.tsx 'use client' import { useState, useEffect } from 'react' import { Button } from '@workspace/ui/components/button' import { Wallet } from 'ethers' // Add this to your package.json if not already there export function DirectKeyAuth() { const [sessionStatus, setSessionStatus] = useState<'checking' | 'authenticated' | 'unauthenticated'>('checking') const [sessionData, setSessionData] = useState(null) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) // Check if we already have a session const checkSession = async () => { try { setSessionStatus('checking') const response = await fetch('http://localhost:8000/auth/session', { method: 'GET', credentials: 'include', }) if (response.ok) { const data = await response.json() setSessionStatus('authenticated') setSessionData(data) console.log('Session check successful:', data) } else { setSessionStatus('unauthenticated') setSessionData(null) console.log('Session check failed:', await response.text()) } } catch (error) { console.error('Error checking session:', error) setSessionStatus('unauthenticated') setSessionData(null) } } // Check session on component mount useEffect(() => { checkSession() }, []) // Sign in with private key const signInWithKey = async () => { try { setIsLoading(true) setError(null) // Create wallet from private key const privateKey = '0x23ad64eabeba406086636c621893370c32d8678b5c879195ed4616e842b7aa42'; const wallet = new Wallet(privateKey); // Get the address const address = wallet.address; console.log('Derived address:', address); // Create SIWE message const domain = window.location.host; const origin = window.location.origin; const nonce = Math.random().toString(36).slice(2); const issuedAt = new Date().toISOString(); const message = `${domain} wants you to sign in with your Ethereum account: ${address} Sign in With Ethereum. URI: ${origin} Version: 1 Chain ID: 1 Nonce: ${nonce} Issued At: ${issuedAt}`; console.log('Message to sign:', message); // Sign the message const signature = await wallet.signMessage(message); console.log('Generated signature:', signature); // Send to backend const response = await fetch('http://localhost:8000/auth/validate', { method: 'POST', headers: { 'Content-Type': 'application/json', }, credentials: 'include', body: JSON.stringify({ message, signature }) }); const responseData = await response.text(); console.log('Response data:', responseData); if (response.ok) { console.log('Authentication successful!'); await checkSession(); } else { setError(`Authentication failed: ${responseData}`); } } catch (error) { console.error('Error signing in with key:', error); setError(`Error: ${error instanceof Error ? error.message : String(error)}`); } finally { setIsLoading(false); } }; return (

Direct Key Authentication

This component uses a local private key to sign messages directly, bypassing the wallet UI.

Backend Session

Status: {' '}{sessionStatus}

{sessionData && (
{JSON.stringify(sessionData, null, 2)}
)}
{error && (
{error}
)}
) }