forked from wayneashleyberry/wunderline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwunderline-auth.js
executable file
·52 lines (48 loc) · 1.55 KB
/
wunderline-auth.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
var app = require('commander')
var inquirer = require('inquirer')
var request = require('request')
var Configstore = require('configstore')
var conf = new Configstore('wunderline')
var wrap = require('wordwrap')(80)
app
.description('Authenticate Wunderline')
.parse(process.argv)
var questions = [
{
name: 'client_id',
message: 'CLIENT ID',
validate: function (input) {
return input.length > 0
}
},
{
name: 'access_token',
message: 'ACCESS TOKEN',
validate: function (input) {
return input.length > 0
}
}
]
console.log(wrap('Please create a Wunderlist Application before you proceed, you can do so over here: https://developer.wunderlist.com/apps/new, once that is done please enter your access token and client id below.'))
inquirer.prompt(questions).then(function (answers) {
request.get({
json: true,
url: 'https://a.wunderlist.com/api/v1/user',
headers: {
'X-Access-Token': answers.access_token,
'X-Client-ID': answers.client_id
}
}, function (err, res, body) {
if (err || body.error) {
console.error(JSON.stringify(err || body.error, null, 2))
process.exit(1)
} else if (body.invalid_request || body.unauthorized) {
console.error('Authentication failed (wrong CLIENT ID and/or ACCESS TOKEN)!')
process.exit(1)
}
conf.set('authenticated_at', new Date())
conf.set('client_id', answers.client_id)
conf.set('access_token', answers.access_token)
console.log('Thanks ' + body.name.split(' ')[0] + ', Wunderline has been authenticated.')
})
})