fix oauth

This commit is contained in:
2025-06-17 13:43:34 +09:00
parent fcab7c7f83
commit c1d3678d96
2 changed files with 109 additions and 61 deletions

View File

@@ -1215,6 +1215,13 @@ function App() {
const contentText = isNewFormat ? value.text : (value.content || value.body || ''); const contentText = isNewFormat ? value.text : (value.content || value.body || '');
// Use the author from the record if available, otherwise fall back to AI profile // Use the author from the record if available, otherwise fall back to AI profile
const authorInfo = value.author || aiProfile; const authorInfo = value.author || aiProfile;
// Debug log for author information
if (value.author) {
console.log('[Debug] Using record author:', value.author);
} else {
console.log('[Debug] Using AI profile fallback:', aiProfile);
}
const postInfo = isNewFormat ? value.post : null; const postInfo = isNewFormat ? value.post : null;
const contentType = value.type || 'unknown'; const contentType = value.type || 'unknown';
const createdAt = value.createdAt || value.generated_at || ''; const createdAt = value.createdAt || value.generated_at || '';
@@ -1313,27 +1320,51 @@ function App() {
<section className="comment-section"> <section className="comment-section">
{/* Authentication Section */} {/* Authentication Section */}
{!user ? ( {!user ? (
<div className="auth-section search-bar-layout"> <div className="auth-section">
<input <div className="search-bar-layout">
type="text" <input
id="handle-input" type="text"
name="handle" id="handle-input"
placeholder="user.bsky.social" name="handle"
className="handle-input" placeholder="user.bsky.social"
value={handleInput} className="handle-input"
onChange={(e) => setHandleInput(e.target.value)} value={handleInput}
onKeyDown={(e) => { onChange={(e) => setHandleInput(e.target.value)}
if (e.key === 'Enter') { onKeyDown={(e) => {
e.preventDefault(); if (e.key === 'Enter') {
executeOAuth(); e.preventDefault();
} executeOAuth();
}} }
/> }}
/>
<button
onClick={executeOAuth}
className="atproto-button"
>
<i class="fab fa-bluesky"></i>
</button>
</div>
<button <button
onClick={executeOAuth} onClick={async () => {
className="atproto-button" console.log('Force clearing all OAuth sessions...');
await atprotoOAuthService.logout();
localStorage.clear();
sessionStorage.clear();
// Force reset session check
await atprotoOAuthService.checkSession(true);
window.location.reload();
}}
style={{
padding: '5px 10px',
fontSize: '12px',
backgroundColor: '#ff6b6b',
color: 'white',
border: 'none',
borderRadius: '3px',
marginTop: '5px'
}}
> >
<i class="fab fa-bluesky"></i> Clear All Sessions
</button> </button>
</div> </div>
) : ( ) : (

View File

@@ -95,26 +95,25 @@ class AtprotoOAuthService {
} }
private async processSession(session: any): Promise<{ did: string; handle: string }> { private async processSession(session: any): Promise<{ did: string; handle: string }> {
console.log('[OAuth Debug] Processing session object:', session);
// Log full session structure // Log session structure
console.log('[OAuth Debug] Session keys:', Object.keys(session));
console.log('[OAuth Debug] Session.sub:', session.sub);
console.log('[OAuth Debug] Session.did:', session.did);
console.log('[OAuth Debug] Session.handle:', session.handle);
// Check if agent has properties we can access // Check if agent has properties we can access
if (session.agent) { if (session.agent) {
console.log('[OAuth Debug] Session has agent property');
} }
const did = session.sub || session.did; const did = session.sub || session.did;
let handle = session.handle || 'unknown'; let handle = session.handle || 'unknown';
console.log('[OAuth Debug] Extracted DID:', did);
console.log('[OAuth Debug] Extracted handle:', handle);
// Create Agent directly with session (per official docs) // Create Agent directly with session (per official docs)
try { try {
this.agent = new Agent(session); this.agent = new Agent(session);
@@ -254,10 +253,10 @@ class AtprotoOAuthService {
async initiateOAuthFlow(handle?: string): Promise<void> { async initiateOAuthFlow(handle?: string): Promise<void> {
try { try {
console.log('[OAuth Debug] Starting OAuth flow...');
if (!this.oauthClient) { if (!this.oauthClient) {
console.log('[OAuth Debug] No OAuth client, initializing...');
await this.initialize(); await this.initialize();
} }
@@ -273,33 +272,32 @@ class AtprotoOAuthService {
} }
} }
console.log('[OAuth Debug] Input handle:', handle);
// Detect PDS based on handle // Detect PDS based on handle
const pdsUrl = await this.detectPDSFromHandle(handle); const pdsUrl = await this.detectPDSFromHandle(handle);
console.log('[OAuth Debug] Detected PDS for handle', handle, ':', pdsUrl); console.log('[OAuth Debug] Detected PDS for handle', handle, ':', pdsUrl);
// Always re-initialize OAuth client with detected PDS // Re-initialize OAuth client with correct PDS if needed
console.log('[OAuth Debug] Re-initializing OAuth client'); if (pdsUrl !== 'https://bsky.social') {
console.log('[OAuth Debug] Re-initializing OAuth client for non-Bluesky PDS');
// Clear existing client to force fresh initialization
this.oauthClient = null; // Determine PLC directory for syu.is only
this.initializePromise = null; const plcDirectoryUrl = handle.endsWith('.syu.is') || handle.endsWith('.syui.ai') ? 'https://plc.syu.is' : 'https://plc.directory';
console.log('[OAuth Debug] Using PLC directory:', plcDirectoryUrl);
// Determine PLC directory based on input handle, not environment PDS
let plcDirectoryUrl = 'https://plc.directory'; // Default to Bluesky PLC this.oauthClient = await BrowserOAuthClient.load({
if (handle.endsWith('.syu.is') || handle.endsWith('.syui.ai')) { clientId: this.getClientId(),
plcDirectoryUrl = 'https://plc.syu.is'; handleResolver: pdsUrl,
plcDirectoryUrl: plcDirectoryUrl,
});
console.log('[OAuth Debug] OAuth client re-initialized for', pdsUrl);
} else {
console.log('[OAuth Debug] Using default OAuth client for Bluesky');
} }
console.log('[OAuth Debug] Using PLC directory:', plcDirectoryUrl);
this.oauthClient = await BrowserOAuthClient.load({
clientId: this.getClientId(),
handleResolver: pdsUrl,
plcDirectoryUrl: plcDirectoryUrl,
});
console.log('[OAuth Debug] OAuth client re-initialized successfully');
// OAuth client initialized // OAuth client initialized
@@ -376,9 +374,14 @@ class AtprotoOAuthService {
} }
} }
async checkSession(): Promise<{ did: string; handle: string } | null> { async checkSession(forceReset: boolean = false): Promise<{ did: string; handle: string } | null> {
try { try {
console.log('[OAuth Debug] Checking session...'); console.log('[OAuth Debug] Checking session... (forceReset:', forceReset, ')');
if (forceReset) {
console.log('[OAuth Debug] Force reset requested - clearing all sessions');
await this.logout();
}
if (!this.oauthClient) { if (!this.oauthClient) {
console.log('[OAuth Debug] No OAuth client, initializing...'); console.log('[OAuth Debug] No OAuth client, initializing...');
@@ -460,28 +463,27 @@ class AtprotoOAuthService {
async logout(): Promise<void> { async logout(): Promise<void> {
try { try {
console.log('[OAuth Debug] Starting logout process...');
// Clear Agent // Clear Agent
this.agent = null; this.agent = null;
// Clear BrowserOAuthClient session // Clear BrowserOAuthClient session
if (this.oauthClient) { if (this.oauthClient) {
console.log('[OAuth Debug] Clearing OAuth client session...');
try { try {
// BrowserOAuthClient may have a revoke or signOut method // BrowserOAuthClient may have a revoke or signOut method
if (typeof (this.oauthClient as any).signOut === 'function') { if (typeof (this.oauthClient as any).signOut === 'function') {
await (this.oauthClient as any).signOut(); await (this.oauthClient as any).signOut();
console.log('[OAuth Debug] OAuth client signOut completed');
} else if (typeof (this.oauthClient as any).revoke === 'function') { } else if (typeof (this.oauthClient as any).revoke === 'function') {
await (this.oauthClient as any).revoke(); await (this.oauthClient as any).revoke();
console.log('[OAuth Debug] OAuth client revoke completed');
} else { } else {
console.log('[OAuth Debug] No signOut/revoke method available');
} }
} catch (oauthError) { } catch (oauthError) {
console.log('[OAuth Debug] OAuth logout error:', oauthError);
} }
// Reset the OAuth client to force re-initialization // Reset the OAuth client to force re-initialization
@@ -491,6 +493,21 @@ class AtprotoOAuthService {
// Clear any stored session data // Clear any stored session data
localStorage.removeItem('atproto_session'); localStorage.removeItem('atproto_session');
// Clear all OAuth-related storage
console.log('[OAuth Debug] Clearing all OAuth storage...');
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && (key.includes('oauth') || key.includes('atproto') || key.includes('session'))) {
console.log('[OAuth Debug] Removing localStorage key:', key);
localStorage.removeItem(key);
}
}
// Clear internal session info
(this as any)._sessionInfo = null;
console.log('[OAuth Debug] Logout completed');
sessionStorage.clear(); sessionStorage.clear();
// Clear all localStorage items that might be related to OAuth // Clear all localStorage items that might be related to OAuth