refactor base auth

This commit is contained in:
2025-11-07 10:33:48 -03:00
parent 6f4df44a00
commit 57dd9492ef
14 changed files with 1176 additions and 1580 deletions

View File

@@ -4,21 +4,12 @@ import { components } from "./_generated/api";
import { type DataModel } from "./_generated/dataModel";
import { query } from "./_generated/server";
import { betterAuth } from "better-auth";
// import authSchema from "./betterAuth/schema";
const siteUrl = process.env.SITE_URL!;
// The component client has methods needed for integrating Convex with Better Auth,
// as well as helper methods for general use.
export const authComponent = createClient<DataModel>(components.betterAuth);
// export const authComponent = createClient<DataModel, typeof authSchema>(
// components.betterAuth,
// {
// local: {
// schema: authSchema,
// },
// }
// );
export const createAuth = (
ctx: GenericCtx<DataModel>,
@@ -49,6 +40,18 @@ export const createAuth = (
export const getCurrentUser = query({
args: {},
handler: async (ctx) => {
return authComponent.getAuthUser(ctx);
const authUser = await authComponent.safeGetAuthUser(ctx as any);
if (!authUser) {
return;
}
const user = await ctx.db
.query("usuarios")
.withIndex("authId", (q) => q.eq("authId", authUser._id))
.unique();
if (!user) {
return;
}
return user;
},
});