44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { expo } from "@better-auth/expo";
|
|
import { createClient, type GenericCtx } from "@convex-dev/better-auth";
|
|
import { convex, crossDomain } from "@convex-dev/better-auth/plugins";
|
|
import { betterAuth } from "better-auth/minimal";
|
|
|
|
import { components } from "./_generated/api";
|
|
import type { DataModel } from "./_generated/dataModel";
|
|
import { query } from "./_generated/server";
|
|
import authConfig from "./auth.config";
|
|
|
|
const siteUrl = process.env.SITE_URL!;
|
|
const nativeAppUrl = process.env.NATIVE_APP_URL || "code://";
|
|
|
|
export const authComponent = createClient<DataModel>(components.betterAuth);
|
|
|
|
function createAuth(ctx: GenericCtx<DataModel>) {
|
|
return betterAuth({
|
|
baseURL: process.env.CONVEX_SITE_URL,
|
|
trustedOrigins: [siteUrl, nativeAppUrl, "exp://"],
|
|
database: authComponent.adapter(ctx),
|
|
emailAndPassword: {
|
|
enabled: true,
|
|
requireEmailVerification: false,
|
|
},
|
|
plugins: [
|
|
expo(),
|
|
crossDomain({ siteUrl }),
|
|
convex({
|
|
authConfig,
|
|
jwksRotateOnTokenGenerationError: true,
|
|
}),
|
|
],
|
|
});
|
|
}
|
|
|
|
export { createAuth };
|
|
|
|
export const getCurrentUser = query({
|
|
args: {},
|
|
handler: async (ctx) => {
|
|
return await authComponent.safeGetAuthUser(ctx);
|
|
},
|
|
});
|