cameronm
cameronm4mo ago

auth:store signUp Error with duplicate email

I am attempting to handle cases where a user attempts to create an account with an email that already exists in the users table Based on docs it seems I am suppose to use createOrUpdateUser callback in auth.ts My issue is that auth:store is throwing an error before this callback is reached. auth:store failure 314ms Uncaught Error: Account <someexistingemail@email.com> already exists at handler (../../../../node_modules/.pnpm/@convex-dev+auth@0.0.61_convex@1.14.4/node_modules/@convex-dev/auth/dist/server/implementation.js:601:28)
1 Reply
cameronm
cameronmOP4mo ago
My auth.ts file isn't overly complex. I am just setting this up as a basic test. I know the callback isn't being executed because my log stream never shows the "Caught" log in the catch block.
import { Password } from "@convex-dev/auth/providers/Password";
import { Anonymous } from "@convex-dev/auth/providers/Anonymous";
import { convexAuth } from "@convex-dev/auth/server";
import { DataModel } from "./_generated/dataModel";

const CustomPassword = Password<DataModel>({
profile(params) {
return {
email: params.email as string,
first_name: params.first_name as string,
last_name: params.last_name as string,
full_name: params.full_name as string,
};
},
});

export const { auth, signIn, signOut, store } = convexAuth({
providers: [Anonymous, CustomPassword],
callbacks: {
async createOrUpdateUser(ctx, args) {
try {
console.log(args.existingUserId);
if (args.existingUserId) {
// The user already exists, return the existing user ID
return args.existingUserId;
}

// The user does not exist yet, create a new user
const newUser = await ctx.db.insert("users", {
email: args.profile.email,
first_name: args.profile.first_name,
last_name: args.profile.last_name,
full_name: args.profile.full_name,
});

// Return the new user ID
return newUser;
} catch (e) {
console.log("Caught");
}
},
},
});
import { Password } from "@convex-dev/auth/providers/Password";
import { Anonymous } from "@convex-dev/auth/providers/Anonymous";
import { convexAuth } from "@convex-dev/auth/server";
import { DataModel } from "./_generated/dataModel";

const CustomPassword = Password<DataModel>({
profile(params) {
return {
email: params.email as string,
first_name: params.first_name as string,
last_name: params.last_name as string,
full_name: params.full_name as string,
};
},
});

export const { auth, signIn, signOut, store } = convexAuth({
providers: [Anonymous, CustomPassword],
callbacks: {
async createOrUpdateUser(ctx, args) {
try {
console.log(args.existingUserId);
if (args.existingUserId) {
// The user already exists, return the existing user ID
return args.existingUserId;
}

// The user does not exist yet, create a new user
const newUser = await ctx.db.insert("users", {
email: args.profile.email,
first_name: args.profile.first_name,
last_name: args.profile.last_name,
full_name: args.profile.full_name,
});

// Return the new user ID
return newUser;
} catch (e) {
console.log("Caught");
}
},
},
});

Did you find this page helpful?