Trying to add avatar upload. Project works rn so wanna push

This commit is contained in:
2025-09-03 10:52:10 -05:00
parent 35215d34a1
commit 6febfa05d4
8 changed files with 144 additions and 5 deletions

View File

@@ -15,6 +15,7 @@ import type {
} from "convex/server";
import type * as CustomPassword from "../CustomPassword.js";
import type * as auth from "../auth.js";
import type * as files from "../files.js";
import type * as http from "../http.js";
import type * as myFunctions from "../myFunctions.js";
@@ -29,6 +30,7 @@ import type * as myFunctions from "../myFunctions.js";
declare const fullApi: ApiFromModules<{
CustomPassword: typeof CustomPassword;
auth: typeof auth;
files: typeof files;
http: typeof http;
myFunctions: typeof myFunctions;
}>;

View File

@@ -1,5 +1,6 @@
import { ConvexError, v } from 'convex/values';
import { convexAuth, getAuthUserId } from '@convex-dev/auth/server';
import { query } from './_generated/server';
import { mutation, query } from './_generated/server';
import Password from './CustomPassword';
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
@@ -10,7 +11,7 @@ export const getUser = query(async (ctx) => {
const userId = await getAuthUserId(ctx);
if (!userId) return null;
const user = await ctx.db.get(userId);
if (!user) return null;
if (!user) throw new ConvexError('User not found.');
return {
id: user._id,
email: user.email ?? null,
@@ -18,3 +19,15 @@ export const getUser = query(async (ctx) => {
image: user.image ?? null,
};
});
export const updateUserImage = mutation({
args: {
storageId: v.id('_storage')
},
handler: async (ctx, {storageId}) => {
const userId = await getAuthUserId(ctx);
if (!userId) throw new ConvexError('Not authenticated.');
await ctx.db.patch(userId, { image: storageId });
return { success: true };
},
});

13
convex/files.ts Normal file
View File

@@ -0,0 +1,13 @@
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") },
handler: async (ctx, { storageId }) => {
return await ctx.storage.getUrl(storageId);
},
});