updated Auth hook

This commit is contained in:
ivic00
2024-08-20 18:50:40 +02:00
parent 0b6199989b
commit 4a20f7a53e

View File

@ -1,4 +1,5 @@
import { useState, useEffect } from "react";
import { useMutation } from "react-query";
import auth from "@react-native-firebase/auth";
import firestore from "@react-native-firebase/firestore";
import {
@ -11,6 +12,25 @@ import {
type ProfileType = "parent" | "child" | "caregiver" | null;
const useAuth = () => {
const createUserMutation = useMutation(() =>
auth().createUserWithEmailAndPassword(email, password)
);
const signInMutation = useMutation(() =>
auth().signInWithEmailAndPassword(email, password)
);
const loginMutation = useMutation(() =>
auth().signInWithEmailAndPassword(email, password)
);
const signOutMutation = useMutation(() => auth().signOut());
/*const setProfileDataMutation = useMutation((profileData) => {
const currentUser = auth().currentUser;
if (currentUser) {
return firestore()
.collection("Profiles")
.doc(currentUser.uid)
.set(profileData);
}
});*/
const [user, setUser] = useState<User | null>(null);
const [profileType, setProfileType] = useState<ProfileType>(null);
const [email, setEmail] = useState<string>("");
@ -55,13 +75,13 @@ const useAuth = () => {
const handleRegister = async () => {
try {
await auth().createUserWithEmailAndPassword(email, password);
console.log("user registered!");
await createUserMutation.mutateAsync();
console.log("User registered!");
await auth().signInWithEmailAndPassword(email, password);
console.log("user signed in!");
await signInMutation.mutateAsync();
console.log("User signed in!");
let profileData: ParentProfile | ChildProfile | CaregiverProfile;
/*let profileData: ParentProfile | ChildProfile | CaregiverProfile;
switch (profileType) {
case "parent":
profileData = {
@ -96,7 +116,7 @@ const useAuth = () => {
.doc(currentUser.uid)
.set(profileData);
console.log("Profile document added!");
}
}*/
} catch (error) {
console.error("Error during registration: ", error);
}
@ -104,7 +124,7 @@ const useAuth = () => {
const handleLogin = async () => {
try {
await auth().signInWithEmailAndPassword(email, password);
await loginMutation.mutateAsync();
console.log("User signed in!");
} catch (error) {
console.error("Error during sign in:", error);
@ -113,7 +133,7 @@ const useAuth = () => {
const handleSignOut = async () => {
try {
await auth().signOut();
await signOutMutation.mutateAsync();
console.log("User signed out!");
} catch (error) {
console.error("Error during sign out:", error);