Authentication

Explanation

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

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

const routes = prepareRoutes({
  routes: [
    // ...,
    {
      name: 'Protected',
      path: 'super-secret',
      respond: () => {
        if (!store.userIsAuthenticated) {
          return {
            redirect: {
              name: "Login"
            },
            meta: {
              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.