Fei Xiang
Fei Xiang4mo ago

How can I save additional user information like role "vendor" or "customer" in user table when using

How can I save additional user information like role "vendor" or "customer" in user table when using convex google Auth Google({ profile(res, tokens) { console.info({ res, tokens }); return { id: res.sub, balance: '0', fullname: res.name, email: res.email, type: res.customer, }; }, }), onClick={() => signIn('google', {customer: 'vender'} }) } As you can see, customer is not default property of google profile.... I tried like this but can received addtional info please provide me with guidance?
5 Replies
allen
allen4mo ago
You may need to combine with this to fetch and populate: https://labs.convex.dev/auth/api_reference/server#callbacksafterusercreatedorupdated
server - Convex Auth
Authentication library for your Convex backend
Fei Xiang
Fei XiangOP4mo ago
Thanks for your reply. Can I use createOrUpdateUser function to save additional user info not from google profile? signIn('google', { createOrUpdateUser: , }) if then please provide me with example
sshader
sshader4mo ago
Can I use createOrUpdateUser function to save additional user info not from google profile?
I believe it can only access information in the profile of whatever OAuth provider you're using (or data already stored in the DB, like a pre-existing account for the same user). https://next-auth.js.org/providers/google is a good starting point for what fields you do have available You can store other information in a separate mutation after authenticating (https://labs.convex.dev/auth/authz#get-currently-signed-in-user-id)
Authorization - Convex Auth
Authentication library for your Convex backend
jamalsoueidan
jamalsoueidan4mo ago
This is how I do it! If user already exist, just update his login time, or new users, assign accessLevel 0.
export const { auth, signIn, signOut, store } = convexAuth({
providers: [Facebook],
callbacks: {
async createOrUpdateUser(ctx, args) {
if (args.existingUserId) {
await ctx.db.patch(args.existingUserId, { updatingTime: Date.now() });
return args.existingUserId;
}

return ctx.db.insert("users", {
...args.profile,
accessLevel: 0,
updatingTime: Date.now(),
});
},
},
});
export const { auth, signIn, signOut, store } = convexAuth({
providers: [Facebook],
callbacks: {
async createOrUpdateUser(ctx, args) {
if (args.existingUserId) {
await ctx.db.patch(args.existingUserId, { updatingTime: Date.now() });
return args.existingUserId;
}

return ctx.db.insert("users", {
...args.profile,
accessLevel: 0,
updatingTime: Date.now(),
});
},
},
});
Its done in the backend not frontend.

Did you find this page helpful?