Woodshop
Search…
Impersonate a user
This example shows you how to create a Smart Action "Impersonate" to login as one of your customers.
It can be useful to help your customers debug an issue or to get a better understanding of what they see on their account (in your app).

Requirements

    An admin backend running on forest-express-sequelize/forest-express-mongoose

How it works

Directory: /models

This directory contains the users.js file where the model is declared.
Sequelize
Mongoose
/models/users.js
1
module.exports = (sequelize, DataTypes) => {
2
const { Sequelize } = sequelize;
3
const Users = sequelize.define('users', {
4
email: {
5
type: DataTypes.STRING,
6
},
7
createdAt: {
8
type: DataTypes.DATE,
9
},
10
//...
11
}, {
12
tableName: 'users',
13
timestamps: false,
14
schema: process.env.DATABASE_SCHEMA,
15
});
16
17
Users.associate = (models) => {
18
};
19
20
return Users;
21
};
Copied!
/models/users.js
1
const mongoose = require('mongoose');
2
3
const schema = mongoose.Schema({
4
'email': String,
5
'createdAt': Date,
6
...
7
}, {
8
timestamps: false,
9
});
10
11
module.exports = mongoose.model('users', schema, 'users');
Copied!

Directory: /forest

This directory contains the users.js file where the Smart Action Impersonateis declared.
Sequelize
Mongoose
/forest/users.js
1
const { collection } = require('forest-express-sequelize');
2
3
collection('users', {
4
actions: [{
5
name: 'Impersonate',
6
type: 'single'
7
}],
8
});
Copied!
/forest/users.js
1
const { collection } = require('forest-express-mongoose');
2
3
collection('users', {
4
actions: [{
5
name: 'Impersonate',
6
type: 'single'
7
}],
8
});
Copied!

Directory: /routes

This directory contains the users.js file where the implementation of the route is handled. The POST /forest/actions/impersonate API call is triggered when you click on the Smart Action in the Forest UI.
Sequelize
Mongoose
routes/users.js
1
router.post('/actions/impersonate',
2
(req, res) => {
3
let userId = req.body.data.attributes.ids[0];
4
5
response.send({
6
webhook: { // This is the object that will be used to fire http calls.
7
url: 'https://my-app-url/login', // The url of the company providing the service.
8
method: 'POST', // The method you would like to use (typically a POST).
9
headers: { }, // You can add some headers if needed (you can remove it).
10
body: { // A body to send to the url (only JSON supported).
11
adminToken: 'your-admin-token',
12
},
13
},
14
success: `Impersonating user ${userId}`, // The success message that will be toasted.
15
redirectTo: 'https://my-app-url/', // Force the redirection to your app if needed.
16
});
17
18
});
19
20
module.exports = router;
Copied!
/routes/users.js
1
router.post('/actions/impersonate',
2
(req, res) => {
3
let userId = req.body.data.attributes.ids[0];
4
5
response.send({
6
webhook: { // This is the object that will be used to fire http calls.
7
url: 'https://my-app-url/login', // The url of the company providing the service.
8
method: 'POST', // The method you would like to use (typically a POST).
9
headers: { }, // You can add some headers if needed (you can remove it).
10
body: { // A body to send to the url (only JSON supported).
11
adminToken: 'your-admin-token',
12
},
13
},
14
success: `Impersonating user ${userId}`, // The success message that will be toasted.
15
redirectTo: 'https://my-app-url/', // Force the redirection to your app if needed.
16
});
17
18
});
19
20
module.exports = router;
Copied!
This is useful for authentication using cookies. By using this exemple, you're performing the login request directly from the browser. Thus, the cookies will be automatically sent from your own service to the browser (as you'd normally do with your own app).
Last modified 1yr ago