Authentication

Explanation

Sometimes you will want to redirect based on the results of your resolve functions. For instance, you might see that a user is not authenticated and shouldn't be able to view a page.

A response() function can modify the response by setting a redirectTo property on its return object. Curi will automatically (unless configured not to) redirect to that location.

const routes = prepareRoutes([
  // ...,
  {
    name: 'Protected',
    path: 'super-secret',
    response: () => {
      if (!store.userIsAuthenticated) {
        return {
          redirectTo: { name: "Login" },
          status: 302
        };
      }
    }
  },
  {
    name: 'Login',
    path: 'login',
    ...
  }
]);

Live Demo

Use the three buttons at the top of the Sandbox to toggle view modes. Clicking the menu button in the top left corner opens a menu to switch between files.

On GitHub

If you want to run this code locally, the source code is available on GitHub here.