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

add account deletion to settings #718

Merged
merged 2 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type ContextType = {
handleLoginGoogle: () => void
handleLoginApple: () => void
handleLogout: () => Promise<void>
handleDeleteAccount: () => Promise<void>
}

const AuthContext = React.createContext<ContextType | undefined>(undefined)
Expand Down Expand Up @@ -125,40 +126,67 @@ function useAppleAuth() {
export const AuthProvider: React.FC<AuthenticationProviderProps> = ({
children,
}) => {
const [firebaseUser, setFirebaseUser] = useState<firebase.User>()
const [loading, setLoading] = useState(true)
const [user, setUser] = useState<User>()

const handleLoginGoogle = useGoogleAuth()
const handleLoginApple = useAppleAuth()

useEffect(() => {
firebase.auth().onAuthStateChanged(async firebaseUser => {
firebase.auth().onAuthStateChanged(newFirebaseUser => {
setLoading(false)
if (!firebaseUser) return
setFirebaseUser(newFirebaseUser)
})
}, [])

useEffect(() => {
const updateUser = async () => {
if (!firebaseUser) {
setUser(null)
return
}
const idToken = await firebaseUser.getIdToken()
setUser({
name: firebaseUser.displayName,
email: firebaseUser.email,
uid: firebaseUser.uid,
idToken: await firebaseUser.getIdToken(),
idToken,
})
})
}, [])
}

updateUser()
}, [firebaseUser])

const handleLogout = useCallback<ContextType['handleLogout']>(async () => {
await firebase.auth().signOut()
setUser(null)
}, [])

const handleDeleteAccount = useCallback<
ContextType['handleLogout']
>(async () => {
if (firebaseUser) await firebaseUser.delete()
setUser(null)
}, [firebaseUser])

const context = useMemo<ContextType>(
() => ({
loading,
user,
handleLogout,
handleLoginApple,
handleLoginGoogle,
handleDeleteAccount,
}),
[user, loading, handleLogout, handleLoginGoogle, handleLoginApple]
[
user,
loading,
handleLogout,
handleLoginGoogle,
handleLoginApple,
handleDeleteAccount,
]
)

return <AuthContext.Provider value={context}>{children}</AuthContext.Provider>
Expand Down
38 changes: 38 additions & 0 deletions src/hooks/use-delete-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useCallback, useMemo } from 'react'

import { useAuth } from '../context/AuthContext'
import { useSecrets } from '../context/SecretsContext'
import apiFactory from '../lib/api'

export const useDeleteAccount = () => {
const { user, handleDeleteAccount } = useAuth()
const api = useMemo(() => apiFactory({ idToken: user.idToken }), [user])
const { secrets, replace } = useSecrets()

const deleteAllTokens = useCallback(async () => {
const deletePromises = []

secrets.forEach(({ tokens }) => {
if (Array.isArray(tokens)) {
tokens.forEach(t => {
deletePromises.push(api.revokeToken(t.token))
})
}
})
await Promise.all(deletePromises)
}, [api, secrets])

const deleteAllSecrets = useCallback(async () => {
await replace([])
}, [replace])

const deleteAccount = useCallback(async () => {
await deleteAllTokens()
await deleteAllSecrets()
await handleDeleteAccount()
}, [deleteAllTokens, deleteAllSecrets, handleDeleteAccount])

return {
deleteAccount,
}
}
104 changes: 104 additions & 0 deletions src/screens/SettingsScreen.test.tsx.snap.android
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,110 @@ exports[`SettingsScreen should match snapshot 1`] = `
</View>
</View>
</View>
<View
collapsable={false}
style={
Object {
"backgroundColor": "transparent",
"borderColor": "transparent",
"borderRadius": 4,
"borderStyle": "solid",
"borderWidth": 0,
"elevation": 0,
"marginTop": 80,
"minWidth": 64,
"shadowColor": "#000000",
"shadowOffset": Object {
"height": 0,
"width": 0,
},
"shadowOpacity": 0,
"shadowRadius": 0,
}
}
>
<View
accessibilityRole="button"
accessibilityState={
Object {
"disabled": false,
}
}
accessible={true}
focusable={true}
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
Array [
Object {
"overflow": "hidden",
},
Object {
"borderRadius": 4,
},
]
}
>
<View
style={
Array [
Object {
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
undefined,
]
}
>
<Text
numberOfLines={1}
selectable={false}
style={
Array [
Object {
"color": "#6D6D68",
"fontFamily": "sans-serif",
"fontWeight": "normal",
},
Object {
"textAlign": "left",
},
Array [
Object {
"letterSpacing": 1,
"marginHorizontal": 16,
"marginVertical": 9,
"textAlign": "center",
},
undefined,
Object {
"textTransform": "uppercase",
},
Object {
"color": "red",
"fontFamily": "sans-serif-medium",
"fontWeight": "normal",
},
Object {
"fontFamily": "sans-serif-medium",
"fontWeight": "normal",
},
undefined,
],
]
}
>
Delete account
</Text>
</View>
</View>
</View>
</View>
</View>
</View>
Expand Down
104 changes: 104 additions & 0 deletions src/screens/SettingsScreen.test.tsx.snap.ios
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,110 @@ exports[`SettingsScreen should match snapshot 1`] = `
</View>
</View>
</View>
<View
collapsable={false}
style={
Object {
"backgroundColor": "transparent",
"borderColor": "transparent",
"borderRadius": 4,
"borderStyle": "solid",
"borderWidth": 0,
"elevation": 0,
"marginTop": 80,
"minWidth": 64,
"shadowColor": "#000000",
"shadowOffset": Object {
"height": 0,
"width": 0,
},
"shadowOpacity": 0,
"shadowRadius": 0,
}
}
>
<View
accessibilityRole="button"
accessibilityState={
Object {
"disabled": false,
}
}
accessible={true}
focusable={true}
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
Array [
Object {
"overflow": "hidden",
},
Object {
"borderRadius": 4,
},
]
}
>
<View
style={
Array [
Object {
"alignItems": "center",
"flexDirection": "row",
"justifyContent": "center",
},
undefined,
]
}
>
<Text
numberOfLines={1}
selectable={false}
style={
Array [
Object {
"color": "#6D6D68",
"fontFamily": "System",
"fontWeight": "400",
},
Object {
"textAlign": "left",
},
Array [
Object {
"letterSpacing": 1,
"marginHorizontal": 16,
"marginVertical": 9,
"textAlign": "center",
},
undefined,
Object {
"textTransform": "uppercase",
},
Object {
"color": "red",
"fontFamily": "System",
"fontWeight": "500",
},
Object {
"fontFamily": "System",
"fontWeight": "500",
},
undefined,
],
]
}
>
Delete account
</Text>
</View>
</View>
</View>
</View>
</View>
</View>
Expand Down
Loading