const appElement = document.getElementById('app') /** * Shows the login form and sets a event listener for form submission to start the OAuth flow. */ function showLoginForm() { appElement.innerHTML = `

ATProto OAuth Playground

` document.querySelector('#login-form').addEventListener('submit', async (e) => { e.preventDefault() const handle = document.querySelector('#handle').value.trim() const errorEl = document.querySelector('#error') errorEl.textContent = '' try { // This will redirect to the OAuth authorization page on the PDS await window.oauthClient.signIn(handle) } catch (error) { console.error('Sign in error:', error) errorEl.textContent = error.message || 'Failed to sign in. Please check your handle and try again.' } }) } /** * Demo component to show an authenticated request by fetching the user's notifications. * @returns {Promise} */ async function notificationsList(){ const notifications = await window.atpAgent.app.bsky.notification.listNotifications({ limit: 5 }) return notifications.data.notifications.map(notif => { const reasonText = { 'like': 'liked your post', 'repost': 'reposted your post', 'follow': 'followed you', 'mention': 'mentioned you', 'reply': 'replied to your post', 'quote': 'quoted your post' }[notif.reason] || notif.reason return `
${notif.author.displayName}

${notif.author.displayName || notif.author.handle} ${reasonText}

${new Date(notif.indexedAt).toLocaleString()}

` }).join('') } /** * Shows the logged in page with the user's profile and notifications. */ async function showLoggedInPage(session) { const profile = await window.atpAgent.getProfile({ actor: session.sub }) const { avatar, displayName, handle, followersCount, followsCount } = profile.data appElement.innerHTML = `

Logged In

Profile picture

${displayName || handle}

@${handle}

${followersCount || 0} Followers ${followsCount || 0} Following

Recent Notifications

${await notificationsList()}
` document.querySelector('#logout').addEventListener('click', async () => { try { await window.oauthClient.revoke(session.sub) showLoginForm() } catch (error) { console.error('Sign out error:', error) } }) } /** * * DANGER WILL ROBINSON */ function showError(message) { appElement.innerHTML = `

ATProto OAuth Playground

${message}
Back to login
` } export { showLoginForm, showLoggedInPage, showError }