refactor: improve user retrieval and formatting in auth component

- Enhanced the getCurrentUser function to include the user's profile picture URL.
- Cleaned up formatting for better readability and consistency across the auth.ts file.
- Maintained existing functionality while improving code organization.
This commit is contained in:
2025-11-12 12:15:43 -03:00
parent da26a21f7e
commit 6087990eaf

View File

@@ -1,10 +1,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 { mutation, MutationCtx, query, QueryCtx } from "./_generated/server";
import { betterAuth } from "better-auth";
import { v } from "convex/values";
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 { MutationCtx, query, QueryCtx } from './_generated/server';
import { betterAuth } from 'better-auth';
const siteUrl = process.env.SITE_URL!;
@@ -13,106 +12,108 @@ const siteUrl = process.env.SITE_URL!;
export const authComponent = createClient<DataModel>(components.betterAuth);
export const createAuth = (
ctx: GenericCtx<DataModel>,
{ optionsOnly } = { optionsOnly: false }
ctx: GenericCtx<DataModel>,
{ optionsOnly } = { optionsOnly: false }
) => {
return betterAuth({
// disable logging when createAuth is called just to generate options.
// this is not required, but there's a lot of noise in logs without it.
logger: {
disabled: optionsOnly,
},
baseURL: siteUrl,
database: authComponent.adapter(ctx),
// Configure simple, non-verified email/password to get started
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
plugins: [
// The Convex plugin is required for Convex compatibility
convex(),
],
});
return betterAuth({
// disable logging when createAuth is called just to generate options.
// this is not required, but there's a lot of noise in logs without it.
logger: {
disabled: optionsOnly
},
baseURL: siteUrl,
database: authComponent.adapter(ctx),
// Configure simple, non-verified email/password to get started
emailAndPassword: {
enabled: true,
requireEmailVerification: false
},
plugins: [
// The Convex plugin is required for Convex compatibility
convex()
]
});
};
// Example function for getting the current user
// Feel free to edit, omit, etc.
export const getCurrentUser = query({
args: {},
handler: async (ctx) => {
const authUser = await authComponent.safeGetAuthUser(ctx as any);
if (!authUser) {
return;
}
args: {},
handler: async (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();
const user = await ctx.db
.query('usuarios')
.withIndex('authId', (q) => q.eq('authId', authUser._id))
.unique();
if (!user) {
return;
}
if (!user) {
return;
}
if (!user.roleId) {
return { ...user, role: null };
}
const fotoPerfilUrl = user.fotoPerfil ? await ctx.storage.getUrl(user.fotoPerfil) : null;
const role = await ctx.db
.query("roles")
.withIndex("by_id", (q) => q.eq("_id", user.roleId))
.unique();
if (!user.roleId) {
return { ...user, role: null, fotoPerfilUrl };
}
return { ...user, role };
},
const role = await ctx.db
.query('roles')
.withIndex('by_id', (q) => q.eq('_id', user.roleId))
.unique();
return { ...user, role, fotoPerfilUrl };
}
});
export const getCurrentUserFunction = async (ctx: QueryCtx | MutationCtx) => {
const authUser = await authComponent.safeGetAuthUser(ctx as any);
if (!authUser) {
return;
}
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;
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 }
ctx: MutationCtx,
args: { nome: string; email: string; password: string }
) => {
const { auth, headers } = await authComponent.getAuth(createAuth, ctx as any);
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,
},
});
const result = await auth.api.signUpEmail({
headers,
body: {
name: args.nome,
email: args.email,
password: args.password
}
});
return result.user.id;
return result.user.id;
};
export const updatePassword = async (
ctx: MutationCtx,
args: { newPassword: string; currentPassword: string }
ctx: MutationCtx,
args: { newPassword: string; currentPassword: string }
) => {
const { auth, headers } = await authComponent.getAuth(createAuth, ctx as any);
const { auth, headers } = await authComponent.getAuth(createAuth, ctx as any);
await auth.api.changePassword({
headers,
body: {
currentPassword: args.currentPassword,
newPassword: args.newPassword,
},
});
await auth.api.changePassword({
headers,
body: {
currentPassword: args.currentPassword,
newPassword: args.newPassword
}
});
};