Skip to content

Commit d5f9022

Browse files
committed
feat: Locked down the account creation process..
1 parent d59d492 commit d5f9022

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

app/auth/login/page.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
GoogleAuthProvider,
77
signInWithPopup,
88
signOut,
9+
deleteUser,
910
} from "firebase/auth";
10-
import { doc, getDoc, setDoc, serverTimestamp } from "firebase/firestore";
11+
import { doc, getDoc } from "firebase/firestore";
1112
import { auth, db } from "@/lib/firebase";
1213
import { useRouter } from "next/navigation";
1314
import { FaEye, FaEyeSlash } from "react-icons/fa";
@@ -30,7 +31,21 @@ export default function LoginPage() {
3031
setError("");
3132

3233
try {
33-
await signInWithEmailAndPassword(auth, email, password);
34+
const result = await signInWithEmailAndPassword(auth, email, password);
35+
const user = result.user;
36+
37+
// Check if user exists in 'authors' collection
38+
const userDocRef = doc(db, "authors", user.uid);
39+
const userDoc = await getDoc(userDocRef);
40+
41+
if (!userDoc.exists()) {
42+
// User is not authorized, delete from Auth if they manage to log in without a profile
43+
await deleteUser(user);
44+
await signOut(auth);
45+
setError("You do not have access to this site.");
46+
return;
47+
}
48+
3449
router.push("/admin");
3550
} catch (err: any) {
3651
handleAuthError(err);
@@ -53,7 +68,8 @@ export default function LoginPage() {
5368
const userDoc = await getDoc(userDocRef);
5469

5570
if (!userDoc.exists()) {
56-
// User is not authorized, prevent account creation
71+
// User is not authorized, prevent account creation by deleting the auth profile
72+
await deleteUser(user);
5773
await signOut(auth);
5874
setError("You do not have access to this site.");
5975
return;

0 commit comments

Comments
 (0)