NazCodeland
NazCodeland
Explore posts from servers
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
so now I have if (token) client.setAuth(token); set on client from ConvexHTTPClient which is the same one being used by the wrapper functions that call the convex functions. Still logs null for const identity = await ctx.auth.getUserIdentity();`
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
I noticed that within +page.svelte the client was from useConvexClient whereas, the wrapper that are getting called from the class setters, are using client that is from ConvexHttpClient so, I removed the code within src/route/+page.svelte in favour of this within +page.svelte.ts
import type { PageServerLoad } from './$types';
import { PUBLIC_CONVEX_URL } from '$env/static/public';
import { ConvexHttpClient } from 'convex/browser';
import { api } from '../convex/_generated/api';

export const load: PageServerLoad = async ({ locals }) => {
const client = new ConvexHttpClient(PUBLIC_CONVEX_URL);

const token = await locals.auth.getToken({ template: 'convex' });
console.log("token", token);
if (token) client.setAuth(token);

const initial = await client.query(api.queries.getUsers, {});
return { initial };
};
import type { PageServerLoad } from './$types';
import { PUBLIC_CONVEX_URL } from '$env/static/public';
import { ConvexHttpClient } from 'convex/browser';
import { api } from '../convex/_generated/api';

export const load: PageServerLoad = async ({ locals }) => {
const client = new ConvexHttpClient(PUBLIC_CONVEX_URL);

const token = await locals.auth.getToken({ template: 'convex' });
console.log("token", token);
if (token) client.setAuth(token);

const initial = await client.query(api.queries.getUsers, {});
return { initial };
};
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
oops
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
by the time shareVisibility is called the user is logged in and I have a clerkId, but to answer your other question and how do you ensure isAuthenticated is true in the client before calling it, I don't think I am, I assumed that would be set to true because I am logged in to clerk
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
the logic might be broken within this setter but I just wanted to show you how its being called
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
I have this wrapper
export async function updateConvexShareVisibility(
userId: Id<"users">,
payload: Partial<WithoutSystemFields<Doc<"users">>>
) {
if (payload.shareVisibility) {
await client.mutation(api.mutations.updateShareVisibility, {
userId,
shareVisibility: payload.shareVisibility
});
}
};
export async function updateConvexShareVisibility(
userId: Id<"users">,
payload: Partial<WithoutSystemFields<Doc<"users">>>
) {
if (payload.shareVisibility) {
await client.mutation(api.mutations.updateShareVisibility, {
userId,
shareVisibility: payload.shareVisibility
});
}
};
which I calling from a setter within a class
set shareVisibility(value) {
// avoid double clicks
if (this.shareVisibility === value) return;

this.#shareVisibility = value;
updateLocalStorage('appState', { shareVisibility: value });

const sharingLocation = this.shareVisibility !== 'offline';


// [✅] convex user
// [✅] clerk user
if (this.convexId && this.clerkId) {
console.log("1");
updateConvexShareVisibility(this.convexId, this.buildConvexUser());
}

// [✅] convex user
// [] clerk user
else if (this.convexId) {
console.log("2");
updateConvexShareVisibility(this.convexId, this.buildConvexUser());
}

// [] convex user
// [] clerk user
else if (sharingLocation && !this.convexId) {
console.log("3");
createConvexUser(this.buildConvexUser());
}
}
set shareVisibility(value) {
// avoid double clicks
if (this.shareVisibility === value) return;

this.#shareVisibility = value;
updateLocalStorage('appState', { shareVisibility: value });

const sharingLocation = this.shareVisibility !== 'offline';


// [✅] convex user
// [✅] clerk user
if (this.convexId && this.clerkId) {
console.log("1");
updateConvexShareVisibility(this.convexId, this.buildConvexUser());
}

// [✅] convex user
// [] clerk user
else if (this.convexId) {
console.log("2");
updateConvexShareVisibility(this.convexId, this.buildConvexUser());
}

// [] convex user
// [] clerk user
else if (sharingLocation && !this.convexId) {
console.log("3");
createConvexUser(this.buildConvexUser());
}
}
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
Followed all the react agnostic steps from https://docs.convex.dev/auth/clerk
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
within all my funcitons, such as
export const updateShareVisibility = mutation({
args: {
userId: v.id("users"),
shareVisibility: schema.tables.users.validator.fields.shareVisibility
},
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
console.log(identity) // logs null
...
export const updateShareVisibility = mutation({
args: {
userId: v.id("users"),
shareVisibility: schema.tables.users.validator.fields.shareVisibility
},
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
console.log(identity) // logs null
...
I've refreshed the page after signing into clerk just incase, it still doesn't recognize that I set clent.setAuth()
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
it logs isAuthenticated: true in devtools when I sign into clerk
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
I'll share this for now, maybe making a repo is unncessary since Im just doing this: src/route/+layout.svelte
<script>
import { setupConvex } from 'convex-svelte';
import { PUBLIC_CONVEX_URL, PUBLIC_CLERK_PUBLISHABLE_KEY } from '$env/static/public';
setupConvex(PUBLIC_CONVEX_URL);

let { data } = $props();

</script>


<ClerkProvider {...data} publishableKey={PUBLIC_CLERK_PUBLISHABLE_KEY}>
...
</ClerkProvider>
<script>
import { setupConvex } from 'convex-svelte';
import { PUBLIC_CONVEX_URL, PUBLIC_CLERK_PUBLISHABLE_KEY } from '$env/static/public';
setupConvex(PUBLIC_CONVEX_URL);

let { data } = $props();

</script>


<ClerkProvider {...data} publishableKey={PUBLIC_CLERK_PUBLISHABLE_KEY}>
...
</ClerkProvider>
src/route/+page.svelte
<script>
import { useConvexClient } from 'convex-svelte';
import { useClerkContext } from 'svelte-clerk/client';


const client = useConvexClient();
const ctx = useClerkContext();



const fetchToken = async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => {
try {
const token = await ctx.session?.getToken({ template: 'convex' });
return token || null;
} catch (error) {
console.error('Error fetching token:', error);
return null;
}
};


$effect(() => {
client?.setAuth(fetchToken, (isAuthenticated: boolean) => {
if (isAuthenticated) {
console.log('isAuthenticated:', isAuthenticated);
}
});
});
</script>
<script>
import { useConvexClient } from 'convex-svelte';
import { useClerkContext } from 'svelte-clerk/client';


const client = useConvexClient();
const ctx = useClerkContext();



const fetchToken = async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => {
try {
const token = await ctx.session?.getToken({ template: 'convex' });
return token || null;
} catch (error) {
console.error('Error fetching token:', error);
return null;
}
};


$effect(() => {
client?.setAuth(fetchToken, (isAuthenticated: boolean) => {
if (isAuthenticated) {
console.log('isAuthenticated:', isAuthenticated);
}
});
});
</script>
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
Sure, I'll try to make a separate repo later today
32 replies
CCConvex Community
Created by Sam Whitmore on 7/11/2024 in #support-community
Convex Auth: Implementation with Svelte(Kit)
hey, is there there an example of this in code by any chance? - I've tried it and const identity = await ctx.auth.getUserIdentity() is returning null
32 replies
CDCloudflare Developers
Created by NazCodeland on 4/6/2024 in #workers-help
storing stateful WebSockets connection
I figured out what I was confused about
6 replies
CDCloudflare Developers
Created by NazCodeland on 4/6/2024 in #workers-help
storing stateful WebSockets connection
was something else, sorry for the late reply but thanks for the support
6 replies
CDCloudflare Developers
Created by NazCodeland on 4/6/2024 in #workers-help
storing stateful WebSockets connection
But, Durable Objects can store stateful information it seems. I am wondering why is it that Durable Objects are able to store stateful information given that it is not persisted in the memory of the running application
6 replies
CDCloudflare Developers
Created by NazCodeland on 4/4/2024 in #workers-help
How access 'env' in the WebSockets Template
hmm, you're right, it seems to be working - awaiting further confirmation lol
8 replies
CDCloudflare Developers
Created by NazCodeland on 4/4/2024 in #workers-help
How access 'env' in the WebSockets Template
so if you define it with, the new syntax
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
..
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
..
it won't work as expected because, from the client-side when you invoke websocket.send(), I don't think that will invoke the fetch on the Worker
8 replies
CDCloudflare Developers
Created by NazCodeland on 4/4/2024 in #workers-help
How access 'env' in the WebSockets Template
I hope I am wrong but I think you can't use the new syntax because its a WebSocket connection
8 replies
CDCloudflare Developers
Created by NazCodeland on 4/4/2024 in #workers-help
How access 'env' in the WebSockets Template
I've tried but I can't get it to work.
8 replies
CDCloudflare Developers
Created by NazCodeland on 4/4/2024 in #workers-help
How access 'env' in the WebSockets Template
I want to access env so I can access MY_KV_NAMESPACE
8 replies