title: How can I get the user details in Auth0 using SvelteKit on the server side
body: <p>I am trying to authenticate a user with Auth0 service using SvelteKit, on the server side but keep getting the error <code>invalid_token</code>, <code>cannot read properties of undefined</code>:</p>
  <pre><code>outer {
    error: 'invalid_token',
    errorDescription: &quot;Cannot read properties of undefined (reading 'XMLHttpRequest')&quot;
  }
  </code></pre>
  <p>The login endpoint:</p>
  <pre><code>import * as auth0 from &quot;auth0-js&quot;;
  
  /** @type {import('@sveltejs/kit').RequestHandler} */
  export async function post({ request }) {
  
          const body = await request.json();
          const hash = body.hash;
          console.log(&quot;Sign-in.ts&quot;, hash);
  
          // Initialize the Auth0 application
          var webAuth = new auth0.WebAuth({
              domain: '&lt;domain&gt;',
              clientID: '&gt;clientid&gt;'
          });
          // Parse the URL and extract the Access Token
          webAuth.parseHash({hash: hash, state: &quot;rtre4&quot;, nonce: &quot;er324&quot;}, function (err, authResult) {
              if (err) {
                  return console.log(&quot;outer&quot;, err);
              }
              
              webAuth.client.userInfo(authResult.accessToken, function (innerErr, user) {
                  // This method will make a request to the /userinfo endpoint
                  // and return the user object, which contains the user's information,
                  // similar to the response below.
                  if (innerErr) {
                      return console.log(&quot;inner&quot;, innerErr);
                      
                  }
  
                  console.log(user);
              });
          });
  
          return {
              status: 200,
              body: {
                  message: &quot;OK&quot;
              }
          };
  };
  </code></pre>
  <p>I can successfully log the user in and redirect the user to my /login route with the access and id tokens as hash in the url.
  Next I am trying to get the user details. I want to get the user details in the server, so I send the complete hash to my <code>/api/login.ts</code> endpoint with a <code>POST</code> request, and call the <code>auth0-js</code> function <code>parseHash</code> in the server endpoint, which fails. The same code works on the client-side, though.</p>
  <p>The landing page:</p>
  <pre><code>&lt;script lang=&quot;ts&quot;&gt;
      import { onMount } from 'svelte';
  
      onMount(async () =&gt; {
          const hash = window.location.hash;
          const response = await fetch(&quot;/api/sign-in&quot;, {
                  method: &quot;POST&quot;,
                  headers: {
                      &quot;Content-Type&quot;: &quot;application/json&quot;
                  },
                  body: JSON.stringify({
                      hash: hash
                  })
              });
              console.log(response);
      });
  &lt;/script&gt;
  &lt;div class=&quot;content&quot;&gt;
      &lt;p&gt;Redirecting, please wait...&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  <p>Therefore I am thinking either the cookie somehow changes during stringifying it, or Sveltekit does not have XmlHttpRequest, but does anyone has further insight?</p>