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:
@@ -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!;
|
||||
|
||||
@@ -20,19 +19,19 @@ export const createAuth = (
|
||||
// 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,
|
||||
disabled: optionsOnly
|
||||
},
|
||||
baseURL: siteUrl,
|
||||
database: authComponent.adapter(ctx),
|
||||
// Configure simple, non-verified email/password to get started
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
requireEmailVerification: false,
|
||||
requireEmailVerification: false
|
||||
},
|
||||
plugins: [
|
||||
// The Convex plugin is required for Convex compatibility
|
||||
convex(),
|
||||
],
|
||||
convex()
|
||||
]
|
||||
});
|
||||
};
|
||||
|
||||
@@ -47,25 +46,27 @@ export const getCurrentUser = query({
|
||||
}
|
||||
|
||||
const user = await ctx.db
|
||||
.query("usuarios")
|
||||
.withIndex("authId", (q) => q.eq("authId", authUser._id))
|
||||
.query('usuarios')
|
||||
.withIndex('authId', (q) => q.eq('authId', authUser._id))
|
||||
.unique();
|
||||
|
||||
if (!user) {
|
||||
return;
|
||||
}
|
||||
|
||||
const fotoPerfilUrl = user.fotoPerfil ? await ctx.storage.getUrl(user.fotoPerfil) : null;
|
||||
|
||||
if (!user.roleId) {
|
||||
return { ...user, role: null };
|
||||
return { ...user, role: null, fotoPerfilUrl };
|
||||
}
|
||||
|
||||
const role = await ctx.db
|
||||
.query("roles")
|
||||
.withIndex("by_id", (q) => q.eq("_id", user.roleId))
|
||||
.query('roles')
|
||||
.withIndex('by_id', (q) => q.eq('_id', user.roleId))
|
||||
.unique();
|
||||
|
||||
return { ...user, role };
|
||||
},
|
||||
return { ...user, role, fotoPerfilUrl };
|
||||
}
|
||||
});
|
||||
|
||||
export const getCurrentUserFunction = async (ctx: QueryCtx | MutationCtx) => {
|
||||
@@ -75,8 +76,8 @@ export const getCurrentUserFunction = async (ctx: QueryCtx | MutationCtx) => {
|
||||
}
|
||||
|
||||
const user = await ctx.db
|
||||
.query("usuarios")
|
||||
.withIndex("authId", (q) => q.eq("authId", authUser._id))
|
||||
.query('usuarios')
|
||||
.withIndex('authId', (q) => q.eq('authId', authUser._id))
|
||||
.unique();
|
||||
if (!user) {
|
||||
return;
|
||||
@@ -95,8 +96,8 @@ export const createAuthUser = async (
|
||||
body: {
|
||||
name: args.nome,
|
||||
email: args.email,
|
||||
password: args.password,
|
||||
},
|
||||
password: args.password
|
||||
}
|
||||
});
|
||||
|
||||
return result.user.id;
|
||||
@@ -112,7 +113,7 @@ export const updatePassword = async (
|
||||
headers,
|
||||
body: {
|
||||
currentPassword: args.currentPassword,
|
||||
newPassword: args.newPassword,
|
||||
},
|
||||
newPassword: args.newPassword
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user