I am using firebase auth for authenticating users.
Current setup for my app is, I have a condition in my getCurrentUser
that checks if the user’s UID already exists in my postgres db.
in api/src/lib/auth.ts
export const getCurrentUser = async (
decoded,
/* eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars */
{ token, type },
/* eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars */
{ event, context }
) => {
const { uid } = decoded
// check if user id already exists in db
// create user if user id does not exists
return decoded
}
This works perfectly fine, I’m just wondering if this is the best way to approach creating users in my database. I’m thinking I might be doing unnecessary reads to check if the user exists in the database every time getCurrentUser
is called.
any help, insights, or suggestions would be much appreciated, Thanks and have a great day!
Hi @redwoodjsuser123 my question is why do you need a User record in a database if you already are creating and storing that info in Firebase — that’s what its purpose is.
It can store all the user info.
You can still use its user id to relate records.
That said, instead I might create a Profile record in your database that has an id and the Firebase user id. And I would create the Profile record via a webhook from Firebase on certain user creation events. The user id on Profile would be unique.
Then fetch the Profile and update it by querying by its userId. You can include personal info like email
Address and other user preferences there.
Hi, thanks a lot for the reply!
my question is why do you need a User record in a database if you already are creating and storing that info in Firebase — that’s what its purpose is.
I made a user record in postgres mainly for better searching. In my application, I am supporting user queries by an array of strings. e.g. [“jiujitsu”, “mma”, “karate”]
users are queried by each value in the array, using prisma’s contains
operator.
my approach:
my tables:
model User {
id String @id // same as id in firebase
name String?
UserSearchKeywords UserSearchKeywords[]
}
model UserSearchKeywords {
id Int @id @default(autoincrement())
User User @relation(fields: [userId], references: [id])
userId String
keyword String
@@index([keyword, userId])
}
in users.ts
export const getusersByKeywords: QueryResolvers['getusersByKeywords'] = async ({ input }) => {
const { keywords } = input
return db.user.findMany({
where: {
UserSearchKeywords: {
some: {
OR: keywords.map((k) => ({ keyword: { contains: k } })),
},
},
},
})
}
from reviewing firestore’s docs and yt tutorials, I don’t think they support querying field values using contains
. is there a way to implement these kinds of queries ( using contains
operators ) in firestore?
That said, instead I might create a Profile record in your database that has an id and the Firebase user id. And I would create the Profile record via a webhook from Firebase on certain user creation events. The user id on Profile would be unique.
I see, webhooks sounds like a better approach for this. thanks!