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