philbert816
philbert8163mo ago

Convex Auth Delete Users

Is there documentations on how the users, authAccounts, authSessions etc. tables are created while using convex auth? I want to be able to delete user but not sure which tables and entries I should delete.
7 Replies
Convex Bot
Convex Bot3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Sean Aguinaga
Sean Aguinaga3mo ago
I need help with this too
erquhart
erquhart2mo ago
Never occurred to me that this isn't a part of the library Here's what I've used for this:
const [authSessions, authAccounts] = await Promise.all([
ctx.db
.query('authSessions')
.withIndex('userId', (q) => q.eq('userId', user._id))
.collect(),
ctx.db
.query('authAccounts')
.withIndex('userId', (q) => q.eq('userId', user._id))
.collect(),
])
const [authRefreshTokens, authVerificationCodes, authVerifiers] =
await Promise.all([
(
await asyncMap(authSessions, async (session) => {
return ctx.db
.query('authRefreshTokens')
.withIndex('sessionId', (q) => q.eq('sessionId', session._id))
.collect()
})
).flat(),
(
await asyncMap(authAccounts, async (account) => {
return ctx.db
.query('authVerificationCodes')
.withIndex('accountId', (q) => q.eq('accountId', account._id))
.collect()
})
).flat(),
(
await asyncMap(authSessions, async (session) => {
return ctx.db
.query('authVerifiers')
.withIndex('sessionId', (q) => q.eq('sessionId', session._id))
.collect()
})
).flat(),
])
await Promise.all([
asyncMap(authSessions, (session) => ctx.db.delete(session._id)),
asyncMap(authAccounts, (account) => ctx.db.delete(account._id)),
asyncMap(authRefreshTokens, (token) => ctx.db.delete(token._id)),
asyncMap(authVerificationCodes, (code) => ctx.db.delete(code._id)),
asyncMap(authVerifiers, (verifier) => ctx.db.delete(verifier._id)),
ctx.db.delete(user._id),
])
const [authSessions, authAccounts] = await Promise.all([
ctx.db
.query('authSessions')
.withIndex('userId', (q) => q.eq('userId', user._id))
.collect(),
ctx.db
.query('authAccounts')
.withIndex('userId', (q) => q.eq('userId', user._id))
.collect(),
])
const [authRefreshTokens, authVerificationCodes, authVerifiers] =
await Promise.all([
(
await asyncMap(authSessions, async (session) => {
return ctx.db
.query('authRefreshTokens')
.withIndex('sessionId', (q) => q.eq('sessionId', session._id))
.collect()
})
).flat(),
(
await asyncMap(authAccounts, async (account) => {
return ctx.db
.query('authVerificationCodes')
.withIndex('accountId', (q) => q.eq('accountId', account._id))
.collect()
})
).flat(),
(
await asyncMap(authSessions, async (session) => {
return ctx.db
.query('authVerifiers')
.withIndex('sessionId', (q) => q.eq('sessionId', session._id))
.collect()
})
).flat(),
])
await Promise.all([
asyncMap(authSessions, (session) => ctx.db.delete(session._id)),
asyncMap(authAccounts, (account) => ctx.db.delete(account._id)),
asyncMap(authRefreshTokens, (token) => ctx.db.delete(token._id)),
asyncMap(authVerificationCodes, (code) => ctx.db.delete(code._id)),
asyncMap(authVerifiers, (verifier) => ctx.db.delete(verifier._id)),
ctx.db.delete(user._id),
])
Sean Aguinaga
Sean Aguinaga2mo ago
It has some type errors?
No description
erquhart
erquhart2mo ago
hmmm I must have had a custom index Yeah I define the auth table schemas individually, must have added that index:
authSessions: authTables.authSessions,
authAccounts: authTables.authAccounts.index('userId', ['userId']),
authRefreshTokens: authTables.authRefreshTokens,
authVerificationCodes: authTables.authVerificationCodes,
authVerifiers: authTables.authVerifiers.index('sessionId', ['sessionId']),
authRateLimits: authTables.authRateLimits,
authSessions: authTables.authSessions,
authAccounts: authTables.authAccounts.index('userId', ['userId']),
authRefreshTokens: authTables.authRefreshTokens,
authVerificationCodes: authTables.authVerificationCodes,
authVerifiers: authTables.authVerifiers.index('sessionId', ['sessionId']),
authRateLimits: authTables.authRateLimits,
Sean Aguinaga
Sean Aguinaga2mo ago
how were you able to add that? ohhhh Thank you - works great
milton
milton4w ago
Thank you!

Did you find this page helpful?