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
Authentication Flow
Creating a Session
Use theAtpAgent class for automatic session management:
Session Data Structure
Persisting Sessions
Persist sessions across app restarts:create- New session createdcreate-failed- Session creation failedupdate- Session refreshedexpired- Session expired or logged outnetwork-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
- Long-lived (months or longer)
- Should be stored securely
- Used to obtain new access tokens
Best Practices
Store refresh tokens securely
Store refresh tokens securely
Use secure storage mechanisms:
- Web: httpOnly cookies or encrypted localStorage
- Mobile: Secure enclave/keychain
- Never expose refresh tokens to XSS attacks
Implement session timeout
Implement session timeout
Clear sessions after periods of inactivity to reduce exposure.
Handle token refresh failures
Handle token refresh failures
If refresh fails, prompt user to re-authenticate rather than silently failing.
Revoke on logout
Revoke on logout
Always call logout() to revoke refresh tokens server-side.
Use HTTPS only
Use HTTPS only
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:Related Topics
- Identity - DIDs and handles used in authentication
- Overview - AT Protocol architecture
- API Reference - AtpAgent API documentation
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