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

2 messages · Page 1 of 1 (latest)

proven vale
#

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))
                                        ~~~~~~~
wet estuary
#

Agree this looks like a bug with the types.

We've currently typed it so the ctx / db in those callbacks doesn't know about your schema. To work around this, I'd add a type annotation ctx: MutationCtx (where MutationCtx comes from your _generated/server file) to the first argument of those callbacks