EMILIANOM
EMILIANOM
CCConvex Community
Created by Slewt on 10/15/2024 in #support-community
Is nextjs recommended with convex? or not ideal?
Hello Jamie, will i need to know react to use tanstack? I use Sveltekit and im pretty happy, but I want to be integrated into Convex in the best way possible Whats your suggestion? thanks!
22 replies
CCConvex Community
Created by EMILIANOM on 10/7/2024 in #support-community
Svelte + Clerk | Any example of how to get it working?
No description
7 replies
CCConvex Community
Created by EMILIANOM on 9/26/2024 in #support-community
Convex + Lucia auth + Svelte | Help
Thank for your time! That’s enough, I will keep experimenting and learning about convex, I’m really liking it, just the major pain point was the auth with svelte but your repo saved me Night 🌙
14 replies
CCConvex Community
Created by EMILIANOM on 9/26/2024 in #support-community
Convex + Lucia auth + Svelte | Help
When the page loads it verifies the auth (hooks.server.ts) and then when it calls the database via (query, mutation, etc) it also verifies the auth on every query, i think this is secure but is it the recommended way?
14 replies
CCConvex Community
Created by EMILIANOM on 9/26/2024 in #support-community
Convex + Lucia auth + Svelte | Help
tasks.ts
export const get = queryWithAuth({
args: {},
handler: async (ctx) => {
const user = ctx.userSessionContext?.user;
if (!user) {
throw new ConvexError('Not authenticated');
}

const tasks = await ctx.db
.query('tasks')
.withIndex('by_user', (q) => q.eq('userId', user.id))
.collect();
return tasks.map((task) => ({ ...task, assigner: 'tom' }));
}
});
export const get = queryWithAuth({
args: {},
handler: async (ctx) => {
const user = ctx.userSessionContext?.user;
if (!user) {
throw new ConvexError('Not authenticated');
}

const tasks = await ctx.db
.query('tasks')
.withIndex('by_user', (q) => q.eq('userId', user.id))
.collect();
return tasks.map((task) => ({ ...task, assigner: 'tom' }));
}
});
withAuth.ts
export function queryWithAuth<ArgsValidator extends PropertyValidators, Output>({
args,
handler
}: QueryWithAuth<ArgsValidator, Output>) {
return query({
args: {
...args,
sessionId: v.union(v.null(), v.string())
},
handler: async (ctx, args: any) => {
const auth = getAuth(ctx.db as DatabaseWriter);
const userSessionContext = await getValidExistingSession(auth, args.sessionId);
return handler(
{ ...ctx, userSessionContext, auth, table: entsTableFactory(ctx, entDefinitions) },
args
);
}
});
}
export function queryWithAuth<ArgsValidator extends PropertyValidators, Output>({
args,
handler
}: QueryWithAuth<ArgsValidator, Output>) {
return query({
args: {
...args,
sessionId: v.union(v.null(), v.string())
},
handler: async (ctx, args: any) => {
const auth = getAuth(ctx.db as DatabaseWriter);
const userSessionContext = await getValidExistingSession(auth, args.sessionId);
return handler(
{ ...ctx, userSessionContext, auth, table: entsTableFactory(ctx, entDefinitions) },
args
);
}
});
}
Yeah that was it! I think that it is working now, do you think if im doing it the correcy way pls? not sure if i should check for null user inside every query / mutation or if there is a better way in order to have less code. Pushed the code to my github repo if you need to see the whole code, basically its the same repo as yours 😄
14 replies