@joystick.js/node

req.context.user

How to access the authenticated user on req.context.user in your routes.

When a user logs in to the app, for each request, Joystick will attempt to authenticate the user via the joystick_login_token cookie (if present). If a user exists, the accounts middleware will automatically assign that user’s data to the req.context.user object.

Example Usage

/index.server.js

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

joystick.app({
  routes: {
    '/': async (req = {}, res = {}) => {
      return res.render('ui/pages/index/index.js', {
        layout: 'ui/layouts/app/index.js',
        props: {
          has_user: !!req.context.user,
        },
      });
    },
  },
});

API

Definition

The req.context.user object contains the authenticated user’s data for the current request. The structure of this object depends on your database provider.

MongoDB

{
  _id: string,
  emailAddress: string,
  password: string (hash),
  username: string,
  sessions: array[object],
  language: string,
}

PostgreSQL

{
  user_id: string,
  email_address: string,
  password: string (hash),
  username: string,
  sessions: array[object],
  language: string,
}

Parameters

_id string
The ID for the user. Set if the users database is MongoDB.
user_id string
The ID for the user. Set if the users database is PostgreSQL.
emailAddress string
The user's email address.
password string
The user's password as a SHA-256 hash.
username string
If set, the user's username.
sessions array[object]
The user's existing login sessions.
language string
If set, the user's language as a combination of the ISO-639 language code with the ISO-3166 country code (e.g., en-US, de-DE, es-ES, or ja-JP).