|
| 1 | +import { |
| 2 | + getAuth, |
| 3 | + createUserWithEmailAndPassword, |
| 4 | + signInWithEmailAndPassword, |
| 5 | + signInWithPopup, |
| 6 | + GoogleAuthProvider, |
| 7 | + signOut, |
| 8 | +} from 'firebase/auth' |
| 9 | +import { |
| 10 | + getFirestore, |
| 11 | + doc, |
| 12 | + setDoc, |
| 13 | + getDoc, |
| 14 | + getDocs, |
| 15 | + onSnapshot, |
| 16 | + collection, |
| 17 | + query, |
| 18 | + where, |
| 19 | + arrayUnion, |
| 20 | + addDoc, |
| 21 | +} from 'firebase/firestore' |
| 22 | +import { db } from './firebase' |
| 23 | +import { updateDoc } from 'firebase/firestore' |
| 24 | +import Alert from '@mui/material/Alert' |
| 25 | + |
| 26 | +// import { db } from './firebaseConfig'; |
| 27 | +import { app } from './firebaseConfig' |
| 28 | +const auth = getAuth(app) |
| 29 | + |
| 30 | +const firestore = getFirestore(app) |
| 31 | + |
| 32 | +const googleProvider = new GoogleAuthProvider() |
| 33 | + |
| 34 | +export const waitList = async (email) => { |
| 35 | + await addDoc(collection(firestore, 'waitList'), { |
| 36 | + email: email, |
| 37 | + createdAt: new Date().toDateString(), |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +export const readTokens = async (user) => { |
| 42 | + const userRef = doc(firestore, 'users', user.uid) |
| 43 | + const userDoc = await userRef.get() |
| 44 | + const userData = userDoc.data() |
| 45 | + if (userData) { |
| 46 | + return userData.tokens |
| 47 | + } |
| 48 | + return null |
| 49 | +} |
| 50 | + |
| 51 | +export const getRealTimeToken = async (user) => { |
| 52 | + let value |
| 53 | + const tokenRef = doc(firestore, 'users', user.uid) |
| 54 | + await onSnapshot(tokenRef, (snapshot) => { |
| 55 | + value = snapshot.data().tokens |
| 56 | + }) |
| 57 | + return value |
| 58 | + |
| 59 | + // if (doc1.exists) { |
| 60 | + // console.log("this is real time data "+doc1.data().tokens) |
| 61 | + // return doc1 |
| 62 | + // } else { |
| 63 | + // return null; |
| 64 | + // } |
| 65 | +} |
| 66 | + |
| 67 | +export const generateRealTimeToken = (user) => { |
| 68 | + // Get the current user's token |
| 69 | + if (!user) return null |
| 70 | + let token = getUserToken(user) |
| 71 | + const tokenRef = doc(firestore, 'users', user.uid) |
| 72 | + // Listen for changes to the token |
| 73 | + onSnapshot(tokenRef, (newToken) => { |
| 74 | + // Update the token if it has changed |
| 75 | + if (newToken !== token) { |
| 76 | + token = newToken |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + // Return the current token |
| 81 | + return token |
| 82 | +} |
| 83 | + |
| 84 | +export const getUserToken = async (user) => { |
| 85 | + if (!user) return null |
| 86 | + |
| 87 | + const tokenRef = doc(firestore, 'users', user.uid) |
| 88 | + const doc1 = await getDoc(tokenRef) |
| 89 | + |
| 90 | + if (doc1.exists) { |
| 91 | + return doc1.data().tokens |
| 92 | + } else { |
| 93 | + return null |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export const updateTokens = async (user, newTokenValue) => { |
| 98 | + const userRef = doc(firestore, 'users', user.uid) |
| 99 | + |
| 100 | + await updateDoc(userRef, { tokens: newTokenValue }) |
| 101 | +} |
| 102 | + |
| 103 | +export const updateModel = async (user, newModelValue) => { |
| 104 | + const userRef = doc(firestore, 'users', user.uid) |
| 105 | + await updateDoc(userRef, { model: newModelValue }) |
| 106 | +} |
| 107 | +export const addDraft = async (user, data, platform) => { |
| 108 | + const userRef = doc(db, 'users', user.uid) |
| 109 | + const newObject = { draft: data, platform: user } |
| 110 | + const CurrentDate = new Date() |
| 111 | + |
| 112 | + try { |
| 113 | + const userDoc = await getDoc(userRef) |
| 114 | + |
| 115 | + if (userDoc.exists()) { |
| 116 | + await updateDoc(userRef, { |
| 117 | + draft: arrayUnion({ |
| 118 | + draft: data, |
| 119 | + platform: platform, |
| 120 | + date: CurrentDate, |
| 121 | + }), |
| 122 | + }) |
| 123 | + ;<Alert severity="success"> |
| 124 | + This is a success alert — check it out! |
| 125 | + </Alert> |
| 126 | + } else { |
| 127 | + alert('User document not found') |
| 128 | + } |
| 129 | + } catch (error) { |
| 130 | + alert('Error:', error) |
| 131 | + console.log(error) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +export const addToWaitList = async (email) => { |
| 136 | + db.collection('waitList') |
| 137 | + .add({ |
| 138 | + email: email, |
| 139 | + timestamp: firebase.firestore.FieldValue.serverTimestamp(), |
| 140 | + }) |
| 141 | + .then(function (docRef) { |
| 142 | + console.log('Document written with ID: ', docRef.id) |
| 143 | + }) |
| 144 | + .catch(function (error) { |
| 145 | + console.error('Error adding document: ', error) |
| 146 | + }) |
| 147 | +} |
| 148 | + |
| 149 | +export const fetchUserDrafts = async (user) => { |
| 150 | + const userRef = doc(firestore, 'users', user.uid) |
| 151 | + |
| 152 | + try { |
| 153 | + const docSnap = await getDoc(userRef) |
| 154 | + |
| 155 | + if (docSnap.exists()) { |
| 156 | + const userData = docSnap.data() |
| 157 | + const userDrafts = userData.draft || [] |
| 158 | + return userDrafts |
| 159 | + } else { |
| 160 | + throw new Error('User document not found') |
| 161 | + } |
| 162 | + } catch (error) { |
| 163 | + throw new Error('Error fetching drafts: ' + error.message) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +export const createUserWithEmail = async (email, password) => { |
| 168 | + const { user } = await createUserWithEmailAndPassword(auth, email, password) |
| 169 | + if (user.email && user.uid) { |
| 170 | + const userData = { |
| 171 | + email: user.email, |
| 172 | + tokens: 100, |
| 173 | + model: 'text-davinci-002', |
| 174 | + isNewUser: true, |
| 175 | + uid: user.uid, |
| 176 | + drafts: [], |
| 177 | + } |
| 178 | + await setDoc(doc(firestore, 'users', user.uid), userData) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +export const signInWithEmail = async (email, password) => { |
| 183 | + try { |
| 184 | + const { user } = await signInWithEmailAndPassword(auth, email, password) |
| 185 | + return user |
| 186 | + } catch (error) { |
| 187 | + if (error.code === 'auth/user-not-found') { |
| 188 | + throw new Error('User does not exist') |
| 189 | + } else { |
| 190 | + throw error |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +export const Logout = async () => { |
| 196 | + try { |
| 197 | + await signOut(auth) |
| 198 | + } catch (err) { |
| 199 | + console.error(err) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +export const signInWithGoogle = async () => { |
| 204 | + const googleProvider = new GoogleAuthProvider() |
| 205 | + const { user } = await signInWithPopup(auth, googleProvider) |
| 206 | + |
| 207 | + const userRef = collection(db, 'users') |
| 208 | + const q = query(userRef, where('email', '==', user.email)) |
| 209 | + |
| 210 | + const querySnapshot = await getDocs(q) |
| 211 | + let checkUser = [] |
| 212 | + querySnapshot.forEach((doc) => { |
| 213 | + checkUser.push({ id: doc.id, ...doc.data() }) |
| 214 | + }) |
| 215 | + |
| 216 | + if (checkUser.length > 0) return |
| 217 | + |
| 218 | + const userData = { |
| 219 | + email: user.email, |
| 220 | + tokens: 100, |
| 221 | + model: 'text-davinci-002', |
| 222 | + isNewUser: true, |
| 223 | + uid: user.uid, |
| 224 | + } |
| 225 | + await setDoc(doc(firestore, 'users', user.uid), userData, { merge: true }) |
| 226 | + return user |
| 227 | +} |
| 228 | + |
| 229 | +export const onUserSignedIn = async (user, router) => { |
| 230 | + const userDoc = await doc(firestore, 'users', user.uid).get() |
| 231 | + const isNewUser = userDoc.data().isNewUser |
| 232 | + if (isNewUser) { |
| 233 | + router.push('/') |
| 234 | + } else { |
| 235 | + router.push('/') |
| 236 | + } |
| 237 | +} |
0 commit comments