refactor: remove authentication module and integrate Better Auth
- Deleted the `autenticacao.ts` file to streamline the authentication process. - Updated the `auth.ts` file to include new functions for user management and password updates. - Modified the schema to enforce the presence of `authId` for users, ensuring integration with Better Auth. - Refactored the seed process to create users with Better Auth integration, enhancing user management capabilities. - Cleaned up the `usuarios.ts` file to utilize the new authentication functions and improve code clarity.
This commit is contained in:
@@ -2,8 +2,9 @@ import { createClient, type GenericCtx } from "@convex-dev/better-auth";
|
||||
import { convex } from "@convex-dev/better-auth/plugins";
|
||||
import { components } from "./_generated/api";
|
||||
import { type DataModel } from "./_generated/dataModel";
|
||||
import { query } from "./_generated/server";
|
||||
import { mutation, MutationCtx, query, QueryCtx } from "./_generated/server";
|
||||
import { betterAuth } from "better-auth";
|
||||
import { v } from "convex/values";
|
||||
|
||||
const siteUrl = process.env.SITE_URL!;
|
||||
|
||||
@@ -55,3 +56,52 @@ export const getCurrentUser = query({
|
||||
return user;
|
||||
},
|
||||
});
|
||||
|
||||
export const getCurrentUserFunction = async (ctx: QueryCtx | MutationCtx) => {
|
||||
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;
|
||||
};
|
||||
|
||||
export const createAuthUser = async (
|
||||
ctx: MutationCtx,
|
||||
args: { nome: string; email: string; password: string }
|
||||
) => {
|
||||
const { auth, headers } = await authComponent.getAuth(createAuth, ctx as any);
|
||||
|
||||
const result = await auth.api.signUpEmail({
|
||||
headers,
|
||||
body: {
|
||||
name: args.nome,
|
||||
email: args.email,
|
||||
password: args.password,
|
||||
},
|
||||
});
|
||||
|
||||
return result.user.id;
|
||||
};
|
||||
|
||||
export const updatePassword = async (
|
||||
ctx: MutationCtx,
|
||||
args: { newPassword: string; currentPassword: string }
|
||||
) => {
|
||||
const { auth, headers } = await authComponent.getAuth(createAuth, ctx as any);
|
||||
|
||||
await auth.api.changePassword({
|
||||
headers,
|
||||
body: {
|
||||
currentPassword: args.currentPassword,
|
||||
newPassword: args.newPassword,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user