-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (66 loc) · 2.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const express = require('express')
const bodyParser = require('body-parser')
const dotenv = require('dotenv')
const jsforce = require('jsforce')
const app = express()
const PORT = process.env.PORT || 5000
dotenv.config();
const AuthenticationClient = require('auth0').AuthenticationClient;
const auth0 = new AuthenticationClient({
domain: process.env.AUTH0_DOMAIN,
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET
});
app.listen(PORT, () => console.log(`Listening on ${PORT}`))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.post('/login', async (req, res, next) => {
if (!process.env.SALESFORCE_LOGIN_URL.endsWith('.salesforce.com')) {
res.status(400).send({
"error": "invalid_environment_variable",
"error_description": "SALESFORCE_LOGIN_URL must end with '.salesforce.com'. Contact your administrator to see Heroku setting."
});
}
if (!req.body || !req.body.email || !req.body.password) {
res.status(401).send({
"error": "missing_required_fields",
"error_description": "email or password is not specified, or invalid format."
});
}
try {
await auth0.oauth.passwordGrant({
username: req.body.email,
password: req.body.password,
realm: 'Username-Password-Authentication'
});
await createSalesforceConnection(res);
} catch (error) {
console.log(error);
// Auth0 throws an error as follows.
// error = { statusCode: 401, message: '{"error":"invalid_grant", "error_description":"Wrong email or password." }'
const message = JSON.parse(error.message);
res.status(error.statusCode).send(message);
}
})
async function createSalesforceConnection(res) {
try {
const conn = new jsforce.Connection({
loginUrl: process.env.SALESFORCE_LOGIN_URL
});
await conn.login(
process.env.SALESFORCE_INTEGRATION_USERNAME,
process.env.SALESFORCE_INTEGRATION_PASSWORD + process.env.SALESFORCE_INTEGRATION_TOKEN
);
const response = {
"access_token": conn.accessToken,
"instance_url": conn.instanceUrl,
};
res.send(response);
} catch (error) {
console.error(error);
return Promise.reject({
statusCode: 403,
message: `{"error":"invalid_salesforce_connection", "error_description":"${error.message}"}`,
});
}
}