Handling authorization

I’m looking to use client-side authorization via Auth0. Check out this minimal demo to see what the issue is. Here’s the event handler I’ve installed:

document.documentElement.addEventListener(
  "turbo:before-fetch-request",
  event => {
    console.log(event)
    if (token) {
      event.detail.fetchOptions.headers["Authorization"] = `Bearer ${token}`;
    }
  }
);

I’m populating token when the user logs in, and calling Turbo.clearCache() to re-fetch all visible content. However, I’m not seeing anything load, and there are no error messages either. What am I doing wrong?

Of course I figured it out literally minutes later. You need to call Turbo.visit(document.location, {action: 'replace'}) after clearing the cache. Leaving this here in case someone else runs into the same issue.

1 Like