forked from vaibhav45sktech/crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.js
More file actions
78 lines (68 loc) · 2.42 KB
/
Copy pathfirebase.js
File metadata and controls
78 lines (68 loc) · 2.42 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
import { initializeApp } from 'firebase/app'
import { getAuth, GoogleAuthProvider, signInWithPopup, signOut, onAuthStateChanged, createUserWithEmailAndPassword, signInWithEmailAndPassword } from 'firebase/auth'
import { getFirestore, doc, getDoc, setDoc, collection, getDocs } from 'firebase/firestore'
import { useEffect, useState } from 'react'
const firebaseConfig = {
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
appId: import.meta.env.VITE_FIREBASE_APP_ID
}
const enabled = Boolean(firebaseConfig.apiKey && firebaseConfig.projectId)
let app
let authInst
let dbInst
if (enabled) {
app = initializeApp(firebaseConfig)
authInst = getAuth(app)
dbInst = getFirestore(app)
}
export const auth = authInst
export const db = dbInst
export const firebaseEnabled = enabled
export async function signInWithGoogle() {
if (!enabled) return
const provider = new GoogleAuthProvider()
await signInWithPopup(auth, provider)
}
export async function signOutUser() {
if (!enabled) return
await signOut(auth)
}
export function useAuth() {
const [user, setUser] = useState(null)
useEffect(() => {
if (!enabled) return
return onAuthStateChanged(auth, setUser)
}, [])
return user
}
export async function loadPortfolio(uid) {
if (!enabled) return { positions: [] }
const ref = doc(db, 'portfolios', uid)
const snap = await getDoc(ref)
return snap.exists() ? snap.data() : { positions: [] }
}
export async function savePortfolio(uid, data) {
if (!enabled) return
const ref = doc(db, 'portfolios', uid)
await setDoc(ref, data, { merge: true })
}
export async function listPortfolios() {
if (!enabled) return []
const col = collection(db, 'portfolios')
const snaps = await getDocs(col)
return snaps.docs.map(d => ({ id: d.id, ...d.data() }))
}
export async function signUpWithEmail(email, password) {
if (!enabled) return null
const cred = await createUserWithEmailAndPassword(auth, email, password)
return cred.user
}
export async function signInWithEmail(email, password) {
if (!enabled) return null
const cred = await signInWithEmailAndPassword(auth, email, password)
return cred.user
}