@serge-ivo/firestore-client
Version:
A Firestore data management module
99 lines (98 loc) • 3.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthService = void 0;
// src/AuthService.ts
const auth_1 = require("firebase/auth");
class AuthService {
/**
* Creates an instance of AuthService.
* @param {FirebaseApp} app - The initialized Firebase App instance.
* @throws Error if the FirebaseApp instance is not provided.
*/
constructor(app) {
if (!app) {
throw new Error("FirebaseApp instance is required for AuthService constructor");
}
this.auth = (0, auth_1.getAuth)(app);
this.googleProvider = new auth_1.GoogleAuthProvider(); // Initialize Google Provider
console.log("AuthService instance created successfully.");
}
/**
* Gets the underlying Firebase Auth instance.
* @returns {Auth} The Firebase Auth instance.
*/
getFirebaseAuth() {
return this.auth;
}
/**
* Gets the currently signed-in user.
* @returns {User | null} The current user object or null if not signed in.
*/
getCurrentUser() {
return this.auth.currentUser;
}
/**
* Gets the ID of the currently signed-in user.
* @returns {string | null} The current user's ID or null if not signed in.
*/
getCurrentUserId() {
var _a, _b;
return (_b = (_a = this.auth.currentUser) === null || _a === void 0 ? void 0 : _a.uid) !== null && _b !== void 0 ? _b : null;
}
/**
* Signs in a user with email and password.
* @param {string} email - The user's email.
* @param {string} password - The user's password.
* @returns {Promise<UserCredential>} A promise resolving to the user credential.
*/
async signInWithEmailPassword(email, password) {
try {
return await (0, auth_1.signInWithEmailAndPassword)(this.auth, email, password);
}
catch (error) {
console.error("Error signing in with email/password:", error);
// Re-throw the error for the caller to handle
throw error;
}
}
/**
* Initiates sign-in with Google using a popup.
* @returns {Promise<UserCredential>} A promise resolving to the user credential.
*/
async signInWithGoogle() {
try {
// Consider adding custom parameters or scopes if needed later
// this.googleProvider.addScope('profile');
// this.googleProvider.setCustomParameters({ login_hint: 'user@example.com' });
return await (0, auth_1.signInWithPopup)(this.auth, this.googleProvider);
}
catch (error) {
console.error("Error signing in with Google:", error);
// Handle specific error codes if necessary (e.g., popup closed)
// if (error.code === 'auth/popup-closed-by-user') { ... }
throw error;
}
}
/**
* Signs out the current user.
* @returns {Promise<void>}
*/
async signOut() {
try {
await (0, auth_1.signOut)(this.auth);
}
catch (error) {
console.error("Error signing out:", error);
throw error;
}
}
/**
* Subscribes to changes in the user's authentication state.
* @param {(user: User | null) => void} callback - Function to call when the auth state changes.
* @returns {Unsubscribe} A function to unsubscribe from the listener.
*/
onAuthStateChanged(callback) {
return (0, auth_1.onAuthStateChanged)(this.auth, callback);
}
}
exports.AuthService = AuthService;