Danny I
Danny I
CCConvex Community
Created by Danny I on 9/30/2024 in #support-community
Chat app, only one messages table?
Ah sorry, kapa.ai gave a great answer. 🙏🏼
3 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
oh my god it's working. 😭 Thank you so much
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
My guess is I can just move this to the first callback and use the userId I get back when I insert the user record.
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
It might not be necessary anymore, but, using the userId I need to add a record in the businesses table, I think I can do this in the first callback though, on user creation since I do have the userId... 🤔
afterUserCreatedOrUpdated: async (ctx, { userId, profile }) => {
const { businessName } = profile;
const businessId = await ctx.db.insert("businesses", {
ownerId: userId,
name: businessName as string,
projects: []
});
await ctx.db.patch(userId, { businesses: [businessId] });
},
afterUserCreatedOrUpdated: async (ctx, { userId, profile }) => {
const { businessName } = profile;
const businessId = await ctx.db.insert("businesses", {
ownerId: userId,
name: businessName as string,
projects: []
});
await ctx.db.patch(userId, { businesses: [businessId] });
},
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
Okay it created account, I had altogether left out the initialization of the user, thinking it was somehow abstracted away since I didn't see it happenign earlier. But indeed it does actually run. Now the second callback is not running, which is a new issue but would love your quick outlook on it.
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
let me try again
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
I'm a dummy one second.
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
oh
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
{
"argsInCreateCallback": {
"existingUserId": null,
"type": "credentials",
"provider": {
"id": "password",
"type": "credentials",
"options": {
"id": "password",
"crypto": {},
"extraProviders": [
null,
{
"id": "resend-otp",
"type": "email",
"name": "Resend",
"from": "Auth.js <no-reply@authjs.dev>",
"maxAge": 86400,
"options": {
"id": "resend-otp",
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
},
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
}
],
"verify": {
"id": "resend-otp",
"type": "email",
"name": "Resend",
"from": "Auth.js <no-reply@authjs.dev>",
"maxAge": 86400,
"options": {
"id": "resend-otp",
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
},
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
}
},
"crypto": {},
"extraProviders": [
null,
{
"id": "resend-otp",
"type": "email",
"name": "Resend",
"from": "Auth.js <no-reply@authjs.dev>",
"maxAge": 86400,
"options": {
"id": "resend-otp",
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
},
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
}
],
"verify": {...}
},
"profile": {
"accountType": "business",
"businessName": "Test Business",
"conversationIds": [],
"email": "danny.israel@gmail.com",
"name": "Danny"
},
"shouldLinkViaEmail": true,
"shouldLinkViaPhone": false
}
{
"argsInCreateCallback": {
"existingUserId": null,
"type": "credentials",
"provider": {
"id": "password",
"type": "credentials",
"options": {
"id": "password",
"crypto": {},
"extraProviders": [
null,
{
"id": "resend-otp",
"type": "email",
"name": "Resend",
"from": "Auth.js <no-reply@authjs.dev>",
"maxAge": 86400,
"options": {
"id": "resend-otp",
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
},
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
}
],
"verify": {
"id": "resend-otp",
"type": "email",
"name": "Resend",
"from": "Auth.js <no-reply@authjs.dev>",
"maxAge": 86400,
"options": {
"id": "resend-otp",
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
},
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
}
},
"crypto": {},
"extraProviders": [
null,
{
"id": "resend-otp",
"type": "email",
"name": "Resend",
"from": "Auth.js <no-reply@authjs.dev>",
"maxAge": 86400,
"options": {
"id": "resend-otp",
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
},
"apiKey": "re_8xzZwFbM_NcFZunViHTtiC3sEskBZJDgZ"
}
],
"verify": {...}
},
"profile": {
"accountType": "business",
"businessName": "Test Business",
"conversationIds": [],
"email": "danny.israel@gmail.com",
"name": "Danny"
},
"shouldLinkViaEmail": true,
"shouldLinkViaPhone": false
}
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
all gone, it's all empty, including the auth sessions
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
No description
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
Ah, yeah, it didn't work, I'm not sure why it didn't error out that one time, but it is now, with the same error.
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
I also may have spoken too soon, I still need to check that everything worked, it just didn't error this time.
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
I'm sure there's a good reason for it though.
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
I did find it confusing though since the logs didn't reflect that, and I expected this function to run on initial user creation 🤷🏻‍♂️
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
users: defineTable({
email: v.string(),
name: v.string(),
accountType: v.union(v.literal("personal"), v.literal("business")),
businesses: v.union(v.array(v.id("businesses")), v.null()),
businessName: v.union(v.string(), v.null()),
conversationIds: v.array(v.id("conversations")),
}).index("email", ["email"]),
users: defineTable({
email: v.string(),
name: v.string(),
accountType: v.union(v.literal("personal"), v.literal("business")),
businesses: v.union(v.array(v.id("businesses")), v.null()),
businessName: v.union(v.string(), v.null()),
conversationIds: v.array(v.id("conversations")),
}).index("email", ["email"]),
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
Edit: Actually it still didn't work. Hehe 😅 Another silly moment I guess. I saw this
async createOrUpdateUser(ctx: MutationCtx, args) {
if (args.existingUserId) {
// Optionally merge updated fields into the existing user object here
return args.existingUserId;
}

// Implement your own account linking logic:
const existingUser = await findUserByEmail(ctx, args.profile.email);
if (existingUser) return existingUser._id;

// Implement your own user creation:
return ctx.db.insert("users", {
/* ... */
});
},
async createOrUpdateUser(ctx: MutationCtx, args) {
if (args.existingUserId) {
// Optionally merge updated fields into the existing user object here
return args.existingUserId;
}

// Implement your own account linking logic:
const existingUser = await findUserByEmail(ctx, args.profile.email);
if (existingUser) return existingUser._id;

// Implement your own user creation:
return ctx.db.insert("users", {
/* ... */
});
},
At which point I realized maybe I just need to return if the user was created successfully, which indeed fixed it.
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
Also, the user is added to the database with the accountType field correctly set.
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
These are the params I get in profile:
{
"accountType": "business",
"businessName": "Demo",
"conversationIds": [],
"email": d@gmail.com",
"flow": "signUp",
"name": "Danny",
"password": "12341234"
},
{
"accountType": "business",
"businessName": "Demo",
"conversationIds": [],
"email": d@gmail.com",
"flow": "signUp",
"name": "Danny",
"password": "12341234"
},
43 replies
CCConvex Community
Created by Danny I on 9/23/2024 in #support-community
Sign up user with custom fields
So I'm still having issues signing up, I get this error:
Server Error: Failed to insert or update a document in table "users" because it does not match the schema: Object is missing the required field `accountType`.
Server Error: Failed to insert or update a document in table "users" because it does not match the schema: Object is missing the required field `accountType`.
Which is because in my callback for creating a user, args doesn't have the data I sent in for sign up, only the email is coming through:
callbacks: {
createOrUpdateUser: async (ctx, args) => {
const params = args.profile; // Only has email.
const userId = await ctx.db.insert("users", {
email: params.email as string,
name: params.name as string,
accountType: params.accountType as 'personal' | 'business',
businesses: params.accountType === 'business' ? [] : null,
businessName: params.accountType === 'business' ? params.businessName as string : null,
conversationIds: [],
});

return userId;
},
callbacks: {
createOrUpdateUser: async (ctx, args) => {
const params = args.profile; // Only has email.
const userId = await ctx.db.insert("users", {
email: params.email as string,
name: params.name as string,
accountType: params.accountType as 'personal' | 'business',
businesses: params.accountType === 'business' ? [] : null,
businessName: params.accountType === 'business' ? params.businessName as string : null,
conversationIds: [],
});

return userId;
},
Even though my profile function does receive the params and returns them:
export default Password<DataModel>({
profile(params) {
const { error, data } = ParamsSchema.safeParse(params);
if (error) { throw new ConvexError(error.format()); }

return {
email: data.email as string,
name: params.name as string,
accountType: params.accountType as 'personal' | 'business',
businesses: params.businesses as Id<"businesses">[] | null,
businessName: params.businessName as string | null,
conversationIds: params.conversationIds as Id<"conversations">[],
};
},
verify: ResendOTP
export default Password<DataModel>({
profile(params) {
const { error, data } = ParamsSchema.safeParse(params);
if (error) { throw new ConvexError(error.format()); }

return {
email: data.email as string,
name: params.name as string,
accountType: params.accountType as 'personal' | 'business',
businesses: params.businesses as Id<"businesses">[] | null,
businessName: params.businessName as string | null,
conversationIds: params.conversationIds as Id<"conversations">[],
};
},
verify: ResendOTP
👀
43 replies