ianpaschal
ianpaschal
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
Which I guess could be a workaround right now, but the documentation seems to suggest this is not needed and in later when using the inferred types it would be nice to not have | undefined for username and the visibilities :/
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
export const visibilityLevels = v.union(
v.literal('hidden'),
v.literal('public'),
);

const userFields = {
email: v.string(),
avatarUrl: v.optional(v.string()),
givenName: v.optional(v.string()),
familyName: v.optional(v.string()),
countryCode: v.optional(v.string()),
username: v.optional(v.string()),
nameVisibility: v.optional(visibilityLevels),
locationVisibility: v.optional(visibilityLevels),
};

export const usersTable = defineTable({
...userFields,
modifiedAt: v.optional(v.number()),
})
.index('byCountryCode', ['countryCode'])
.index('byName', ['givenName', 'familyName'])
.index('byUsername', ['username']);
export const visibilityLevels = v.union(
v.literal('hidden'),
v.literal('public'),
);

const userFields = {
email: v.string(),
avatarUrl: v.optional(v.string()),
givenName: v.optional(v.string()),
familyName: v.optional(v.string()),
countryCode: v.optional(v.string()),
username: v.optional(v.string()),
nameVisibility: v.optional(visibilityLevels),
locationVisibility: v.optional(visibilityLevels),
};

export const usersTable = defineTable({
...userFields,
modifiedAt: v.optional(v.number()),
})
.index('byCountryCode', ['countryCode'])
.index('byName', ['givenName', 'familyName'])
.index('byUsername', ['username']);
This is what it looks like. Which works fine, so long as all fields but email are optional.
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
I took most of those fields out as they're neither needed or actively confusing (name instead of givenName and username), etc.
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
It all seems to work if those fields are optional. If they are required, something goes wrong...
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
yup!
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
However this seems to be a bug in that case...
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
No description
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
yup!
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
So I guess FormData is the correct approach...
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
So! I made everything optional, it absolutely comes into the users table.
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
Right. that's a bit cleaner.
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
Let me give that a try...
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
As in, temporarily wrap it in v.optional()?
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
Also, just to check, because it's not stated in the documentation, but I assume the CustomProfile.ts in the documentation should be added to auth.ts in place of the default Password?
import { convexAuth } from "@convex-dev/auth/server";
import Password from "./CustomProfile";

export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
providers: [Password],
});
import { convexAuth } from "@convex-dev/auth/server";
import Password from "./CustomProfile";

export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
providers: [Password],
});
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
(Thanks, by the way, for the help so far)
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
export const visibilityLevels = v.union(
v.literal('hidden'),
v.literal('public'),
);

const userFields = {
email: v.string(),
avatarUrl: v.optional(v.string()),
givenName: v.optional(v.string()),
familyName: v.optional(v.string()),
countryCode: v.optional(v.string()),
username: v.string(),
nameVisibility: visibilityLevels,
locationVisibility: visibilityLevels,
};

export const usersTable = defineTable({
...userFields,
modifiedAt: v.optional(v.number()),
})
.index('byCountryCode', ['countryCode'])
.index('byName', ['givenName', 'familyName'])
.index('byUsername', ['username']);
export const visibilityLevels = v.union(
v.literal('hidden'),
v.literal('public'),
);

const userFields = {
email: v.string(),
avatarUrl: v.optional(v.string()),
givenName: v.optional(v.string()),
familyName: v.optional(v.string()),
countryCode: v.optional(v.string()),
username: v.string(),
nameVisibility: visibilityLevels,
locationVisibility: visibilityLevels,
};

export const usersTable = defineTable({
...userFields,
modifiedAt: v.optional(v.number()),
})
.index('byCountryCode', ['countryCode'])
.index('byName', ['givenName', 'familyName'])
.index('byUsername', ['username']);
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
This is my form onSubmit, adapted slightly from the Convex Auth boilerplate:
onSubmit={(e) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
formData.set("flow", flow);
formData.set("nameVisibility", 'hidden');
formData.set("locationVisibility", 'hidden');
void signIn("password", formData).catch((error) => {
setError(error.message);
});
}}
onSubmit={(e) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
formData.set("flow", flow);
formData.set("nameVisibility", 'hidden');
formData.set("locationVisibility", 'hidden');
void signIn("password", formData).catch((error) => {
setError(error.message);
});
}}
I've also added this per the documentation:
import { Password } from "@convex-dev/auth/providers/Password";
import { DataModel } from "./_generated/dataModel";

export default Password<DataModel>({
profile(params) {
return {
email: params.email as string,
username: params.username as string,
locationVisibility: params.locationVisibility as 'hidden' | 'public',
nameVisibility: params.nameVisibility as 'hidden' | 'public',
};
},
});
import { Password } from "@convex-dev/auth/providers/Password";
import { DataModel } from "./_generated/dataModel";

export default Password<DataModel>({
profile(params) {
return {
email: params.email as string,
username: params.username as string,
locationVisibility: params.locationVisibility as 'hidden' | 'public',
nameVisibility: params.nameVisibility as 'hidden' | 'public',
};
},
});
However when signing up, I still get an error:
Uncaught Error: Failed to insert or update a document in table "users" because it does not match the schema: Object is missing the required field `locationVisibility`
Uncaught Error: Failed to insert or update a document in table "users" because it does not match the schema: Object is missing the required field `locationVisibility`
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
I have added such a file, and added the username field and include it to the FormData, no dice.
31 replies
CCConvex Community
Created by ianpaschal on 2/23/2025 in #support-community
Convex Auth: How to add custom data to signIn() (or up)
Yes, I'm aware of those two pages but they seem incomplete... Step 2 doesn't explain how to actually pass the values.
31 replies