Skip to main content

Lesson 6 of 9

Secure Store - Tokens & Sensitive Data

expo-secure-store uses iOS Keychain Services and Android Keystore to encrypt values at rest. It's the correct storage layer for JWT tokens, refresh tokens, device keys, and any value that shouldn't be readable if a device is compromised.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

expo-secure-store Production Patterns

expo-secure-store Production Patterns
import * as SecureStore from 'expo-secure-store';
// npx expo install expo-secure-store

// ---- Store with encryption options ----
await SecureStore.setItemAsync('access_token', token, {
  // Only accessible when device is unlocked (most secure, default)
  keychainAccessible: SecureStore.WHEN_UNLOCKED,
  // Or: AFTER_FIRST_UNLOCK (survives backgrounding)
  // Or: ALWAYS_THIS_DEVICE_ONLY (no iCloud backup, most secure)
});

// ---- Read ----
const token = await SecureStore.getItemAsync('access_token');
// Returns: string | null (null if not found, not null if wrong type)

// ---- Delete ----
await SecureStore.deleteItemAsync('access_token');

// ---- Token Manager pattern ----
const TOKEN_KEYS = {
  ACCESS: 'auth.access_token',
  REFRESH: 'auth.refresh_token',
  DEVICE_ID: 'auth.device_id',
} as const;

export const tokenManager = {
  async storeTokens(accessToken: string, refreshToken: string) {
    await Promise.all([
      SecureStore.setItemAsync(TOKEN_KEYS.ACCESS, accessToken),
      SecureStore.setItemAsync(TOKEN_KEYS.REFRESH, refreshToken),
    ]);
  },

  async getAccessToken() {
    return SecureStore.getItemAsync(TOKEN_KEYS.ACCESS);
  },

  async getRefreshToken() {
    return SecureStore.getItemAsync(TOKEN_KEYS.REFRESH);
  },

  async clearAll() {
    await Promise.all([
      SecureStore.deleteItemAsync(TOKEN_KEYS.ACCESS),
      SecureStore.deleteItemAsync(TOKEN_KEYS.REFRESH),
    ]);
  },

  async getOrCreateDeviceId() {
    let deviceId = await SecureStore.getItemAsync(TOKEN_KEYS.DEVICE_ID);
    if (!deviceId) {
      deviceId = generateUUID();
      await SecureStore.setItemAsync(TOKEN_KEYS.DEVICE_ID, deviceId, {
        keychainAccessible: SecureStore.ALWAYS_THIS_DEVICE_ONLY, // never leaves device
      });
    }
    return deviceId;
  },
};

function generateUUID(): string {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    const r = Math.random() * 16 | 0;
    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
}

Tip

Tip

Practice Secure Store Tokens Sensitive Data in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagramโ€ฆ

Hardware-backed encryption for JWT tokens, passwords, and biometric credentials

Practice Task

Note

Practice Task - (1) Write a working example of Secure Store Tokens Sensitive Data from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Secure Store Tokens Sensitive Data is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/9 done