get-convex / convex-svelte

https://convex-svelte.vercel.app
Apache License 2.0
23 stars 5 forks source link

Auth with JWT in Convex Svelte #13

Open COCPORN opened 1 week ago

COCPORN commented 1 week ago

I am running into some issues with auth using Convex Svelte. I was directed here from the Convex Discord. I feel like I am so close, so thank you for checking this out. This works:

...however...:

My "main" file looks like this:

<script lang="ts">
    import { PUBLIC_CONVEX_URL } from '$env/static/public';
    import { setupConvex, useConvexClient } from 'convex-svelte';
    import { SignIn, useClerkContext } from 'svelte-clerk';
    import * as Sidebar from '$lib/components/ui/sidebar/index.js';
    import AppSidebar from '$lib/components/app-sidebar.svelte';
    import '../app.css';

    let { children } = $props();

    const ctx = useClerkContext();
    const isLoaded = $derived(ctx.isLoaded);
    const user = $derived(ctx.user);

    setupConvex(PUBLIC_CONVEX_URL);

    $effect.pre(() => {
        const client = useConvexClient();
        client.setAuth(async () => {            
            const token = await ctx.session?.getToken();
            console.log('INSIDE setAuth', token);
            return token;
        });
    });
</script>

This code is created from a discussion on Discord #general. As I mentioned above the console.log-line outputs a valid JWT.

Thanks!