Auth context

Auth context is a request scoped context that stores auth user and token.

Using the context you can retrieve info about the current user in any place of code.

Token from the auth context is used in service connectors as default way to authorize requests between services.

Usage

import { AuthContext } from '@titanhouse/global';

// First of all we need to initialize context and bind it to request
// For that purpose we have express middleware
app.use(AuthContext.middleware);

...

async function request(url) {
  console.log('Auth user:', AuthContext.getUser());
  return axios.get(
    url,
    { headers: AuthContext.getAuthHeader() }
  );
}

Auth context implementation is based on cls-hooked lib that helps it to survive between nodejs context switches.

results matching ""

    No results matching ""