Got the profile picture thing working!

This commit is contained in:
2025-09-03 12:51:07 -05:00
parent 6febfa05d4
commit be23ea1070
6 changed files with 102 additions and 43 deletions

View File

@@ -1,6 +1,7 @@
import { ConvexError, v } from 'convex/values';
import { convexAuth, getAuthUserId } from '@convex-dev/auth/server';
import { mutation, query } from './_generated/server';
import { type Id } from './_generated/dataModel';
import Password from './CustomPassword';
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
@@ -12,19 +13,23 @@ export const getUser = query(async (ctx) => {
if (!userId) return null;
const user = await ctx.db.get(userId);
if (!user) throw new ConvexError('User not found.');
const image: Id<'_storage'> | null =
typeof user.image === 'string' && user.image.length > 0
? user.image as Id<'_storage'>
: null
return {
id: user._id,
email: user.email ?? null,
name: user.name ?? null,
image: user.image ?? null,
image,
};
});
export const updateUserImage = mutation({
args: {
storageId: v.id('_storage')
storageId: v.id('_storage'),
},
handler: async (ctx, {storageId}) => {
handler: async (ctx, { storageId }) => {
const userId = await getAuthUserId(ctx);
if (!userId) throw new ConvexError('Not authenticated.');
await ctx.db.patch(userId, { image: storageId });

View File

@@ -1,12 +1,12 @@
import { mutation, query } from "./_generated/server";
import { v } from "convex/values";
import { mutation, query } from './_generated/server';
import { v } from 'convex/values';
export const generateUploadUrl = mutation(async (ctx) => {
return await ctx.storage.generateUploadUrl();
});
export const getImageUrl = query({
args: { storageId: v.id("_storage") },
args: { storageId: v.id('_storage') },
handler: async (ctx, { storageId }) => {
return await ctx.storage.getUrl(storageId);
},