Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// src/firebase/auth.js

import {
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
signOut,
onAuthStateChanged,} from 'firebase/auth'

Check failure on line 7 in new.js

View workflow job for this annotation

GitHub Actions / lint-code

Unable to resolve path to module 'firebase/auth'

Check failure on line 7 in new.js

View workflow job for this annotation

GitHub Actions / lint-code

Insert `⏎`
import { auth } from './firebase-init.js'

Check failure on line 8 in new.js

View workflow job for this annotation

GitHub Actions / lint-code

Unable to resolve path to module './firebase-init.js'

/**
* Creates a new user with email and password.
* @param {string} email
* @param {string} password
* @returns {Promise<User>} 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<User>} 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)
}
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading