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,7 +1320,8 @@ 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">
<div className="search-bar-layout">
<input <input
type="text" type="text"
id="handle-input" id="handle-input"
@@ -1336,6 +1344,29 @@ function App() {
<i class="fab fa-bluesky"></i> <i class="fab fa-bluesky"></i>
</button> </button>
</div> </div>
<button
onClick={async () => {
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'
}}
>
Clear All Sessions
</button>
</div>
) : ( ) : (
<div className="user-section"> <div className="user-section">
<div className="user-info"> <div className="user-info">

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 session structure
// Log full 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,24 +272,20 @@ 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 // Determine PLC directory for syu.is only
this.oauthClient = null; const plcDirectoryUrl = handle.endsWith('.syu.is') || handle.endsWith('.syui.ai') ? 'https://plc.syu.is' : 'https://plc.directory';
this.initializePromise = null;
// Determine PLC directory based on input handle, not environment PDS
let plcDirectoryUrl = 'https://plc.directory'; // Default to Bluesky PLC
if (handle.endsWith('.syu.is') || handle.endsWith('.syui.ai')) {
plcDirectoryUrl = 'https://plc.syu.is';
}
console.log('[OAuth Debug] Using PLC directory:', plcDirectoryUrl); console.log('[OAuth Debug] Using PLC directory:', plcDirectoryUrl);
this.oauthClient = await BrowserOAuthClient.load({ this.oauthClient = await BrowserOAuthClient.load({
@@ -299,7 +294,10 @@ class AtprotoOAuthService {
plcDirectoryUrl: plcDirectoryUrl, plcDirectoryUrl: plcDirectoryUrl,
}); });
console.log('[OAuth Debug] OAuth client re-initialized successfully'); console.log('[OAuth Debug] OAuth client re-initialized for', pdsUrl);
} else {
console.log('[OAuth Debug] Using default OAuth client for Bluesky');
}
// 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