···133133134134 setStatus('Getting user profile...');
135135136136- // We'll extract the PDS endpoint from the decoded token
136136+ // Extract PDS endpoint from the token
137137+ let pdsEndpoint = null;
138138+139139+ // First, try to decode the access token
140140+ try {
141141+ const parts = tokenResponse.access_token.split('.');
142142+ if (parts.length === 3) {
143143+ const payload = JSON.parse(atob(parts[1]));
144144+ if (payload.aud && typeof payload.aud === 'string' && payload.aud.startsWith('did:web:')) {
145145+ pdsEndpoint = 'https://' + payload.aud.replace('did:web:', '');
146146+ }
147147+ }
148148+ } catch (e) {
149149+ console.warn('Failed to extract PDS endpoint from token');
150150+ }
137151138152 // Get user profile
139153 let profileResponse;
···143157 keyPair,
144158 dpopNonce,
145159 undefined, // Use default handle
146146- pdsEndpoint // Pass the PDS endpoint
160160+ pdsEndpoint // Pass the PDS endpoint if we have it
147161 );
148162 } catch (profileError: any) {
149163 console.error('Profile fetch error:', profileError);
···161175 const userDid = tokenResponse.sub;
162176 console.log('User DID from token:', userDid);
163177164164- // Extract PDS endpoint from the token
165165- let extractedPdsEndpoint = null;
166166-167167- // Try to decode the access token to extract the audience
168168- try {
169169- if (tokenResponse.access_token) {
170170- const parts = tokenResponse.access_token.split('.');
171171- if (parts.length === 3) {
172172- // Decode the payload (second part)
173173- const payload = JSON.parse(atob(parts[1]));
174174-175175- // Extract audience from the decoded token
176176- if (payload.aud && typeof payload.aud === 'string') {
177177- // Update the pdsEndpoint from the decoded token if available
178178- if (payload.aud.startsWith('did:web:')) {
179179- // Convert did:web:example.com to https://example.com
180180- extractedPdsEndpoint = 'https://' + payload.aud.replace('did:web:', '');
181181- console.log('Extracted PDS endpoint from decoded token:', extractedPdsEndpoint);
182182-183183- // Update our variable for use in the rest of the function
184184- pdsEndpoint = extractedPdsEndpoint;
185185- }
186186- }
187187- }
188188- }
189189- } catch (error) {
190190- console.warn('Failed to decode token:', error);
191191- }
192192-193193- // If we couldn't extract the PDS endpoint, show an error
194194- if (!pdsEndpoint) {
195195- console.error('Failed to extract PDS endpoint from token. This will cause API calls to fail.');
178178+ // If we were able to extract the PDS endpoint, log it
179179+ if (pdsEndpoint) {
180180+ console.log('Extracted PDS endpoint from token:', pdsEndpoint);
181181+ } else {
182182+ console.warn('Could not extract PDS endpoint from token');
196183 }
197184198185 // Now that we have the PDS endpoint, try to get the user's handle directly
···209196 keyPair,
210197 dpopNonce,
211198 userDid, // Use the user's DID instead of default
212212- pdsEndpoint
199199+ pdsEndpoint // Pass the PDS endpoint if we have it
213200 );
214201215202 if (handleResponse && handleResponse.handle) {
···224211 }
225212 }
226213227227- // Log the PDS endpoint that will be used
228228- console.log('Using PDS endpoint for API requests:', pdsEndpoint);
229229-230214 // Store auth data
231231- console.log('Saving PDS endpoint to auth context:', pdsEndpoint);
232215 setAuth({
233216 accessToken: tokenResponse.access_token,
234217 refreshToken: tokenResponse.refresh_token,