Last active
August 8, 2022 04:10
-
-
Save DiegoGonzalezCruz/0f75b1548b6122da9fd2e78b257c9d40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This adapter only works with JWT. Sessions are not available. | |
// If you want to make it work, rewrite the logic to admin sdk for node. | |
import { adminDb } from "./firebaseAdmin" | |
export default function FirebaseAdapter(client, options = {}) { | |
const usersRef = adminDb.collection("users") | |
const accountsRef = adminDb.collection("accounts") | |
// const sessionsRef = adminDb.collection("sessions") | |
return { | |
async createUser(newUser) { | |
try { | |
// console.log('**********createUser', newUser) | |
const user = await usersRef.add(newUser) | |
return { ...newUser, id: user.id } | |
} catch (error) { | |
Sentry.captureEvent(error) | |
Sentry.captureMessage('error createUser') | |
} | |
}, | |
async getUser(id) { | |
try { | |
// console.log('**********getUser', id) | |
const userDoc = await usersRef.doc(id).get() | |
// console.log(userDoc.data(), '***userDoc.data()***') | |
if (userDoc.empty) | |
return null | |
return userDoc.data() | |
} catch (error) { | |
Sentry.captureEvent(error) | |
Sentry.captureMessage('error getUser') | |
} | |
}, | |
async getUserByEmail(email) { | |
try { | |
// console.log('**********getuserByEmail', email) | |
const userDocs = await usersRef.where("email", "==", email).limit(1).get() | |
if (!userDocs.empty) { | |
const userData = userDocs.docs[0].data() | |
return userData | |
} | |
return null | |
} catch (error) { | |
Sentry.captureEvent(error) | |
Sentry.captureMessage('error getUserByEmail') | |
} | |
}, | |
async getUserByAccount({ provider, providerAccountId }) { | |
try { | |
// console.log('******getuserbyAccount******') | |
const accountDocs = await accountsRef.where("provider", "==", provider).where("providerAccountId", "==", providerAccountId).limit(1).get() | |
if (!accountDocs.empty) { | |
const accountData = accountDocs.docs[0].data() | |
// console.log(accountData, '***accountData***') | |
// console.log('entrando if 1') | |
const { userId } = accountData | |
const userDoc = await usersRef.doc(userId).get() | |
const userData = userDoc.data() | |
// console.log(userData, '***userData***') | |
if (!userData.empty) { | |
// console.log('entrando if 2') | |
// console.log(userDoc.data(), '***userDoc.data()***') | |
return { ...userData, id: userId } | |
} | |
} | |
} catch (error) { | |
Sentry.captureEvent(error) | |
Sentry.captureMessage('get user by account') | |
} | |
}, | |
async updateUser(partialUser) { | |
try { | |
// console.log('**********updateUser', partialUser) | |
await usersRef.doc(partialUser.id).update(partialUser) | |
const userDoc = await usersRef.doc(partialUser.id).get() | |
return userDoc.data() | |
} catch (error) { | |
Sentry.captureEvent(error) | |
Sentry.captureMessage('error updateUser') | |
} | |
}, | |
async deleteUser(userId) { | |
// not implemented yet | |
// console.log('*** deleteUser not implemented yet ***') | |
}, | |
async linkAccount(account) { | |
try { | |
// console.log('**********linkAccount', account) | |
const accountRef = await accountsRef.add(account) | |
const accountSnapshot = await accountRef.get(accountRef.id) | |
if (accountSnapshot.exists) { | |
const accountData = accountSnapshot.data() | |
return accountData | |
} | |
} catch (error) { | |
Sentry.captureEvent(error) | |
Sentry.captureMessage('error linkAccount') | |
} | |
}, | |
async unlinkAccount({ provider, providerAccountId }) { | |
//not implemented yet | |
// console.log('*** unlinkAccount not implemented yet ***') | |
}, | |
async createSession(session) { | |
// console.log('**********createSession', session) | |
// const { id } = await sessionsRef.add(session) | |
// return { ...session, id } | |
}, | |
async getSessionAndUser(sessionToken) { | |
// console.log('**********getSessionAndUser', sessionToken) | |
// const sessionDocs = sessionsRef.where("sessionToken", "==", sessionToken).limit(1).get() | |
// if (sessionDocs.empty) | |
// return null | |
// const session = Sessions.converter.fromFirestore(sessionDocs.docs[0]) | |
// if (!session) | |
// return null | |
// const userDoc = await getDoc(doc(Users, session.userId)) | |
// if (!userDoc.exists()) | |
// return null | |
// const user = Users.converter.fromFirestore(userDoc) | |
// return { session, user } | |
}, | |
async updateSession(partialSession) { | |
// console.log('**********updateSession', partialSession) | |
// const sessionQuery = query(Sessions, where("sessionToken", "==", partialSession.sessionToken), limit(1)) | |
// const sessionDocs = await getDocs(sessionQuery) | |
// if (sessionDocs.empty) | |
// return null | |
// await updateDoc(sessionDocs.docs[0].ref, partialSession) | |
}, | |
async deleteSession(sessionToken) { | |
// console.log('**********deleteSession', sessionToken) | |
// const sessionQuery = query(Sessions, where("sessionToken", "==", sessionToken), limit(1)) | |
// const sessionDocs = await getDocs(sessionQuery) | |
// if (sessionDocs.empty) | |
// return | |
// await deleteDoc(sessionDocs.docs[0].ref) | |
}, | |
async createVerificationToken(verificationToken) { | |
// console.log('**********createVerificationToken', verificationToken) | |
// await addDoc(VerificationTokens, verificationToken) | |
// return verificationToken | |
}, | |
async useVerificationToken({ identifier, token }) { | |
// console.log('**********useVerificationToken', identifier, token) | |
// const verificationTokensQuery = query(VerificationTokens, where("identifier", "==", identifier), where("token", "==", token), limit(1)) | |
// const verificationTokenDocs = await getDocs(verificationTokensQuery) | |
// if (verificationTokenDocs.empty) | |
// return null | |
// await deleteDoc(verificationTokenDocs.docs[0].ref) | |
// const verificationToken = VerificationTokens.converter.fromFirestore(verificationTokenDocs.docs[0]) | |
// // @ts-expect-error | |
// delete verificationToken.id | |
// return verificationToken | |
}, | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment