-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(user): delete user * fix(user): aria label * chore(user): refactor * chore(user): specify consequences of deleting user
- Loading branch information
1 parent
3695a75
commit c8cdb1f
Showing
6 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
next-tavla/app/(admin)/components/Footer/components/DeleteUser.tsx
This file contains 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,74 @@ | ||
import { SecondarySquareButton } from '@entur/button' | ||
import { CloseIcon } from '@entur/icons' | ||
import { Modal } from '@entur/modal' | ||
import { Heading2, Label, Paragraph } from '@entur/typography' | ||
import { useSearchParamsModal } from 'app/(admin)/hooks/useSearchParamsModal' | ||
import Image from 'next/image' | ||
import ducks from 'assets/illustrations/Ducks.png' | ||
import { SubmitButton } from 'components/Form/SubmitButton' | ||
import { TextField } from '@entur/form' | ||
import { useFormState } from 'react-dom' | ||
|
||
import { getFormFeedbackForField } from 'app/(admin)/utils' | ||
import { deleteProfile } from './actions' | ||
|
||
function DeleteUser() { | ||
const [open, close] = useSearchParamsModal('deleteProfile') | ||
|
||
const [state, action] = useFormState(deleteProfile, undefined) | ||
return ( | ||
<> | ||
<Modal | ||
open={open} | ||
size="small" | ||
onDismiss={close} | ||
closeLabel="Avbryt sletting" | ||
className="flex flex-col justify-start items-center text-center" | ||
> | ||
<SecondarySquareButton | ||
aria-label="Avbryt sletting" | ||
className="ml-auto" | ||
onClick={close} | ||
> | ||
<CloseIcon /> | ||
</SecondarySquareButton> | ||
<Image src={ducks} alt="" className="h-1/2 w-1/2" /> | ||
<Heading2>Slett bruker</Heading2> | ||
<Paragraph> | ||
Alle dine private tavler, samt tavler tilknyttet | ||
organisasjoner hvor du er det eneste medlemmet, vil også bli | ||
slettet. | ||
</Paragraph> | ||
<form | ||
action={action} | ||
className="flex flex-col w-full gap-4" | ||
aria-live="polite" | ||
aria-relevant="all" | ||
> | ||
<Label className="font-medium text-left"> | ||
Bekreft ved å skrive inn din e-postadresse | ||
</Label> | ||
<TextField | ||
name="email" | ||
label="E-post" | ||
type="text" | ||
required | ||
aria-required | ||
className="w-full" | ||
{...getFormFeedbackForField('email', state)} | ||
/> | ||
|
||
<SubmitButton | ||
variant="primary" | ||
width="fluid" | ||
aria-label="Slett bruker" | ||
> | ||
Ja, slett | ||
</SubmitButton> | ||
</form> | ||
</Modal> | ||
</> | ||
) | ||
} | ||
|
||
export { DeleteUser } |
58 changes: 58 additions & 0 deletions
58
next-tavla/app/(admin)/components/Footer/components/actions.ts
This file contains 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,58 @@ | ||
'use server' | ||
|
||
import { TFormFeedback, getFormFeedbackForError } from 'app/(admin)/utils' | ||
import { | ||
deleteOrganization, | ||
deleteUserBoards, | ||
initializeAdminApp, | ||
} from 'app/(admin)/utils/firebase' | ||
import { getUserFromSessionCookie } from 'app/(admin)/utils/server' | ||
import { getAuth } from 'firebase-admin/auth' | ||
import { redirect } from 'next/navigation' | ||
import { logout } from '../../Login/actions' | ||
import { getOrganizationsForUser } from 'app/(admin)/actions' | ||
import { TUserID } from 'types/settings' | ||
|
||
initializeAdminApp() | ||
|
||
export async function deleteProfile( | ||
prevState: TFormFeedback | undefined, | ||
data: FormData, | ||
) { | ||
const user = await getUserFromSessionCookie() | ||
if (!user) return redirect('/') | ||
|
||
const adminAuth = getAuth() | ||
|
||
const email = data.get('email') as string | ||
|
||
try { | ||
const userRecord = await adminAuth.getUser(user.uid) | ||
|
||
if (email !== userRecord.email) | ||
return getFormFeedbackForError('auth/email-mismatch') | ||
await deleteUserBoardsAndOrganizations(userRecord.uid) | ||
await adminAuth.deleteUser(userRecord.uid) | ||
await logout() | ||
} catch (error) { | ||
return getFormFeedbackForError() | ||
} | ||
} | ||
|
||
async function deleteUserBoardsAndOrganizations(uid: TUserID) { | ||
try { | ||
const organizations = await getOrganizationsForUser() | ||
|
||
await deleteUserBoards() | ||
return Promise.all( | ||
organizations.map((org) => { | ||
if (org.owners?.includes(uid) && org.owners.length < 2) { | ||
if (!org.id) return | ||
deleteOrganization(org.id) | ||
} | ||
}), | ||
) | ||
} catch (error) { | ||
return getFormFeedbackForError() | ||
} | ||
} |
This file contains 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 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 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 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