sipher/convex/betterAuth/olm/index.ts
Nixyan df41cf4657 Update dependencies, add OLM support, and improve authentication flow
- Updated various dependencies in package.json and bun.lock for better compatibility and features.
- Added OLM (Object Location Management) support by including necessary files and updating authentication logic.
- Enhanced the authentication flow with better error handling and user feedback.
- Introduced new database schema for OLM accounts and updated related API components.
- Improved socket connection management and user interface elements for a smoother user experience.
2025-12-19 12:18:46 -03:00

50 lines
No EOL
1.3 KiB
TypeScript

import { v } from "convex/values";
import { Id } from "../../_generated/dataModel";
import { mutation, query } from "../../_generated/server";
export const sendKeysToServer = mutation({
args: {
userId: v.string(),
identityKey: v.object({
curve25519: v.string(),
ed25519: v.string(),
}),
oneTimeKeys: v.array(v.object({
keyId: v.string(),
publicKey: v.string(),
})),
forceInsert: v.boolean(), // if true, insert even if user already has an olm account
},
handler: async (ctx, args) => {
console.log("sendKeysToServer", args);
// check if user already has an olm account
// @ts-ignore
const olmAccount = await ctx.db.query("olmAccount").withIndex("userId", (q) => q.eq("userId", args.userId)).first();
console.log("olmAccount", olmAccount);
if (olmAccount && !args.forceInsert) {
throw new Error("User already has an olm account");
}
const insert = await ctx.db.insert<"olmAccount">("olmAccount", {
userId: args.userId,
identityKey: args.identityKey,
oneTimeKeys: args.oneTimeKeys,
});
console.log("insert", insert);
return insert;
},
});
export const retrieveServerOlmAccount = query({
args: {
userId: v.string(),
},
handler: async (ctx, args) => {
const olmAccount = await ctx.db.get<"olmAccount">(args.userId as Id<"olmAccount">);
if (olmAccount) {
return olmAccount;
}
return null;
},
});