Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup Optic Tokens to a file to allow importing from another device #590

Merged
merged 13 commits into from
Jul 13, 2022
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
"expo-camera": "~12.1.2",
"expo-clipboard": "~2.1.0",
"expo-constants": "~13.0.0",
"expo-document-picker": "~10.1.3",
"expo-file-system": "~13.1.4",
"expo-font": "~10.0.4",
"expo-local-authentication": "~12.1.0",
"expo-media-library": "~14.0.0",
"expo-notifications": "~0.14.0",
"expo-random": "~12.1.1",
"expo-secure-store": "~11.1.0",
"expo-sharing": "~10.1.0",
"expo-status-bar": "~1.2.0",
"expo-updates": "~0.11.7",
"expo-web-browser": "~10.1.0",
Expand Down
13 changes: 11 additions & 2 deletions src/context/SecretsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type ContextType = {
add: (_: Omit<Secret, '_id'>) => Promise<void>
update: (_: Secret) => Promise<void>
remove: (_: Secret) => Promise<void>
reset: () => Promise<void>
}

const initialContext: ContextType = {
Expand All @@ -30,6 +31,9 @@ const initialContext: ContextType = {
remove: async () => {
// @todo
},
reset: async () => {
// @todo
},
}

const SecretsContext = createContext<ContextType>(initialContext)
Expand Down Expand Up @@ -86,9 +90,14 @@ export const SecretsProvider: React.FC<SecretsProviderProps> = ({
[user]
)

const reset = useCallback<ContextType['reset']>(async () => {
await secretsManager.reset()
setSecrets([])
}, [])

const value = useMemo<ContextType>(
() => ({ secrets, add, update, remove }),
[secrets, add, update, remove]
() => ({ secrets, add, update, remove, reset }),
[secrets, add, update, remove, reset]
)

return (
Expand Down
15 changes: 14 additions & 1 deletion src/lib/secretsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,17 @@ const remove = async (secretId: string) => {
}
}

export default { getAllByUser, get, upsert, remove }
/**
* Resets the secrets to an empty list.
* @async
* @returns {void}
*/
const reset = async () => {
try {
await save([])
} catch (err) {
console.error('Failed to reset the secrets from data store', err)
}
}

export default { getAllByUser, get, upsert, remove, reset }
1 change: 1 addition & 0 deletions src/screens/HomeScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('HomeScreen', () => {
add: jest.fn(),
remove: jest.fn(),
update: jest.fn(),
reset: jest.fn(),
})

const view = setup()
Expand Down
Loading