CodeBlanks Logo Icon CodeBlanks

Summary - Secure storage

Combine setItemAsync, getItemAsync and token storage to write an auth system.

Summary - Secure storage

In this group you learned to install the expo-secure-store package, write with setItemAsync, read with getItemAsync, delete with deleteItemAsync and the token storage flow. Secure storage is the core of sensitive data.

In the summary we will merge write, read and delete in a single app. These three pieces together form a real token flow. Let us merge them now.

Practice Summary - Write a token

Complete the saveToken function. It takes token as a parameter. Call await SecureStore.setItemAsync('token', token).

Starter code

App.js
import * as SecureStore from 'expo-secure-store';

export async function saveToken(token) {
  // write the token to the secure store
}

Sample solution

App.js
await SecureStore.setItemAsync('token', token);

Practice Summary - Read and delete the token

Complete the logout function. Read with const token = await SecureStore.getItemAsync('token'). Delete with if (token) await SecureStore.deleteItemAsync('token').

Starter code

App.js
import * as SecureStore from 'expo-secure-store';

export async function logout() {
  // read the token, delete if present
}

Sample solution

App.js
const token = await SecureStore.getItemAsync('token');
if (token) {
  await SecureStore.deleteItemAsync('token');
}

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Write the write function

Question
await SecureStore.____('token', value);
Answer
await SecureStore.setItemAsync('token', value);

Explanation: setItemAsync writes to the secure store.

2 Match each flow part to its role

Term Meaning
setItemAsync Writes
getItemAsync Reads
deleteItemAsync Deletes
Bearer Authorization scheme

All lessons in Advanced