kstulgys
kstulgys5mo ago

How do I extend convex auth with my custom auth provider?

So idea is to pass 3 things from frontend
singIn("my-custom-provider", {address, message, signature})
singIn("my-custom-provider", {address, message, signature})
and this is where I'm stuck at:
export const { auth, signIn, signOut, store } = convexAuth({
providers: [
GitHub,
ConvexCredentials({
id: "my-custom-provider"
authorize: async (credentials, ctx) => {
const { address, message, signature } = credentials

const evmAddress = ethers.verifyMessage(message, signature);
const isVerified = evmAddress === address;

// ???
},
}),
],
});
export const { auth, signIn, signOut, store } = convexAuth({
providers: [
GitHub,
ConvexCredentials({
id: "my-custom-provider"
authorize: async (credentials, ctx) => {
const { address, message, signature } = credentials

const evmAddress = ethers.verifyMessage(message, signature);
const isVerified = evmAddress === address;

// ???
},
}),
],
});
How do I finish this implementation? Should I use ConvexCredentials or should I use something else?
2 Replies
Michal Srb
Michal Srb5mo ago
As the authorize method's signature suggests, you need to return
{
userId: GenericId<"users">;
sessionId?: GenericId<"authSessions">;
} | null
{
userId: GenericId<"users">;
sessionId?: GenericId<"authSessions">;
} | null
the sessionId is optional, if you don't return it a session will be created automatically. If you return null the signIn will fail. For example:
export const { auth, signIn, signOut, store } = convexAuth({
providers: [
GitHub,
ConvexCredentials({
id: "my-custom-provider"
authorize: async (credentials, ctx) => {
const { address, message, signature } = credentials

const evmAddress = ethers.verifyMessage(message, signature);
const isVerified = evmAddress === address;

return isVerified ? await ctx.db.runMutation(internal.users.create) : null;
},
}),
],
});
export const { auth, signIn, signOut, store } = convexAuth({
providers: [
GitHub,
ConvexCredentials({
id: "my-custom-provider"
authorize: async (credentials, ctx) => {
const { address, message, signature } = credentials

const evmAddress = ethers.verifyMessage(message, signature);
const isVerified = evmAddress === address;

return isVerified ? await ctx.db.runMutation(internal.users.create) : null;
},
}),
],
});
kstulgys
kstulgysOP5mo ago
Thank you sir, just one correction for those who will see this:
const userId = await ctx.db.runMutation(internal.users.create, userData)
return { userId }
const userId = await ctx.db.runMutation(internal.users.create, userData)
return { userId }

Did you find this page helpful?