diff --git a/new.js b/new.js new file mode 100644 index 000000000000..a8d8884f01ad --- /dev/null +++ b/new.js @@ -0,0 +1,58 @@ +// src/firebase/auth.js + +import { + createUserWithEmailAndPassword, + signInWithEmailAndPassword, + signOut, + onAuthStateChanged,} from 'firebase/auth' +import { auth } from './firebase-init.js' + +/** + * Creates a new user with email and password. + * @param {string} email + * @param {string} password + * @returns {Promise} The created user object. + */ +export async function signUp(email, password) { + try { + const userCredential = await createUserWithEmailAndPassword(auth, email, password) + console.log('Successfully created user:', userCredential.user.uid) + return userCredential.user + } catch (error) { + console.error('Error signing up:', error.message) + throw error // Re-throw the error to be handled by the caller + } +} + +/** + * Signs in an existing user. + * @param {string} email + * @param {string} password + * @returns {Promise} The signed-in user object. + */ +export async function signIn(email, password) { + try { + const userCredential = await signInWithEmailAndPassword(auth, email, password) + console.log('Successfully signed in:', userCredential.user.uid) + return userCredential.user + } catch (error) { + console.error('Error signing in:', error.message) + throw error + } +} + +/** + * Signs out the current user. + */ +export function logOut() { + return signOut(auth) +} + +/** + * Listens for changes in the user's authentication state. + * @param {function} callback - A function to call with the user object (or null). + * @returns {function} An unsubscribe function. + */ +export function onAuthStateChange(callback) { + return onAuthStateChanged(auth, callback) +} diff --git a/package-lock.json b/package-lock.json index b7b29160c194..2637d09a5ddb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16079,6 +16079,9 @@ "license": "MIT" }, "node_modules/vite": { + "version": "6.2.5", + "resolved": "https://registry.npmjs.org/vite/-/vite-6.2.5.tgz", + "integrity": "sha512-j023J/hCAa4pRIUH6J9HemwYfjB5llR2Ps0CWeikOtdR8+pAURAk0DoJC5/mm9kd+UgdnIy7d6HE4EAvlYhPhA==", "version": "6.3.6", "resolved": "https://registry.npmjs.org/vite/-/vite-6.3.6.tgz", "integrity": "sha512-0msEVHJEScQbhkbVTb/4iHZdJ6SXp/AvxL2sjwYQFfBqleHtnCqv1J3sa9zbWz/6kW1m9Tfzn92vW+kZ1WV6QA==",