Heath
Heath4mo ago

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 ?? '',
})
}
},
},
})
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))
~~~~~~~
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))
~~~~~~~
No description
2 Replies
sshader
sshader4mo ago
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
Heath
HeathOP4mo ago
@sshader thank you! that helped

Did you find this page helpful?