forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.ts
More file actions
82 lines (74 loc) · 2.26 KB
/
Copy pathfirebase.ts
File metadata and controls
82 lines (74 loc) · 2.26 KB
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
// Import the functions you need from the SDKs you need
import { initializeApp } from 'firebase/app';
import {
getAuth,
GoogleAuthProvider,
signInWithPopup,
signOut,
onAuthStateChanged,
User,
} from 'firebase/auth';
// Firebase configuration
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase Auth and get a reference to the service
export const auth = getAuth(app);
// Initialize Google Auth Provider
const googleProvider = new GoogleAuthProvider();
googleProvider.setCustomParameters({
prompt: 'select_account',
});
// Auth functions
export const signInWithGoogle = async (): Promise<User | null> => {
try {
console.log('🔑 Initiating Google sign-in...');
const result = await signInWithPopup(auth, googleProvider);
console.log('✅ Google sign-in successful:', {
uid: result.user.uid,
email: result.user.email,
displayName: result.user.displayName,
});
return result.user;
} catch (error) {
if (error instanceof Error) {
console.error('❌ Google sign-in error:', error.message);
}
throw error;
}
};
export const signOutUser = async (): Promise<void> => {
try {
console.log('🚪 Signing out user...');
await signOut(auth);
console.log('✅ User signed out successfully');
} catch (error) {
if (error instanceof Error) {
console.error('❌ Sign out error:', error.message);
}
throw error;
}
};
export const onAuthStateChange = (callback: (user: User | null) => void) => {
return onAuthStateChanged(auth, (user) => {
if (user) {
console.log('👤 User authenticated:', {
uid: user.uid,
email: user.email,
displayName: user.displayName,
});
} else {
console.log('🚫 User not authenticated');
}
callback(user);
});
};
export default app;