File size: 2,894 Bytes
0a08a10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Firebase configuration
const firebaseConfig = {
  apiKey: "AIzaSyD4iOeFtD-hETA5-8qWhWh9wRk-PNdhVKj-E",
  authDomain: "fraudtest-23c54.firebaseapp.com",
  projectId: "fraudtest-23c54",
  storageBucket: "fraudtest-23c54.appspot.com",
  messagingSenderId: "265409594914",
  appId: "1:265409594914:web:e199246e50428971c32c7b"
};

// Initialize Firebase
if (!firebase.apps.length) {
  firebase.initializeApp(firebaseConfig);
}

// Firebase authentication service
const auth = firebase.auth();

// Firebase Auth library with utility functions
const firebaseAuth = {
  // Check authentication state
  checkAuthState: function(callback) {
    auth.onAuthStateChanged(callback);
  },
  
  // Sign up with email and password
  signUpWithEmail: function(email, password) {
    return auth.createUserWithEmailAndPassword(email, password);
  },
  
  // Alias for signUpWithEmail to match template calls
  signUp: function(email, password) {
    return this.signUpWithEmail(email, password);
  },
  
  // Sign in with email and password
  signInWithEmail: function(email, password) {
    return auth.signInWithEmailAndPassword(email, password);
  },
  
  // Alias for signInWithEmail to match template calls
  signIn: function(email, password) {
    return this.signInWithEmail(email, password);
  },
  
  // Sign in with Google
  signInWithGoogle: function() {
    const provider = new firebase.auth.GoogleAuthProvider();
    return auth.signInWithPopup(provider);
  },
  
  // Sign out
  signOut: function() {
    return auth.signOut();
  },
  
  // Reset password
  resetPassword: function(email) {
    return auth.sendPasswordResetEmail(email);
  },
  
  // Update user profile - Fixed to handle either string or object
  updateProfile: function(displayNameOrProfileObj) {
    const user = auth.currentUser;
    if (user) {
      // If it's just a string, it's the display name
      if (typeof displayNameOrProfileObj === 'string') {
        return user.updateProfile({
          displayName: displayNameOrProfileObj
        });
      }
      // Otherwise it's a profile object
      return user.updateProfile(displayNameOrProfileObj);
    }
    return Promise.reject(new Error('No user is signed in'));
  },
  
  // Send email verification
  sendEmailVerification: function() {
    const user = auth.currentUser;
    if (user) {
      return user.sendEmailVerification();
    }
    return Promise.reject(new Error('No user is signed in'));
  },
  
  // Get current user
  getCurrentUser: function() {
    return auth.currentUser;
  },
  
  // Get user token
  getUserToken: async function() {
    const user = auth.currentUser;
    if (user) {
      return await user.getIdToken();
    }
    return null;
  }
};

// Add global object for backend authentication
window.firebaseAuth = firebaseAuth;