@joystick.js/node

req.context.if_not_logged_in()

How to use req.context.if_not_logged_in() to redirect users who are not logged in.

To aid in the process of routing users to the correct location, Joystick includes a helper function req.context.if_not_logged_in() to trigger custom behavior if a user is not logged in.

Example Usage

/index.server.js

import joystick from '@joystick.js/node';

joystick.app({
  routes: {
    '/dashboard': async (req = {}, res = {}) => {
      req.context.if_not_logged_in('/login', () => {
        res.render('ui/pages/dashboard/index.js');
      });
    },
    '/billing': async (req = {}, res = {}) => {
      req.context.if_not_logged_in('/dashboard', () => {
        res.render('ui/pages/billing/index.js');
      });
    },
  },
});

In the example above, we call req.context.if_not_logged_in(), passing two arguments:

  1. The path the user should be redirected to if they are not logged in.
  2. A callback function describing what to do if they are logged in.

API

Definition

req.context.if_not_logged_in(redirect_path: string, callback: function) => void

Parameters

redirect_path string required
If there is not a current user, redirect them to this path.
callback function required
If there is a current user, call this function.