HeathH
Convex Community17mo ago
2 replies
Heath

Auth Callbacks cannot use indexes when making a db.query()

I am attempting to use an auth callback to populate a table after user creation. This works, however, I need to query this table first to avoid creating duplicate records. Typescript does not accept any queries that include a withIndex(). Unclear whether I am doing something wrong or not?

Example:
export const { auth, signIn, signOut, store } = convexAuth({
    providers: [resend],
    callbacks: {
        async createOrUpdateUser({ db }, { profile }) {
            // Tests
            await db.query('users')
            await db.query('users').withIndex('email', (q) => q.eq('email', profile?.email))
        },
        async afterUserCreatedOrUpdated({ db }, { userId, profile }) {
            // Tests
            await db.query('users')
            await db.query('users').withIndex('email', (q) => q.eq('email', profile?.email))

            // What I'm actually trying to do
            const provider = await getOneFrom(db, 'providers', 'userId', userId)

            if (!provider) {
                await db.insert('providers', {
                    userId,
                    name: profile.name ?? '',
                })
            }
        },
    },
})


Output:
13   async createOrUpdateUser({ db }, { profile }) {
           ~~~~~~~~~~~~~~~~~~
convex/auth.ts:16:38 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.

16    await db.query('users').withIndex('email', (q) => q.eq('email', profile?.email))
                                        ~~~~~~~
Screenshot_2024-09-06_at_3.08.37_PM.png
Was this page helpful?