-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathserver.js
36 lines (31 loc) · 1.1 KB
/
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
require('dotenv').config();
const express = require('express');
const request = require('request');
const app = express();
app.get('/api/token', (req, res) => {
if (!req.query.code && !req.query.grant_type) {
return res.status(401).json({
message: 'No authorisation code provided',
});
}
return request.post({
url: 'https://api.monzo.com/oauth2/token',
form: {
grant_type: req.query.grant_type || 'authorization_code',
client_id: process.env.MONZO_CLIENT_ID,
client_secret: process.env.MONZO_CLIENT_SECRET,
redirect_uri: process.env.MONZO_REDIRECT_URI,
[req.query.grant_type === 'refresh_token' ? 'refresh_token' : 'code']:
req.query.grant_type === 'refresh_token' ? req.query.refresh_token : req.query.code,
},
}, (err, response, body) => {
if (!err && response.statusCode === 200) {
res.status(200).json(JSON.parse(body));
} else {
res.status(response.statusCode).json({
message: body,
});
}
});
});
app.listen(process.env.PORT || 8081, () => console.log('[', new Date(), ']', 'MonzoWeb API server running'));