Merge pull request 'fix(auth): allow admins to create accounts when sign-ups are disabled' (#27) from t3code/admin-create-users into main
This commit was merged in pull request #27.
This commit is contained in:
commit
5dc8ca9b0e
3 files changed
+31
-19
No files matched your search
@@ -462,7 +462,7 @@ function UsersSection({
|
||||
<SettingsCard title="Sign Up">
|
||||
<SwitchRow
|
||||
title="Disable account creation"
|
||||
subtitle="Prevent new users from creating accounts."
|
||||
subtitle="Stop people from registering themselves. Admins can still create accounts from the web app."
|
||||
value={appSettings?.disableAccountCreation ?? false}
|
||||
onValueChange={(checked) =>
|
||||
void setDisableCreation({ disabled: checked }).catch(
|
||||
|
||||
@@ -848,9 +848,15 @@ function UsersSection({
|
||||
.catch((error: Error) => toast.error(error.message))
|
||||
}}
|
||||
/>
|
||||
<Label htmlFor="disable-signups" className="font-normal">
|
||||
Disable account creation
|
||||
</Label>
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="disable-signups" className="font-normal">
|
||||
Disable account creation
|
||||
</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Stops people from registering themselves. You can still create
|
||||
accounts.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Button type="button" size="sm" onClick={() => setCreateOpen(true)}>
|
||||
<UserPlus />
|
||||
|
||||
@@ -21,22 +21,28 @@ export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
|
||||
if (existingUserId === null) {
|
||||
const settings = await db.query("appSettings").first()
|
||||
if (settings?.disableAccountCreation) {
|
||||
const sessions = await db
|
||||
.query("authSessions")
|
||||
.withIndex("userId", (q) => q.eq("userId", userId))
|
||||
.collect()
|
||||
for (const session of sessions) {
|
||||
await db.delete(session._id)
|
||||
// Self-service sign-up has no admin session. Accounts created from
|
||||
// the admin tools run as that admin, so they stay allowed.
|
||||
const actorId = await getAuthUserId(ctx)
|
||||
const actor = actorId === null ? null : await db.get(actorId)
|
||||
if (!actor?.isAdmin) {
|
||||
const sessions = await db
|
||||
.query("authSessions")
|
||||
.withIndex("userId", (q) => q.eq("userId", userId))
|
||||
.collect()
|
||||
for (const session of sessions) {
|
||||
await db.delete(session._id)
|
||||
}
|
||||
const accounts = await db
|
||||
.query("authAccounts")
|
||||
.withIndex("userIdAndProvider", (q) => q.eq("userId", userId))
|
||||
.collect()
|
||||
for (const account of accounts) {
|
||||
await db.delete(account._id)
|
||||
}
|
||||
await db.delete(userId)
|
||||
throw new ConvexError("Account creation is currently disabled")
|
||||
}
|
||||
const accounts = await db
|
||||
.query("authAccounts")
|
||||
.withIndex("userIdAndProvider", (q) => q.eq("userId", userId))
|
||||
.collect()
|
||||
for (const account of accounts) {
|
||||
await db.delete(account._id)
|
||||
}
|
||||
await db.delete(userId)
|
||||
throw new ConvexError("Account creation is currently disabled")
|
||||
}
|
||||
|
||||
const users = await db.query("users").take(2)
|
||||
|
||||
Reference in new issue
Block a user