imightbejesus
imightbejesus5mo ago

Auth0 with convex in vanilla js PWA.

Hello. I am trying to wire up convex with auth0 in a vanilla js progressive web app. Docs seem to only cover this for a react app, and I am having trouble getting this to work in vanilla js pwa. I have added auth.config.ts file with my Auth0 provider (specifying domain and applicationID). Auth through Auth0 works fine, as I am able to get logged in. In my convex query I added the following check:
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
if (identity === null) {
throw new Error("Unauthenticated call");
}
}
handler: async (ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
if (identity === null) {
throw new Error("Unauthenticated call");
}
}
However the identity in the code above is always null, even though the user is logged in. What am I missing? Thanks in advance.
1 Reply
Michal Srb
Michal Srb5mo ago
Which client SDK are you using? ConvexClient has a setAuth function which you need to call with a fetcher for the Auth0 token:
async function fetchToken({ forceRefreshToken }: { forceRefreshToken: boolean }) => {
try {
const response = await getAccessTokenSilently({
detailedResponse: true,
cacheMode: forceRefreshToken ? "off" : "on",
});
return response.id_token as string;
} catch (error) {
return null;
}
}
client.setAuth(fetchToken);
async function fetchToken({ forceRefreshToken }: { forceRefreshToken: boolean }) => {
try {
const response = await getAccessTokenSilently({
detailedResponse: true,
cacheMode: forceRefreshToken ? "off" : "on",
});
return response.id_token as string;
} catch (error) {
return null;
}
}
client.setAuth(fetchToken);

Did you find this page helpful?