Skip to content

Commit

Permalink
refactor(verify): remove manual check and change text in message (#1585)
Browse files Browse the repository at this point in the history
* refactor(verify): remove manual check and change text in message

* style(email): left align text and add email in messages
  • Loading branch information
emilielr authored Jul 12, 2024
1 parent 053a7c0 commit 8c1f8bf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 35 deletions.
4 changes: 2 additions & 2 deletions next-tavla/app/(admin)/components/Login/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function Create() {
)
await create(credential.user.uid)
await sendEmailVerification(credential.user)
return getFormFeedbackForError('auth/create')
return getFormFeedbackForError('auth/create', email)
} catch (e) {
if (e instanceof FirebaseError) {
return getFormFeedbackForError(e)
Expand All @@ -48,7 +48,7 @@ function Create() {
}
const [state, action] = useFormState(submit, undefined)
return (
<div className="flex flex-col text-center items-center">
<div className="flex flex-col items-center">
<Image
src={musk}
aria-hidden="true"
Expand Down
4 changes: 2 additions & 2 deletions next-tavla/app/(admin)/components/Login/Email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Email() {
const error = await login(uid)
if (error && auth.currentUser) {
await sendEmailVerification(auth.currentUser)
return getFormFeedbackForError(error)
return getFormFeedbackForError(error, email)
}
} catch (e: unknown) {
if (e instanceof FirebaseError) {
Expand All @@ -59,7 +59,7 @@ function Email() {
const getPathWithParams = useSearchParamsSetter<TLoginPage>('login')

return (
<div className="flex flex-col text-center items-center">
<div className="flex flex-col items-center">
<Image
src={musk}
aria-hidden="true"
Expand Down
15 changes: 8 additions & 7 deletions next-tavla/app/(admin)/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export type TFormFeedback = {

export type TError = FirebaseError | string

export function getFormFeedbackForError(e?: TError): TFormFeedback {
export function getFormFeedbackForError(
e?: TError,
email?: string,
): TFormFeedback {
let code = e
if (e instanceof FirebaseError) {
code = e.code
Expand All @@ -38,7 +41,7 @@ export function getFormFeedbackForError(e?: TError): TFormFeedback {
case 'auth/user-disabled':
return {
form_type: 'user',
feedback: 'Kontoen har blitt deaktivert',
feedback: 'Kontoen har blitt deaktivert.',
variant: 'error',
}
case 'auth/user-not-found':
Expand Down Expand Up @@ -69,15 +72,13 @@ export function getFormFeedbackForError(e?: TError): TFormFeedback {
case 'auth/unverified':
return {
form_type: 'user',
feedback:
'E-postadressen er ikke verifisert. Vi har sendt deg en e-post for å verifisere kontoen din.',
feedback: `E-postadressen er ikke verifisert. Vi har sendt en verifiseringslenke til ${email}.`,
variant: 'warning',
}
case 'auth/create':
return {
form_type: 'user',
feedback:
'Kontoen din har blitt opprettet. Du har mottatt en e-post for å verifisere kontoen din.',
feedback: `Brukeren din er opprettet! Før du er i mål, må du åpne verifiseringslenken som har blitt sendt til ${email}.`,
variant: 'success',
}
case 'auth/password-no-match':
Expand Down Expand Up @@ -210,7 +211,7 @@ export function getFormFeedbackForError(e?: TError): TFormFeedback {

return {
form_type: 'general',
feedback: 'En feil har oppstått',
feedback: 'En feil har oppstått.',
variant: 'error',
}
}
Expand Down
30 changes: 6 additions & 24 deletions next-tavla/app/verify/[oob]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,13 @@ import { FirebaseError } from 'firebase/app'

async function Verify({ params }: { params: { oob: string } }) {
let message = 'E-postadressen din er nå verifisert!'
const app = await getClientApp()
const auth = getAuth(app)

try {
const app = await getClientApp()
const auth = getAuth(app)
await applyActionCode(auth, params.oob)
} catch (e) {
// Check manually if email is verified
const url = `https://identitytoolkit.googleapis.com/v1/accounts:update?key=${app.options.apiKey}`
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
oobCode: params.oob,
}),
})
let emailVerified = false
if (response.ok) {
const data = await response.json()
emailVerified = data.emailVerified
}

if (e instanceof FirebaseError && !emailVerified) {
if (e instanceof FirebaseError) {
switch (e.code) {
case 'auth/expired-action-code':
message =
Expand All @@ -42,11 +25,10 @@ async function Verify({ params }: { params: { oob: string } }) {
message =
'Kontoen din er deaktivert og kunne ikke verifiseres.'
case 'auth/user-not-found':
message = 'Fant ingen bruker som samsvarte med lenken.'
default:
message = 'Noe gikk galt, prøv igjen senere.'
message =
'Det kan hende at e-posten din ikke ble verifisert. Prøv å logge inn. Hvis dette funker, er alt som det skal. Hvis ikke, sender vi en ny e-post for verifisering.'
}
}
} else message = 'Noe gikk galt, prøv igjen senere.'
}
return (
<main className="container mx-auto pt-8 pb-20 text-center">
Expand Down

0 comments on commit 8c1f8bf

Please sign in to comment.