Skip to main content

Token Management

Learn about access tokens, refresh tokens, and token lifecycle management.


Token Types

Access Token

PropertyDescription
PurposeAuthorize API requests
FormatJWT (JSON Web Token)
Lifetime1 hour (configurable)
UsageAuthorization: Bearer <token>

Refresh Token

PropertyDescription
PurposeObtain new access tokens
FormatOpaque string
Lifetime30 days (configurable)
UsageExchange for new access token

ID Token

PropertyDescription
PurposeContains user identity claims
FormatJWT
LifetimeSame as access token
UsageRetrieve user information

Token Response

When you exchange an authorization code for tokens:

{
"access_token": "eyJhbGciOiJSUzI1NiIs...",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "def50200a1b2c3d4e5f6...",
"id_token": "eyJhbGciOiJSUzI1NiIs...",
"scope": "openid profile email"
}

Access Token Claims

Decoded JWT payload:

{
"iss": "https://api.syauth.com/e/v1",
"sub": "user-uuid",
"aud": "your-client-id",
"exp": 1703123456,
"iat": 1703119856,
"scope": "openid profile email",
"email": "[email protected]"
}
ClaimDescription
issToken issuer (SyAuth)
subSubject (user ID)
audAudience (your client ID)
expExpiration timestamp
iatIssued at timestamp
scopeGranted scopes

Refreshing Tokens

Access tokens are short-lived (usually 1 hour). When they expire, use the refresh token to get a new one.

The SDK automatically handles token refreshing in the background before the access token expires.

const { getAccessToken } = useSyAuth();

// This function guarantees a valid token, refreshing if necessary
const token = await getAccessToken();

Token Revocation

When a user logs out, you should revoke their tokens.

curl -X POST https://api.syauth.com/e/v1/oauth/revoke \
-d "token=TOKEN_TO_REVOKE" \
-d "token_type_hint=access_token" \
-d "client_id=YOUR_CLIENT_ID"

Token Storage

The SDK stores tokens in secure HttpOnly cookies:

CookieFlags
syauth_access_tokenHttpOnly, Secure, SameSite=Lax
syauth_refresh_tokenHttpOnly, Secure, SameSite=Lax

Never store tokens in localStorage because:

  • Accessible to JavaScript (XSS vulnerable)
  • Persists across browser sessions
  • No expiration control

Token Validation

You can validate tokens by calling the allowed-methods endpoint or by verifying the JWT signature locally.

Simply verify the token by using it to fetch user info. If the token is invalid, this will return 401.

curl https://api.syauth.com/e/v1/oauth/userinfo \
-H "Authorization: Bearer <access_token>"

Token Expiration Handling

SDK Automatic Handling

The SDK handles expiration automatically:

  1. Monitors token expiration
  2. Refreshes 5 minutes before expiry
  3. Seamlessly updates stored tokens
  4. Re-authenticates if refresh fails

Custom Handling

function MyComponent() {
const { getAccessToken, isAuthenticated } = useSyAuth();

const fetchData = async () => {
// getAccessToken() returns valid token or refreshes if needed
const token = await getAccessToken();

const response = await fetch('/api/data', {
headers: {
'Authorization': `Bearer ${token}`
}
});

return response.json();
};
}

Security Considerations

RiskMitigation
Token theftUse HttpOnly cookies, short expiration
Token replayValidate audience and issuer claims
Refresh token abuseToken rotation on use
XSS attacksNever expose tokens to JavaScript

Next Steps