| name | platform-auth-token-pattern |
| description | Frontend authentication token retrieval patterns and common mistakes Use when this capability is needed. |
| metadata | {"author":"aaaa47080"} |
Platform Auth Token Pattern - Frontend Authentication
This skill documents how to correctly retrieve authentication tokens in the frontend JavaScript code for PI CryptoMind.
The Problem
Incorrect patterns that have caused bugs:
const token = localStorage.getItem('access_token');
const token = AuthManager.currentUser.accessToken;
The Correct Pattern
Primary Source: AuthManager.currentUser
if (AuthManager.currentUser && AuthManager.currentUser.accessToken) {
const token = AuthManager.currentUser.accessToken;
}
Fallback: localStorage
const token = localStorage.getItem('auth_token');
Best Practice: Helper Function
function _getToken() {
if (AuthManager.currentUser && AuthManager.currentUser.accessToken) {
return AuthManager.currentUser.accessToken;
}
const stored = localStorage.getItem('auth_token');
if (stored) {
return stored;
}
return null;
}
const token = _getToken();
if (!token) {
console.error('No authentication token available');
return;
}
Authentication Flow
1. Pi SDK Authentication
async function handlePiLogin() {
try {
const scopes = ['username', 'payments', 'wallet_address'];
const auth = await window.Pi.authenticate(scopes, onIncompletePaymentFound);
const response = await fetch('/api/auth/verify-pi', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ access_token: auth.accessToken })
});
const data = await response.json();
AuthManager.currentUser = {
username: auth.user.username,
uid: auth.user.uid,
accessToken: auth.accessToken,
walletAddress: data.wallet_address,
isPremium: data.is_premium
};
.(, auth.);
;
} (error) {
.(, error);
;
}
}
Common Mistakes & Fixes
Mistake #1: Wrong localStorage Key
const token = localStorage.getItem('access_token');
const token = localStorage.getItem('auth_token');
Root cause: The key is 'auth_token', not 'access_token'
Mistake #2: Not Checking AuthManager First
const token = localStorage.getItem('auth_token');
const token = AuthManager.currentUser?.accessToken || localStorage.getItem('auth_token');
Why: AuthManager is the source of truth after login. localStorage is just persistence.
Version History
- v1.0: Initial documentation (2026-02-08)
Converted and distributed by TomeVault — claim your Tome and manage your conversions.