BrixCafe/firebase/auth.ts
2025-04-16 02:22:34 +01:00

50 lines
1.5 KiB
TypeScript

// auth.ts
import auth from '@react-native-firebase/auth'; // Importing Firebase Authentication from React Native Firebase
import { FirebaseAuthTypes } from '@react-native-firebase/auth';
// Function to handle user sign-in with email and password
export const signIn = async (email: string, password: string) => {
try {
const userCredential = await auth().signInWithEmailAndPassword(email, password);
return { user: userCredential.user }; // Return the user on success
} catch (error: any) {
throw new Error(error.message); // Throw an error if sign-in fails
}
};
// Function to handle user sign-up with email and password
export const signUp = async (email: string, password: string) => {
try {
const userCredential = await auth().createUserWithEmailAndPassword(email, password);
const user = userCredential.user;
console.log('User signed up:', user);
return user;
} catch (error) {
console.error('Error signing up:', error);
throw error;
}
};
// Function to handle user sign-out
export const signOutUser = async () => {
try {
await auth().signOut();
console.log('User signed out');
} catch (error) {
console.error('Error signing out:', error);
throw error;
}
};
// Listen for changes in authentication state
export const authStateListener = (callback: (user: FirebaseAuthTypes.User | null) => void) => {
return auth().onAuthStateChanged(user => {
if (user) {
console.log('User is authenticated:', user);
} else {
console.log('No user is signed in.');
}
callback(user);
});
};