34 lines
995 B
TypeScript
34 lines
995 B
TypeScript
import { ConvexError, v } from 'convex/values';
|
|
import { convexAuth, getAuthUserId } from '@convex-dev/auth/server';
|
|
import { mutation, query } from './_generated/server';
|
|
import Password from './CustomPassword';
|
|
|
|
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
|
|
providers: [Password],
|
|
});
|
|
|
|
export const getUser = query(async (ctx) => {
|
|
const userId = await getAuthUserId(ctx);
|
|
if (!userId) return null;
|
|
const user = await ctx.db.get(userId);
|
|
if (!user) throw new ConvexError('User not found.');
|
|
return {
|
|
id: user._id,
|
|
email: user.email ?? null,
|
|
name: user.name ?? null,
|
|
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 };
|
|
},
|
|
});
|