Skip to content

Commit 9679b37

Browse files
committed
Show error
1 parent 6cb736a commit 9679b37

File tree

2 files changed

+65
-55
lines changed

2 files changed

+65
-55
lines changed

web/lib/util/signup.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ import Router from 'next/router'
22
import { firebaseLogin } from 'web/lib/firebase/users'
33
import { db } from 'web/lib/supabase/db'
44
import { getProfileRow } from 'common/profiles/profile'
5+
import toast from "react-hot-toast";
56

67
export const signupThenMaybeRedirectToSignup = async () => {
7-
const creds = await firebaseLogin()
8-
const userId = creds?.user.uid
9-
if (userId) {
10-
const profile = await getProfileRow(userId, db)
11-
if (profile) {
12-
await Router.push('/')
13-
} else {
14-
await Router.push('/signup')
8+
try {
9+
const creds = await firebaseLogin()
10+
const userId = creds?.user.uid
11+
if (userId) {
12+
const profile = await getProfileRow(userId, db)
13+
if (profile) {
14+
await Router.push('/')
15+
} else {
16+
await Router.push('/signup')
17+
}
1518
}
19+
} catch (e: any) {
20+
console.error(e)
21+
toast.error('Failed to sign in: ' + e.message)
1622
}
1723
}
1824

web/pages/register.tsx

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
1-
"use client";
1+
"use client"
22

3-
import React, {Suspense, useEffect, useState} from "react";
4-
import Link from "next/link";
5-
import {useSearchParams} from "next/navigation";
6-
import {signupThenMaybeRedirectToSignup} from "web/lib/util/signup";
3+
import React, {Suspense, useEffect, useState} from "react"
4+
import Link from "next/link"
5+
import {useSearchParams} from "next/navigation"
6+
import {signupThenMaybeRedirectToSignup} from "web/lib/util/signup"
77

8-
import {createUserWithEmailAndPassword} from "firebase/auth";
9-
import {auth} from "web/lib/firebase/users";
10-
import FavIcon from "web/public/FavIcon";
11-
import {PageBase} from "web/components/page-base";
12-
import {getProfileRow} from "common/profiles/profile";
13-
import {db} from "web/lib/supabase/db";
14-
import Router from "next/router";
15-
import {useUser} from "web/hooks/use-user";
16-
import {GoogleButton} from "web/components/buttons/sign-up-button";
17-
import {SEO} from "web/components/SEO";
8+
import {createUserWithEmailAndPassword} from "firebase/auth"
9+
import {auth} from "web/lib/firebase/users"
10+
import FavIcon from "web/public/FavIcon"
11+
import {PageBase} from "web/components/page-base"
12+
import {getProfileRow} from "common/profiles/profile"
13+
import {db} from "web/lib/supabase/db"
14+
import Router from "next/router"
15+
import {useUser} from "web/hooks/use-user"
16+
import {GoogleButton} from "web/components/buttons/sign-up-button"
17+
import {SEO} from "web/components/SEO"
18+
import toast from "react-hot-toast"
1819

1920

2021
export default function RegisterPage() {
2122
return (
2223
<Suspense fallback={<div></div>}>
2324
<RegisterComponent/>
2425
</Suspense>
25-
);
26+
)
2627
}
2728

28-
// const href = '/signup';
29+
// const href = '/signup'
2930

3031
function RegisterComponent() {
31-
const searchParams = useSearchParams();
32-
const [error, setError] = useState<string | null>(searchParams.get('error'));
33-
const [isLoading, setIsLoading] = useState(false);
34-
const [registrationSuccess, setRegistrationSuccess] = useState(false);
35-
const [registeredEmail, _] = useState('');
32+
const searchParams = useSearchParams()
33+
const [error, setError] = useState<string | null>(searchParams.get('error'))
34+
const [isLoading, setIsLoading] = useState(false)
35+
const [registrationSuccess, setRegistrationSuccess] = useState(false)
36+
const [registeredEmail, _] = useState('')
3637
const user = useUser()
3738

3839
// function redirect() {
3940
// // Redirect to complete profile page
40-
// window.location.href = href;
41+
// window.location.href = href
4142
// }
4243

4344
useEffect(() => {
@@ -51,62 +52,65 @@ function RegisterComponent() {
5152
console.log("Router.push('/signup')")
5253
await Router.push('/signup')
5354
}
54-
setIsLoading(false);
55+
setIsLoading(false)
5556
}
5657
}
5758
checkProfileAndRedirect()
58-
}, [user]);
59+
}, [user])
5960

6061
const handleEmailPasswordSignUp = async (email: string, password: string) => {
6162
try {
62-
const creds = await createUserWithEmailAndPassword(auth, email, password);
63-
console.debug("User signed up:", creds.user);
64-
} catch (error) {
65-
console.error("Error signing up:", error);
63+
const creds = await createUserWithEmailAndPassword(auth, email, password)
64+
console.debug("User signed up:", creds.user)
65+
} catch (error: any) {
66+
console.error("Error signing up:", error)
67+
toast.error("Failed to sign up: " + error.message)
68+
setError(error.message)
69+
setIsLoading(false)
6670
if (error instanceof Error && error.message.includes("email-already-in-use")) {
67-
throw new Error("This email is already registered");
71+
throw new Error("This email is already registered")
6872
}
6973
}
70-
};
74+
}
7175

7276
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
7377
function handleError(error: unknown) {
74-
console.error("Registration error:", error);
75-
setError(error instanceof Error ? error.message : "Registration failed");
78+
console.error("Registration error:", error)
79+
setError(error instanceof Error ? error.message : "Registration failed")
7680
}
7781

7882
try {
79-
event.preventDefault();
80-
setIsLoading(true);
81-
setError(null);
83+
event.preventDefault()
84+
setIsLoading(true)
85+
setError(null)
8286

83-
const formData = new FormData(event.currentTarget);
84-
const email = formData.get("email") as string;
85-
const password = formData.get("password") as string;
87+
const formData = new FormData(event.currentTarget)
88+
const email = formData.get("email") as string
89+
const password = formData.get("password") as string
8690

8791
// Basic validation
8892
if (!email || !password) {
89-
handleError("All fields are required");
93+
handleError("All fields are required")
9094
}
9195

92-
await handleEmailPasswordSignUp(email, password);
96+
await handleEmailPasswordSignUp(email, password)
9397

9498
// Show a success message with email verification notice
95-
// setRegistrationSuccess(true);
96-
// setRegisteredEmail(email);
99+
// setRegistrationSuccess(true)
100+
// setRegisteredEmail(email)
97101

98102
// Sign in after successful registration
99103
// ...
100104

101105
// if (response?.error) {
102-
// handleError("Failed to sign in after registration");
106+
// handleError("Failed to sign in after registration")
103107
// }
104108

105109
// redirect()
106110

107111
} catch (error) {
108-
handleError(error);
109-
setIsLoading(false);
112+
handleError(error)
113+
setIsLoading(false)
110114
}
111115
}
112116

@@ -258,5 +262,5 @@ function RegisterComponent() {
258262
</div>
259263
</div>
260264
</PageBase>
261-
);
265+
)
262266
}

0 commit comments

Comments
 (0)