> For the complete documentation index, see [llms.txt](https://conductor.js.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://conductor.js.org/guides/example-use-cases.md).

# example use cases

## decoding JWTs

{% code title="decodeToken.js" %}

```javascript
const decodeToken = (req) => {
  const { authorization } = req.headers;
  if (!authorization) {
    throw new OAuth2Error('Missing Authorization header', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN);
  }
  // Strip Bearer part
  const token = authorization.split(' ')[1];
  if (!token) {
    throw new OAuth2Error('Missing access token', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN);
  }
  // base64 decode
  const decoded = jwt.decode(token);
  // Check that the token contains a sub claim i.e an userId
  if (!decoded || !decoded.sub) {
    throw new OAuth2Error('Invalid access token', 401, AUTHORIZATION_INVALID_ACCESS_TOKEN);
  }
  return decoded;
};
```

{% endcode %}

{% code title="getAuthorization.js" %}

```javascript
const getAuthorization = req => {
  const { authorization } = req.headers;
  if (!authorization) {
    throw new OAuth2Error('Missing Authorization header', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN);
  }
  return authorization
}
```

{% endcode %}

{% code title="getToken.js" %}

```javascript
const getToken = authorization => {
  // Strip Bearer part
  const token = authorization.split(' ')[1];
  if (!token) {
    throw new OAuth2Error('Missing access token', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN);
  }
  return token
}
```

{% endcode %}

{% code title="getDecodedToken.js" %}

```javascript
const getDecodedToken = token => {
  // base64 decode
  const decoded = jwt.decode(token);
  // Check that the token contains a sub claim i.e an userId
  if (!decoded || !decoded.sub) {
    throw new OAuth2Error('Invalid access token', 401, AUTHORIZATION_INVALID_ACCESS_TOKEN);
  }
  return decoded;
}
```

{% endcode %}

{% tabs %}
{% tab title="decodeToken.js" %}

```javascript
const decodeToken = (req) => {
  const authorization = getAuthorization(req)
  const token = getToken(authorization)
  return decodeToken(token)
};
```

{% endtab %}

{% tab title="getAuthorization.js" %}

```javascript
const getAuthorization = req => {
  const { authorization } = req.headers;
  if (!authorization) {
    throw new OAuth2Error('Missing Authorization header', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN);
  }
  return authorization
}
```

{% endtab %}

{% tab title="getToken.js" %}

```javascript
const getToken = authorization => {
  // Strip Bearer part
  const token = authorization.split(' ')[1];
  if (!token) {
    throw new OAuth2Error('Missing access token', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN);
  }
  return token
}
```

{% endtab %}

{% tab title="getDecodedToken.js" %}

```javascript
const decodeToken = token => {
  // base64 decode
  const decoded = jwt.decode(token);
  // Check that the token contains a sub claim i.e an userId
  if (!decoded || !decoded.sub) {
    throw new OAuth2Error('Invalid access token', 401, AUTHORIZATION_INVALID_ACCESS_TOKEN);
  }
  return decoded;
}
```

{% endtab %}
{% endtabs %}

{% code title="getAuthorization.js" %}

```javascript
import { compose, get, ifElse, not } from 'conductor'

const getAuthorization = compose(
  ifElse(not, () => { throw new OAuth2Error('Missing Authorization header', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN) }),
  get('authorization'),
  get('headers'),
)
```

{% endcode %}

{% code title="getToken.js" %}

```javascript
import { compose, get, ifElse, split } from 'conductor'

const getToken = compose(
  ifElse(not, () => { throw new OAuth2Error('Missing access token', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN) }),
  get(1),
  split(' '),
)
```

{% endcode %}

{% code title="getDecodedToken.js" %}

```javascript
const getDecodedToken = compose(
  ifElse(or(not, compose(not, get('sub'))), () => { throw new OAuth2Error('Invalid access token', 401, AUTHORIZATION_INVALID_ACCESS_TOKEN) })
  jwt.decode,
)
```

{% endcode %}

{% tabs %}
{% tab title="decodeToken.js" %}

```javascript
import { compose } from 'conductor'
import getAuthorization from './getAuthorization'
import getToken from './getToken'
import getDecodedToken from './getDecodedToken'

const decodeToken = compose(getDecodedToken, getToken, getAuthorization)
```

{% endtab %}

{% tab title="getAuthorization.js" %}

```javascript
import { compose, get, ifElse, not } from 'conductor'

const getAuthorization = compose(
  ifElse(not, () => { throw new OAuth2Error('Missing Authorization header', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN) }),
  get('authorization'),
  get('headers'),
)
```

{% endtab %}

{% tab title="getToken.js" %}

```javascript
import { compose, get, ifElse, split } from 'conductor'

const getToken = compose(
  ifElse(not, () => { throw new OAuth2Error('Missing access token', 401, AUTHORIZATION_MISSING_ACCESS_TOKEN) }),
  get(1),
  split(' '),
)
```

{% endtab %}

{% tab title="getDecodeToken.js" %}

```javascript
const getDecodedToken = compose(
  ifElse(or(not, compose(not, get('sub'))), () => { throw new OAuth2Error('Invalid access token', 401, AUTHORIZATION_INVALID_ACCESS_TOKEN) })
  jwt.decode,
)
```

{% endtab %}
{% endtabs %}
