> ## Documentation Index
> Fetch the complete documentation index at: https://auth0.com/llms.txt
> Use this file to discover all available pages before exploring further.

> Learn how to customize the behavior of the Delegated Administration Extension using Extension Hooks.

# Delegated Administration: Extension Hooks

If you're a user assigned the **Delegated Admin - Administrator** role, you can manage the different Extension Hooks and queries that allow you to customize the behavior of the Delegated Administration extension.

To access the configuration area:

1. Log in to the Delegated Administration Dashboard.
2. Click on your name in the top-right corner. You'll see a dropdown menu; click on the **Configure** option.

The **Configuration** page to which you're redirected is where you can manage your Hooks and queries.

## Extension Hooks Context

The context (**ctx**) object will expose a few helpers and information about the current request. The following methods and properties are available in every Extension Hook:

* Logging
* Caching
* Custom Data
* Payload and Request
* Remote Calls

### Logging

To add a message to the Actions Real-time logs (which you can view using Actions Real-time Logs), call the **log** method:

```javascript lines theme={null}
function(ctx, callback) {
  ctx.log('Logging action:', ctx.payload.action);
  return callback();
}
```

### Caching

To cache something (such as a long list of departments), you can store it on the context's **global** object. This object will be available until the Actions Real-Time Logs container recycles.

```javascript lines theme={null}
function(ctx, callback) {
  ctx.global.departments = [ 'IT', 'HR', 'Finance' ];
  return callback();
}
```

### Custom data

You can store custom data within the extension. This field is limited to 400kb of data.

```javascript lines theme={null}
var data = {
departments: [ 'IT', 'HR', 'Finance' ]
};

ctx.write(data)
.then(function() {
    // ...
})
.catch(function(err) {
    // ...
});
```

To read the data:

```js lines theme={null}
ctx.read()
.then(function(data) {
    // ...
})
.catch(function(err) {
    // ...
});
```

### Payload and request

Each Extension Hook exposes the current payload or request with specific information. The request will always contain information about the user that is logged into the Users Dashboard:

```javascript lines theme={null}
function(ctx, callback) {
  ctx.log('Current User:', ctx.request.user);
  return callback();
}
```

### Remote calls

If you want to call an external service (such as an API) to validate data or to load memberships, you can do this using the `request` module.

```javascript lines theme={null}
function(ctx, callback) {
var request = require('request');
    request('http://api.mycompany.com/departments', function (error, response, body) {
        if (error) {
        return callback(error);
        }

        // ...
    });
}
```

### Hook contract

* `ctx`: The context object

  * `payload`: The payload object

    * `action`: The current action (for example, `delete:user`) that is being executed
    * `user`: The user on which the action is being executed
* `callback(error)`: The callback to which you can return an error if access is denied

### Sample use

Kelly manages the Finance department, and she should only be able to access users within her department.

```javascript lines theme={null}
function(ctx, callback) {
  if (ctx.payload.action === 'delete:user') {
    return callback(new Error('You are not allowed to delete users.'));
  }

  // Get the department from the current user's metadata.
  var department = ctx.request.user.app_metadata && ctx.request.user.app_metadata.department;
  if (!department || !department.length) {
    return callback(new Error('The current user is not part of any department.'));
  }

  // The IT department can access all users.
  if (department === 'IT') {
    return callback();
  }

  ctx.log('Verifying access:', ctx.payload.user.app_metadata.department, department);

  if (!ctx.payload.user.app_metadata.department || ctx.payload.user.app_metadata.department !== department) {
    return callback(new Error('You can only access users within your own department.'));
  }

  return callback();
}
```

If this hook is not configured, all users will be accessible.

Supported action names:

* `read:user`
* `delete:user`
* `reset:password`
* `change:password`
* `change:username`
* `change:email`
* `read:devices`
* `read:logs`
* `remove:multifactor-provider`
* `block:user`
* `unblock:user`
* `send:verification-email`

## Available Extension Hooks

The following Extension Hooks are available for use with the Delegated Administration Application:

* [Access Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-access-hook)
* [Custom Domain Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-custom-domain-hook)
* [Filter Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-filter-hook)
* [Memberships Query Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-memberships-query-hook)
* [Settings Query Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-settings-query-hook)
* [Write Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-write-hook)

## Learn more

* [Delegated Administration: Access Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-access-hook)
* [Delegated Administration: Custom Domain Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-custom-domain-hook)
* [Delegated Administration: Filter Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-filter-hook)
* [Delegated Administration: Memberships Query Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-memberships-query-hook)
* [Delegated Administration: Settings Query Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-settings-query-hook)
* [Delegated Administration: Write Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-write-hook)
