Skip to main content

What is Authentication in AT Protocol?

AT Protocol uses JWT (JSON Web Token) based authentication with a dual-token system:
  • Access Token - Short-lived token for API requests
  • Refresh Token - Long-lived token to obtain new access tokens
This approach balances security with user experience, allowing secure API access without requiring frequent re-authentication.

Authentication Flow

Creating a Session

Use the AtpAgent class for automatic session management:

Session Data Structure

Persisting Sessions

Persist sessions across app restarts:
Session Events:
  • create - New session created
  • create-failed - Session creation failed
  • update - Session refreshed
  • expired - Session expired or logged out
  • network-error - Transient refresh failure

Automatic Token Refresh

AtpAgent automatically handles token refresh:

Manual Token Refresh

Manually refresh when needed:

Creating Accounts

Create a new account and session:

Logging Out

End a session:

Using Custom Session Managers

For advanced use cases, implement custom session management:

Multi-Factor Authentication

Handle 2FA when required:

PDS Endpoint Discovery

AtpAgent automatically discovers and uses the correct PDS endpoint:

Session Security

Token Storage

Access Tokens:
  • Short-lived (typically 2 hours)
  • Can be stored in memory
  • Less sensitive than refresh tokens
Refresh Tokens:
  • Long-lived (months or longer)
  • Should be stored securely
  • Used to obtain new access tokens

Best Practices

Use secure storage mechanisms:
  • Web: httpOnly cookies or encrypted localStorage
  • Mobile: Secure enclave/keychain
  • Never expose refresh tokens to XSS attacks
Clear sessions after periods of inactivity to reduce exposure.
If refresh fails, prompt user to re-authenticate rather than silently failing.
Always call logout() to revoke refresh tokens server-side.
Never transmit credentials or tokens over unencrypted connections.

Error Handling

Advanced: Direct XRPC Calls

For fine-grained control, use XRPC directly:

Session Lifecycle Example

Complete session management implementation:

Additional Resources

@atproto/api Package

NPM package documentation

XRPC Specification

XRPC protocol specification

JWT.io

Learn more about JSON Web Tokens

Server API Reference

Authentication API endpoints