Skip to content

Commit

Permalink
fix(verify): manual check if email is verified (#1582)
Browse files Browse the repository at this point in the history
* fix(email): add missing await

* fix(verify): add manuall check for email verified on user
  • Loading branch information
emilielr authored Jul 12, 2024
1 parent 82457a7 commit 053a7c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion next-tavla/app/(admin)/components/Login/Email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Email() {
const uid = await credential.user.getIdToken()
const error = await login(uid)
if (error && auth.currentUser) {
sendEmailVerification(auth.currentUser)
await sendEmailVerification(auth.currentUser)
return getFormFeedbackForError(error)
}
} catch (e: unknown) {
Expand Down
28 changes: 24 additions & 4 deletions next-tavla/app/verify/[oob]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ 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) {
if (e instanceof FirebaseError) {
// 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) {
switch (e.code) {
case 'auth/expired-action-code':
message =
Expand All @@ -25,8 +43,10 @@ async function Verify({ params }: { params: { oob: string } }) {
'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.'
}
} 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 053a7c0

Please sign in to comment.