# Authentication

Poser contains a mock of DataCamp's authentication method out of the box.

All requests sent to Poser will get a x-user_id header injected, to mimic the Kong JWT plugin.

Poser keeps a "current user id" that can be changed at any time to "login" another user. Logout is also possible.

The current user id is displayed when Poser starts. 123 is the default user id.

# Using user_id in API mocks

# GraphQL

In graphQL resolvers, the ctx argument is the Express Request object. The user id can be accessed by reading directly from the x-user_id header:

const resolvers = {
  Query: {
    currentUser: (src, args, ctx) => store.get('User', ctx.get('x-user_id'))
  }
}

# Rest

In rest, the x-user_id header can simply be read from the req object, exactly like an actual service would do:

router.get('/foo', (req, res) => {
  res.send(
    `Hello user ${req.get('x-user_id')}`
  )
})

# Updating the current user_id

# Via UI

Poser's frontpage (http://localhost:1000) has a form to update the user id:

A screenshot of the authorization section of Poser's frontpage
A screenshot of the authorization section of Poser's frontpage

# Via HTTP

The route /_kong/user_id/<newId> allows you to update the current user id.

For example, GET http://localhost:1000/_kong/user_id/99 will set the user id to 99.

# Via CLI

When starting poser, pass --user_id to set the initial user id.

For example, yarn start --user_id=99 will set the initial user id to 99.

# Overriding the current user_id

The current user id can also be overriden (but won't be updated) by passing the x-user_id header.

For example, assuming the current user id is 123, the following request will force a user id of 99, but only for that request:

fetch('http://localhost:1000/my-service/foo', {
  headers: {
    'x-user_id': 99
  }
})

# Disabling user_id

Poser will always include the x-user_id header unless it is set to none, false or null. This allows to mock logged-out users.

For example, http://localhost:1000/_kong/user_id/none will disable user_id injection.