LoginSignup
5
2

More than 3 years have passed since last update.

RactNative(Expo)でSecureStoreを利用する。

Last updated at Posted at 2018-12-01

秘密鍵情報やsecret等の保存は悩ましいところです。
ExpoではSecureStoreという、iOSだと、Keychain Servicesを、AndroidならKeystore Systemをラップした保存手法を提供してくれます。AsyncStoreとほぼ同じ感じで使えるようです。

App.js

実装は以下の通り。特に難しいことはありません。セットしない、deleteした場合はnullが返るようです。

App.js
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import { SecureStore } from 'expo';

export default class App extends React.Component {
    render() {
        return (
            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Button
                    title='setItem'
                    onPress={() => this.handleSetItem()}
                />
                <Button
                    title='getItem'
                    onPress={() => this.handleGetItem()}
                />
                <Button
                    title='deleteItem'
                    onPress={() => this.handleDeleteItem()}
                />
            </View>
        );
    }

    handleSetItem = async () => {
        await SecureStore.setItemAsync('access_key', 'hoge');
        alert('setItem');
    }

    handleGetItem = async () => {
        const key = await SecureStore.getItemAsync('access_key');
        if (key === null) {
            alert('セットされていません。');
        } else {
            alert(key);
        }
    }

    handleDeleteItem = async () => {
        await SecureStore.deleteItemAsync('access_key');
        alert('deleteItem');
    }
}

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2