Skip to content

Commit 495c8c7

Browse files
committed
chore(types): auth module
1 parent ec9a910 commit 495c8c7

1 file changed

Lines changed: 64 additions & 5 deletions

File tree

auth.cjs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import {
23
getAuth,
34
createUserWithEmailAndPassword,
@@ -31,15 +32,22 @@ const firestore = getFirestore(app)
3132

3233
const googleProvider = new GoogleAuthProvider()
3334

35+
/**
36+
* @param {string} email
37+
*/
3438
export const waitList = async (email) => {
3539
await addDoc(collection(firestore, 'waitList'), {
3640
email: email,
3741
createdAt: new Date().toDateString(),
3842
})
3943
}
4044

45+
/**
46+
* @param {any} user
47+
*/
4148
export const readTokens = async (user) => {
4249
const userRef = doc(firestore, 'users', user.uid)
50+
// @ts-ignore
4351
const userDoc = await userRef.get()
4452
const userData = userDoc.data()
4553
if (userData) {
@@ -48,10 +56,14 @@ export const readTokens = async (user) => {
4856
return null
4957
}
5058

59+
/**
60+
* @param {any} user
61+
*/
5162
export const getRealTimeToken = async (user) => {
5263
let value
5364
const tokenRef = doc(firestore, 'users', user.uid)
54-
await onSnapshot(tokenRef, (snapshot) => {
65+
onSnapshot(tokenRef, (snapshot) => {
66+
// @ts-ignore
5567
value = snapshot.data().tokens
5668
})
5769
return value
@@ -64,10 +76,13 @@ export const getRealTimeToken = async (user) => {
6476
// }
6577
}
6678

67-
export const generateRealTimeToken = (user) => {
79+
/**
80+
* @param {any} user
81+
*/
82+
export const generateRealTimeToken = async (user) => {
6883
// Get the current user's token
6984
if (!user) return null
70-
let token = getUserToken(user)
85+
let token = await getUserToken(user)
7186
const tokenRef = doc(firestore, 'users', user.uid)
7287
// Listen for changes to the token
7388
onSnapshot(tokenRef, (newToken) => {
@@ -81,29 +96,48 @@ export const generateRealTimeToken = (user) => {
8196
return token
8297
}
8398

99+
/**
100+
* @param {any} user
101+
*/
84102
export const getUserToken = async (user) => {
85103
if (!user) return null
86104

87105
const tokenRef = doc(firestore, 'users', user.uid)
88106
const doc1 = await getDoc(tokenRef)
89-
107+
// @ts-ignore
90108
if (doc1.exists) {
109+
// @ts-ignore
91110
return doc1.data().tokens
92111
} else {
93112
return null
94113
}
95114
}
96115

116+
/**
117+
* @param {any} user
118+
* @param {string} newTokenValue
119+
*/
120+
97121
export const updateTokens = async (user, newTokenValue) => {
98122
const userRef = doc(firestore, 'users', user.uid)
99123

100124
await updateDoc(userRef, { tokens: newTokenValue })
101125
}
102126

127+
/**
128+
* @param {any} user
129+
* @param {string} newModelValue
130+
*/
103131
export const updateModel = async (user, newModelValue) => {
104132
const userRef = doc(firestore, 'users', user.uid)
105133
await updateDoc(userRef, { model: newModelValue })
106134
}
135+
136+
/**
137+
* @param {any} user
138+
* @param {any} data
139+
* @param {string} platform
140+
*/
107141
export const addDraft = async (user, data, platform) => {
108142
const userRef = doc(db, 'users', user.uid)
109143
const newObject = { draft: data, platform: user }
@@ -127,25 +161,35 @@ export const addDraft = async (user, data, platform) => {
127161
alert('User document not found')
128162
}
129163
} catch (error) {
130-
alert('Error:', error)
164+
alert('Error occured')
131165
console.log(error)
132166
}
133167
}
134168

169+
/**
170+
* @param {string} email
171+
*/
135172
export const addToWaitList = async (email) => {
173+
// @ts-ignore
136174
db.collection('waitList')
137175
.add({
138176
email: email,
177+
// @ts-ignore
139178
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
140179
})
180+
// @ts-ignore
141181
.then(function (docRef) {
142182
console.log('Document written with ID: ', docRef.id)
143183
})
184+
// @ts-ignore
144185
.catch(function (error) {
145186
console.error('Error adding document: ', error)
146187
})
147188
}
148189

190+
/**
191+
* @param {any} user
192+
*/
149193
export const fetchUserDrafts = async (user) => {
150194
const userRef = doc(firestore, 'users', user.uid)
151195

@@ -160,10 +204,15 @@ export const fetchUserDrafts = async (user) => {
160204
throw new Error('User document not found')
161205
}
162206
} catch (error) {
207+
// @ts-ignore
163208
throw new Error('Error fetching drafts: ' + error.message)
164209
}
165210
}
166211

212+
/**
213+
* @param {string} email
214+
* @param {string} password
215+
*/
167216
export const createUserWithEmail = async (email, password) => {
168217
const { user } = await createUserWithEmailAndPassword(auth, email, password)
169218
if (user.email && user.uid) {
@@ -179,11 +228,16 @@ export const createUserWithEmail = async (email, password) => {
179228
}
180229
}
181230

231+
/**
232+
* @param {string} email
233+
* @param {string} password
234+
*/
182235
export const signInWithEmail = async (email, password) => {
183236
try {
184237
const { user } = await signInWithEmailAndPassword(auth, email, password)
185238
return user
186239
} catch (error) {
240+
// @ts-ignore
187241
if (error.code === 'auth/user-not-found') {
188242
throw new Error('User does not exist')
189243
} else {
@@ -226,7 +280,12 @@ export const signInWithGoogle = async () => {
226280
return user
227281
}
228282

283+
/**
284+
* @param {any} user
285+
* @param {any} router
286+
*/
229287
export const onUserSignedIn = async (user, router) => {
288+
// @ts-ignore
230289
const userDoc = await doc(firestore, 'users', user.uid).get()
231290
const isNewUser = userDoc.data().isNewUser
232291
if (isNewUser) {

0 commit comments

Comments
 (0)