True if authenticated with valid tokens, false otherwise
// Protect a feature that requires authentication
if (isAuthenticated()) {
// Show authenticated content
showUserProfile();
} else {
// Show unauthenticated state
showLoginButton();
}
export function isAuthenticated(): boolean {
const accessToken = getAccessToken();
const refreshToken = getRefreshToken();
// Check if both tokens exist
if (!accessToken || !refreshToken) {
return false;
}
// Check if token has expired
const expiry = getTokenExpiry();
if (expiry && expiry <= Date.now()) {
return false;
}
return true;
}
Checks if the user is authenticated with valid tokens
Performs a comprehensive authentication check that verifies:
This function serves as the definitive authority on authentication status throughout the application, providing a single source of truth for determining if the user can make authenticated requests.