Files
cally/hooks/firebase/useAuth.ts
2024-08-09 19:38:46 +02:00

153 lines
3.8 KiB
TypeScript

import { useState, useEffect } from "react";
import auth from "@react-native-firebase/auth";
import firestore from "@react-native-firebase/firestore";
import {
User,
CaregiverProfile,
ChildProfile,
ParentProfile,
} from "./types/profileTypes";
type ProfileType = "parent" | "child" | "caregiver" | null;
const useAuth = () => {
const [user, setUser] = useState<User | null>(null);
const [profileType, setProfileType] = useState<ProfileType>(null);
const [email, setEmail] = useState<string>("");
const [password, setPassword] = useState<string>("");
useEffect(() => {
const isDevelopment = __DEV__;
if (isDevelopment) {
firestore().useEmulator("localhost", 8080);
auth().useEmulator("http://127.0.0.1:9099");
}
const unsubscribe = auth().onAuthStateChanged(async (firebaseUser) => {
if (firebaseUser) {
const userObj: User = {
uid: firebaseUser.uid,
email: firebaseUser.email,
};
setUser(userObj);
try {
const documentSnapshot = await firestore()
.collection("Users")
.doc(firebaseUser.uid)
.get();
if (documentSnapshot.exists) {
setProfileType(documentSnapshot.data()?.profileType || null);
}
} catch (error) {
console.error("Error fetching user profile type:", error);
setProfileType(null);
}
} else {
setUser(null);
setProfileType(null);
}
});
return unsubscribe;
}, []);
const handleRegister = async () => {
try {
await auth().createUserWithEmailAndPassword(email, password);
console.log("user registered!");
await auth().signInWithEmailAndPassword(email, password);
console.log("user signed in!");
let profileData: ParentProfile | ChildProfile | CaregiverProfile;
switch (profileType) {
case "parent":
profileData = {
userType: "parent",
name: "",
childrenIds: [],
} as ParentProfile;
break;
case "child":
profileData = {
userType: "child",
name: "",
birthday: new Date(),
parentId: "X",
} as ChildProfile;
break;
case "caregiver":
profileData = {
userType: "caregiver",
name: "Nanny Nannersen",
contact: "5523123",
} as CaregiverProfile;
break;
default:
throw new Error("Invalid profile type");
}
const currentUser = auth().currentUser;
if (currentUser) {
await firestore()
.collection("Profiles")
.doc(currentUser.uid)
.set(profileData);
console.log("Profile document added!");
}
} catch (error) {
console.error("Error during registration: ", error);
}
};
const handleLogin = async () => {
try {
await auth().signInWithEmailAndPassword(email, password);
console.log("User signed in!");
} catch (error) {
console.error("Error during sign in:", error);
}
};
const handleSignOut = async () => {
try {
await auth().signOut();
console.log("User signed out!");
} catch (error) {
console.error("Error during sign out:", error);
}
};
const handleProfileTypeSelection = async (type: ProfileType) => {
if (user) {
setProfileType(type);
try {
await firestore()
.collection("Users")
.doc(user.uid)
.set({ profileType: type }, { merge: true });
} catch (error) {
console.error("Error saving profile type:", error);
}
}
};
return {
user,
profileType,
email,
setEmail,
password,
setPassword,
handleLogin,
handleSignOut,
handleProfileTypeSelection,
handleRegister,
};
};
export default useAuth;