@joystick.js/node

req.context.if_logged_in()

How to use req.context.if_logged_in() to redirect logged-in users in your routes.

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

Example Usage

/index.server.js

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

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

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

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

API

Definition

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

Parameters

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