-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasana_server.js
90 lines (81 loc) · 3.39 KB
/
asana_server.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
//Include the Asana SDK (available on server side only)
asanaSDK = Npm.require('asana');
//Declare a Server Side loginWithAsana Method
//This is function is called as part of the OAuth follow implemented in Meteor
Meteor.loginWithAsana = function(options, callback) {
if (! callback && typeof options === "function") {
callback = options;
options = null;
}
var credentialRequestCompleteCallback = Accounts.oauth.credentialRequestCompleteHandler(callback);
Asana.requestCredential(options, credentialRequestCompleteCallback);
};
//Triggers once the user returns from Asana
//query argument cotains a "code" field with an access code that we will now convert into a token.
//the returned value will be added to the created user records under Meteor.users
Oauth.registerService('asana', 2, null, function(query) {
var config = ServiceConfiguration.configurations.findOne({service: 'asana'});
if (!config) throw new ServiceConfiguration.ConfigError();
var response = getTokenResponse(query);
return {
serviceData: {
id: response.data.id,
email: response.data.email,
accessToken: response.access_token,
refreshToken: response.refresh_token
},
options: {
profile: {
name: response.data.name,
asana:{}
},
emails:response.data.email
}
};
});
//Bind into the onCreateUser method, this is trigger once a new user is created in the system.
//If the user has the asana service, we query Asana for /user/me endpoint using Asana's JS SDK, and append the returned information into their "profile" fields
//This information will later be available client side by the return value of "Meteor.user();"
Accounts.onCreateUser(function(options, user) {
if (!user.services.hasOwnProperty('asana')) return user;
if (options.profile) user.profile = options.profile;
var accessToken = user.services.asana.accessToken;
var config = ServiceConfiguration.configurations.findOne({service: 'asana'});
if (!config) {throw new ServiceConfiguration.ConfigError();}
var asanaClient = asanaSDK.Client.create({clientId: config.clientId,clientSecret: config.clientSecret,redirectUri: config.redirectUri}).useOauth({credentials:accessToken});
asanaClient.users.me()
.then(Meteor.bindEnvironment(function(response){
var currenAsanaUserDetails = {
photo:response.photo,
workspaces: response.workspaces
};
Meteor.users.update(user._id, {$set: {"profile.asana": currenAsanaUserDetails}});
}))
.catch(function(error){
throw _.extend(new Error("Failed to retrieve user"));
});
return user;
});
var getTokenResponse = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'asana'});
if (!config) {throw new ServiceConfiguration.ConfigError();}
var response;
try {
response = HTTP.post(
"https://app.asana.com/-/oauth_token", {
params: {
code: query.code,
client_id: config.clientId,
client_secret: config.clientSecret,
redirect_uri: config.redirectUri,
grant_type: 'authorization_code'
}
});
if (!response.hasOwnProperty('statusCode') || (response.hasOwnProperty('statusCode') && response.statusCode != 200)){
throw response.content;
}
} catch(error){
throw _.extend(new Error("Failed to complete OAuth handshake with Asana."));
}
return JSON.parse(response.content);
};