Skip to Content

Token API

⚡ 8 min read

The transcodes.token object manages authentication tokens and session state


Methods

getCurrentMember()

Fetches the current member profile from Transcodes Cloud. The JWT only carries identity (sub, projectId); mutable fields (email, name, role) always come from the server so admin changes apply without re-login. Also detects revoked/suspended members and emits MEMBER_REVOKED after clearing the session.

transcodes.token.getCurrentMember(): Promise<GetCurrentMemberResult>

Returns: Promise<GetCurrentMemberResult>

interface GetCurrentMemberResult { success: boolean; member: Member | null; error?: 'unauthenticated' | 'not_found' | 'revoked' | 'transient' | 'error'; }

Example:

const result = await transcodes.token.getCurrentMember(); if (result.success && result.member) { console.log('Current member:', result.member.email); } else if (result.error === 'revoked') { console.log('Member suspended or revoked'); }

For lookup by email or arbitrary fields, use transcodes.member.get(). Use getCurrentMember() when you need the signed-in user with revocation checks.


getAccessToken()

Returns a valid access token from memory or IndexedDB. Does not mint a new token — if both are missing or expired, returns null and you should call redirectToSignIn().

transcodes.token.getAccessToken(): Promise<string | null>

Returns: Promise<string | null> - The access token or null if not authenticated

Example:

const token = await transcodes.token.getAccessToken(); if (token) { fetch('https://api.example.com/data', { headers: { Authorization: `Bearer ${token}`, }, }); }

Do not validate the access token on the client. Always validate tokens on your server


hasToken()

Synchronously checks if there is a valid token in memory

transcodes.token.hasToken(): boolean

Returns: boolean - true if a token exists in memory

Example:

if (transcodes.token.hasToken()) { console.log('Token exists'); } else { console.log('No token found'); }

This is a synchronous method. Use isAuthenticated() for a more accurate async check


isAuthenticated()

Checks if the member is authenticated. Performs pure validity check without token issuance. Checks in order: Memory → IndexedDB

transcodes.token.isAuthenticated(): Promise<boolean>

Returns: Promise<boolean> - true if authenticated

Important: This method returns a Promise. Always use await!

Example:

const isAuth = await transcodes.token.isAuthenticated(); if (isAuth) { console.log('Authenticated'); } else { console.log('Not authenticated'); }

Common Mistake:

// WRONG - this will always be truthy (Promise object) if (transcodes.token.isAuthenticated()) { // This always runs! } // CORRECT - use await if (await transcodes.token.isAuthenticated()) { // This correctly checks auth status }

signOut()

Signs out the current member. Removes tokens and IndexedDB credentials and emits AUTH_STATE_CHANGED with isAuthenticated: false. Does not write an audit log — call trackUserAction({ tag: 'member:signout' }) before signOut() if you need a sign-out trail.

transcodes.token.signOut(): Promise<void>

Returns: Promise<void>

Example:

async function handleSignOut() { try { await transcodes.trackUserAction({ tag: 'member:signout', severity: 'medium', status: true, metadata: { method: 'manual' }, }); await transcodes.token.signOut(); console.log('Signed out successfully'); window.location.href = '/'; } catch (error) { console.error('Sign out failed:', error); } }

Usage Examples

Complete Authentication Check

async function checkAuth() { const isAuth = await transcodes.token.isAuthenticated(); if (!isAuth) { console.log('Not authenticated'); return null; } const token = await transcodes.token.getAccessToken(); console.log('Access token available:', !!token); return token; }

Protected API Call

async function callProtectedAPI(endpoint) { const token = await transcodes.token.getAccessToken(); if (!token) { throw new Error('Not authenticated'); } const response = await fetch(`https://api.example.com${endpoint}`, { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', }, }); if (!response.ok) { throw new Error(`API call failed: ${response.status}`); } return response.json(); }

Last updated on