TwelthDd
TwelthDd
TTCTheo's Typesafe Cult
Created by TwelthDd on 8/22/2024 in #questions
clerk auth default metadata
No description
7 replies
TTCTheo's Typesafe Cult
Created by TwelthDd on 8/22/2024 in #questions
clerk auth default metadata
after some messing around came up with a much simpler solution to use at least for the time being basically when retrieving the tokens in the navbar if it returns as undefined i run a nonblocking function to create the metadata and temporarily show the default amount
export async function GetTokenAmount(userId: string) {
const user = await clerkClient().users.getUser(userId);
const data = user.privateMetadata;
const tokenAmount = data.tokens as number;

if (tokenAmount === undefined) {
// Call CreateTokenMetadata but don't await it to make it non-blocking
CreateTokenMetadata(userId).catch((err) => {
console.error("Error creating token metadata:", err);
});
return 100; // Immediately return the default value without waiting
}

return tokenAmount;
}
export async function GetTokenAmount(userId: string) {
const user = await clerkClient().users.getUser(userId);
const data = user.privateMetadata;
const tokenAmount = data.tokens as number;

if (tokenAmount === undefined) {
// Call CreateTokenMetadata but don't await it to make it non-blocking
CreateTokenMetadata(userId).catch((err) => {
console.error("Error creating token metadata:", err);
});
return 100; // Immediately return the default value without waiting
}

return tokenAmount;
}
export async function CreateTokenMetadata(userId: string) {
await clerkClient.users.updateUserMetadata(userId, {
privateMetadata: {
tokens: 100,
},
});
}
export async function CreateTokenMetadata(userId: string) {
await clerkClient.users.updateUserMetadata(userId, {
privateMetadata: {
tokens: 100,
},
});
}
7 replies
TTCTheo's Typesafe Cult
Created by TwelthDd on 8/22/2024 in #questions
clerk auth default metadata
hi, thanks for the response i think i will try to go with the first solution you provided and in the little tokens ui default it to 100 tokens if no value is found with that user id whilst the webhook takes its time and if that doesn't work for whatever reason i will probably the third option
7 replies