-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
120 lines (109 loc) · 4.88 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var express = require('express');
var request = require('superagent');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.use(express.static('views'));
var mailchimpInstance = 'xxx',
listUniqueId = 'xxxxxxxxxxxx',
mailchimpApiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxx',
mailchimpClientId = 'xxxxxxxxxxxx';
mailchimpSecretKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
app.post('/signup', function (req, res) {
request
.post('https://' + mailchimpInstance + '.api.mailchimp.com/3.0/lists/' + listUniqueId + '/members/')
.set('Content-Type', 'application/json;charset=utf-8')
.set('Authorization', 'Basic ' + new Buffer('any:' + mailchimpApiKey ).toString('base64'))
.send({
'email_address': req.body.email,
'status': 'subscribed',
'merge_fields': {
'FNAME': req.body.firstname,
'LNAME': req.body.lastname
}
})
.end(function(err, response) {
if (response.status < 300 || (response.status === 400 && response.body.title === "Member Exists")) {
res.send('Signed Up!');
} else {
res.send('Sign Up Failed :(');
}
});
});
var querystring = require('querystring');
app.get('/mailchimp/auth/authorize', function(req, res) {
res.redirect('https://login.mailchimp.com/oauth2/authorize?' +
querystring.stringify({
'response_type': 'code',
'client_id': mailchimpClientId,
'redirect_uri': 'http://127.0.0.1:3000/mailchimp/auth/callback'
}));
});
var dataStore = require('./dataStore.js');
app.get('/mailchimp/auth/callback', function(req, res) {
request.post('https://login.mailchimp.com/oauth2/token')
.send(querystring.stringify({
'grant_type': 'authorization_code',
'client_id': mailchimpClientId,
'client_secret': mailchimpSecretKey,
'redirect_uri': 'http://127.0.0.1:3000/mailchimp/auth/callback',
'code': req.query.code
}))
.end((err, result) => {
if (err) {
res.send('An unexpected error occured while trying to perform MailChimp oAuth');
} else {
// we need to get the metadata for the user
request.get('https://login.mailchimp.com/oauth2/metadata')
.set('Accept', 'application/json')
.set('Authorization', 'OAuth ' + result.body.access_token)
.end((err, metaResult) => {
if (err) {
res.send('An unexpected error occured while trying to get MailChimp meta oAuth');
} else {
// save the result.body.access_token
// save the metadata in result.body
// against the current user
var mailchimpConf = metaResult.body;
console.log(mailchimpConf);
mailchimpConf.access_token = result.body.access_token;
dataStore.saveMailchimpForUser(mailchimpConf.login.email, mailchimpConf);
res.redirect('/pick-a-list.html?email=' + mailchimpConf.login.email);
}
});
}
});
});
app.get('/mailchimp/lists', function(req, res) {
var mailchimpConf = dataStore.getMailchimpForUser(req.query.email);
request.get(mailchimpConf.api_endpoint + '/3.0/lists')
.set('Accept', 'application/json')
.set('Authorization', 'OAuth ' + mailchimpConf.access_token)
.end((err, result) => {
if (err) {
res.status(500).json(err);
} else {
res.json(result.body.lists);
}
});
});
app.get('/mailchimp/list/members/:id', function(req, res) {
var mailchimpConf = dataStore.getMailchimpForUser(req.query.email);
request.get(mailchimpConf.api_endpoint + '/3.0/lists/' + req.params.id + '/members')
.set('Accept', 'application/json')
.set('Authorization', 'OAuth ' + mailchimpConf.access_token)
.end((err, result) => {
if (err) {
res.status(500).json(err);
} else {
res.json(result.body.members);
}
});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});