Hi @aleksandra,
Welcome to the Auth0 Community!
The issue should be caused by your attempt to read the user state from the getUser() request, which has not yet set the session cookie in order for your server-side handlers to read it, resulting an undefined user object.
You can try verifying the returned object of your completeInteractiveLogin function which you should be able to use immediately instead of relying on cookies. The code should look something like this:
// server/routes/auth/callback.ts
export default defineEventHandler(async (event) => {
const auth0Client = useAuth0(event);
const auth0ClientOptions = event.context.auth0ClientOptions;
const result = await auth0Client.completeInteractiveLogin(
new URL(event.node.req.url ?? ", auth0ClientOptions.appBaseUrl)
);
const user = result.user || result.session?.user;
const { appState } = result;
if (user) {
const config = useRuntimeConfig();
try {
await $fetch(config.myApiUrl, {
method: 'post',
body: {
id: String(user.sub),
email: String(user.email),
},
});
} catch (error) {
console.error('API Side-effect failed', error);
}
}
return sendRedirect(event, appState?.returnTo ?? '/');
});
Alternatively, you can try decoding the ID token returned by the completeInteractiveLogin function:
import { jwtDecode } from "jwt-decode";
const result = await auth0Client.completeInteractiveLogin(...);
if (result.idToken) {
const claims = jwtDecode(result.idToken);
const userId = claims.sub;
const email = claims.email;
// proceed with the API call
}
I hope this helps and if you have further questions please let me know!
Best regards,
Remus