-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
101 lines (91 loc) · 3.5 KB
/
login.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Login page
//
var jsforce = require('jsforce');
exports.View =
{
title: "Login",
elements:
[
{ control: "image", resource: "{imgLogo}", width: "227" },
{ control: "text", value: "Login", font: { size: 16 }, margin: { bottom: 16 } },
{ control: "text", value: "Username", fontsize: 12, margin: { bottom: 0 } },
{ control: "edit", binding: "username", placeholder: "username", width: 200 },
{ control: "text", value: "Password", fontsize: 12, margin: { bottom: 0 } },
{ control: "password", binding: "password", placeholder: "password", width: 200 },
{ control: "button", caption: "Login", icon: "perm_identity", width: 125, binding: "login" },
]
}
exports.InitializeViewModel = function(context, session)
{
var viewModel =
{
imgLogo: Synchro.getResourceUrl(context, "salesforcelogo.png"),
username: "",
password: "",
}
return viewModel;
}
exports.Commands =
{
login: function * (context, session, viewModel)
{
try
{
var conn;
// You can change loginUrl to connect to alternate endpoint (sandbox or prerelease env).
//
var loginUrl = Synchro.getConfig(context, "SF_LOGIN_URL");
// OAuth params (if set)
//
var clientId = Synchro.getConfig(context, "SF_CLIENT_ID");
var clientSecret = Synchro.getConfig(context, "SF_CLIENT_SECRET");
var redirectUri = Synchro.getConfig(context, "SF_REDIRECT_URI");
// With either login mechanism, user security token may need to be appended to password.
//
if (clientId && clientSecret && redirectUri)
{
// Login using oAuth (user/pass)
//
// Note: With oAuth, you can remove the user security token requirment for a the connected
// app by either being in the configured "Trusted IP Range" of the app or by setting
// the "IP Relaxation" value to "Relax IP restrictions" for the app.
//
console.log("Using OAuth with cliendId: ", clientId);
conn = new jsforce.Connection({
oauth2 : {
loginUrl : loginUrl,
clientId : clientId,
clientSecret : clientSecret,
redirectUri : redirectUri
}
});
}
else
{
// Login using plain user/pass
//
console.log("Using plain user/pass auth");
conn = new jsforce.Connection(
{
loginUrl : loginUrl
});
}
var userInfo = yield Synchro.yieldAwaitable(context, function(callback)
{
conn.login(viewModel.username, viewModel.password, callback)
});
// logged in user
console.log("Logged in user id: " + userInfo.id + ", organizationId: " + userInfo.organizationId);
// Store Salesforce access credentials for future reqests...
//
session.sf_instanceUrl = conn.instanceUrl;
session.sf_accessToken = conn.accessToken;
return Synchro.navigateTo(context, "main");
}
catch (err)
{
console.log(err);
return Synchro.showMessage(context, { title: "Login Error", message: err.message });
}
},
}