-
-
Notifications
You must be signed in to change notification settings - Fork 474
feat: add self-service account deletion to settings page #4650
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
Open
GMetaxakis
wants to merge
9
commits into
ONEARMY:master
Choose a base branch
from
GMetaxakis:feat/delete-account-button
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a17201c
feat: add self-service account deletion to settings page (#4163)
GMetaxakis 0f05898
test: add e2e tests for account deletion
GMetaxakis cd78d68
Merge branch 'master' into feat/delete-account-button
mariojsnunes d9e0cd9
Merge branch 'master' into feat/delete-account-button
mariojsnunes 899b540
fix: merge delete account tests into a single test flow
GMetaxakis 50eb8a2
Merge branch 'feat/delete-account-button' of https://github.com/GMeta…
GMetaxakis b47adc6
Merge remote-tracking branch 'origin/master' into feat/delete-account…
GMetaxakis 9e99aea
fix: address PR review feedback for account deletion
GMetaxakis 44838b3
Merge branch 'master' into feat/delete-account-button
mariojsnunes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/pages/UserSettings/content/sections/DeleteAccount.form.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { Accordion, Button, ConfirmModal, FieldInput } from 'oa-components'; | ||
| import { useState } from 'react'; | ||
| import { Form } from 'react-final-form'; | ||
| import { useNavigate } from 'react-router'; | ||
| import { PasswordField } from 'src/common/Form/PasswordField'; | ||
| import { FormFieldWrapper } from 'src/pages/common/FormFields'; | ||
| import type { SubmitResults } from 'src/pages/User/contact/UserContactError'; | ||
| import { UserContactError } from 'src/pages/User/contact/UserContactError'; | ||
| import { buttons, fields } from 'src/pages/UserSettings/labels'; | ||
| import { Flex } from 'theme-ui'; | ||
| import { accountService } from '../../services/account.service'; | ||
|
|
||
| interface IFormValues { | ||
| password: string; | ||
| } | ||
|
|
||
| export const DeleteAccountForm = () => { | ||
| const navigate = useNavigate(); | ||
| const [submitResults, setSubmitResults] = useState<SubmitResults | null>(null); | ||
| const [isConfirmOpen, setIsConfirmOpen] = useState(false); | ||
| const [pendingPassword, setPendingPassword] = useState(''); | ||
|
|
||
| const formId = 'deleteAccount'; | ||
|
|
||
| const onSubmit = (values: IFormValues) => { | ||
| setPendingPassword(values.password); | ||
| setIsConfirmOpen(true); | ||
| }; | ||
|
|
||
| const onConfirm = async () => { | ||
| setIsConfirmOpen(false); | ||
|
|
||
| try { | ||
| const result = await accountService.deleteAccount(pendingPassword); | ||
|
|
||
| if (!result.ok) { | ||
| setSubmitResults({ | ||
| type: 'error', | ||
| message: result.statusText || 'Oops, something went wrong!', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| navigate('/'); | ||
| } catch { | ||
| setSubmitResults({ | ||
| type: 'error', | ||
| message: 'Oops, something went wrong!', | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Flex data-cy="deleteAccountContainer" sx={{ flexDirection: 'column', gap: 2 }}> | ||
| <UserContactError submitResults={submitResults} /> | ||
|
|
||
| <Accordion title={fields.deleteAccount.title} subtitle={fields.deleteAccount.description}> | ||
| <Form | ||
| onSubmit={onSubmit} | ||
| id={formId} | ||
| render={({ handleSubmit, submitting, values }) => { | ||
| const disabled = submitting || !values.password; | ||
|
|
||
| return ( | ||
| <Flex data-cy="deleteAccountForm" sx={{ flexDirection: 'column', gap: 2 }}> | ||
| <FormFieldWrapper text={fields.password.title} htmlFor="password" required> | ||
| <PasswordField | ||
| autoComplete="off" | ||
| component={FieldInput} | ||
| data-cy="deleteAccountPassword" | ||
| name="password" | ||
| placeholder="Password" | ||
| required | ||
| /> | ||
| </FormFieldWrapper> | ||
|
|
||
| <Button | ||
| data-cy="deleteAccountSubmit" | ||
| disabled={disabled} | ||
| form={formId} | ||
| onClick={handleSubmit} | ||
| type="submit" | ||
| variant="destructive" | ||
| sx={{ | ||
| alignSelf: 'flex-start', | ||
| }} | ||
| > | ||
| {buttons.deleteAccount} | ||
| </Button> | ||
| </Flex> | ||
| ); | ||
| }} | ||
| /> | ||
| </Accordion> | ||
|
|
||
| <ConfirmModal | ||
| isOpen={isConfirmOpen} | ||
| message="Are you sure you want to permanently delete your account? This action cannot be undone and all your data will be lost." | ||
| confirmButtonText="Delete my account" | ||
| handleCancel={() => setIsConfirmOpen(false)} | ||
| handleConfirm={onConfirm} | ||
| confirmVariant="destructive" | ||
| /> | ||
| </Flex> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,18 @@ const changePassword = async (oldPassword: string, newPassword: string) => { | |
| }); | ||
| }; | ||
|
|
||
| const deleteAccount = async (password: string) => { | ||
| const data = new FormData(); | ||
| data.append('password', password); | ||
|
|
||
| return await fetch('/api/account/delete', { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to double-check, using a POST instead of DELETE because we need to send FormData? |
||
| method: 'POST', | ||
| body: data, | ||
| }); | ||
| }; | ||
|
|
||
| export const accountService = { | ||
| changeEmail, | ||
| changePassword, | ||
| deleteAccount, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import type { ActionFunctionArgs } from 'react-router'; | ||
| import { createSupabaseServerClient } from 'src/repository/supabase.server'; | ||
| import { createSupabaseAdminServerClient } from 'src/repository/supabaseAdmin.server'; | ||
| import { ProfileServiceServer } from 'src/services/profileService.server'; | ||
|
|
||
| export const action = async ({ request }: ActionFunctionArgs) => { | ||
| const { client, headers } = createSupabaseServerClient(request); | ||
|
|
||
| try { | ||
| const formData = await request.formData(); | ||
| const password = formData.get('password') as string; | ||
|
|
||
| if (!password) { | ||
| return Response.json({}, { headers, status: 400, statusText: 'Password is required' }); | ||
| } | ||
|
|
||
| const claims = await client.auth.getClaims(); | ||
| const authId = claims.data?.claims?.sub; | ||
|
|
||
| if (!authId) { | ||
| return Response.json({}, { headers, status: 401 }); | ||
| } | ||
|
|
||
| const signInResult = await client.auth.signInWithPassword({ | ||
| email: claims.data?.claims?.email as string, | ||
| password, | ||
| }); | ||
|
|
||
| if (signInResult.error) { | ||
| return Response.json({}, { headers, status: 400, statusText: 'Invalid password' }); | ||
| } | ||
|
|
||
| const profileService = new ProfileServiceServer(client); | ||
| const profile = await profileService.getByAuthId(authId); | ||
|
|
||
| if (!profile) { | ||
| return Response.json({}, { headers, status: 400, statusText: 'Profile not found' }); | ||
| } | ||
|
|
||
| const adminClient = createSupabaseAdminServerClient(); | ||
|
|
||
| const { data: allProfiles } = await adminClient | ||
| .from('profiles') | ||
| .select('id, tenant_id') | ||
| .eq('auth_id', authId); | ||
|
|
||
| const hasOtherTenantProfiles = (allProfiles?.length ?? 0) > 1; | ||
|
|
||
| if (hasOtherTenantProfiles) { | ||
| const { error } = await client.from('profiles').delete().eq('id', profile.id); | ||
|
|
||
| if (error) { | ||
| throw error; | ||
| } | ||
| } else { | ||
| const { error } = await adminClient.auth.admin.deleteUser(authId); | ||
|
|
||
| if (error) { | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| await client.auth.signOut(); | ||
|
|
||
| return new Response(null, { headers, status: 204 }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return Response.json({}, { headers, status: 500, statusText: 'Failed to delete account' }); | ||
| } | ||
| }; |
9 changes: 9 additions & 0 deletions
9
supabase/migrations/20260324000000_fix_profile_delete_cascades.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| alter table "public"."notifications" drop constraint "notifications_triggered_by_id_fkey"; | ||
| alter table "public"."notifications" add constraint "notifications_triggered_by_id_fkey" | ||
| FOREIGN KEY (triggered_by_id) REFERENCES profiles(id) | ||
| ON UPDATE CASCADE ON DELETE SET NULL; | ||
|
|
||
| alter table "public"."notifications_preferences" drop constraint "notifications_preferences_user_id_fkey"; | ||
| alter table "public"."notifications_preferences" add constraint "notifications_preferences_user_id_fkey" | ||
| FOREIGN KEY (user_id) REFERENCES profiles(id) | ||
| ON UPDATE CASCADE ON DELETE CASCADE; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to see what happens to the content of a deleted user:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To do that, the best way might be with a seed. Have the profile and associated items which we delete on the test, instead of signing up a new user and creating the content each time.
If that's difficult for some reason, at least this PR must ensure those delete/set null cascades are correct.