This repository was archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
89 lines (74 loc) · 2.38 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
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
const express = require('express')
var request = require("request");
const bodyParser = require('body-parser')
const webpack = require('webpack')
const config = require('./build/webpack.dev.conf')
const app = express()
const compiler = webpack(config)
const jsonParser = bodyParser.json()
// google client Id and clien secret
const clientId = 'Write google client id here'
const clientSecret = 'Write google client secret here'
var http = require('http').Server(app);
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// handle fallback for HTML5 history API
app.use(require('connect-history-api-fallback')())
// serve webpack bundle output
app.use(require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath,
stats: {
colors: true,
chunks: false
}
}))
// enable hot-reload and state-preserving
// compilation error display
app.use(require('webpack-hot-middleware')(compiler))
app.post('/getAccessToken', jsonParser, (req, res) => {
console.log('req.body: ', req.body)
var options = { method: 'POST',
url: 'https://accounts.google.com/o/oauth2/token',
headers:
{
'cache-control': 'no-cache',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' },
formData:
{ code: req.body.code,
client_id: clientId,
client_secret: clientSecret,
redirect_uri: 'http://localhost:8080/oauthCallback',
grant_type: 'authorization_code' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
// console.log(body);
res.send(body)
});
})
app.post('/getTokenFromRefreshToken', jsonParser, (req, res) => {
var options = { method: 'POST',
url: 'https://www.googleapis.com/oauth2/v4/token',
headers:
{ 'content-type': 'application/x-www-form-urlencoded',
'cache-control': 'no-cache' },
form:
{ client_id: clientId,
client_secret: clientSecret,
refresh_token: req.body.refresh_token,
grant_type: 'refresh_token' } };
request(options, function (error, response, body) {
console.log('err:', error)
if (error) throw new Error(error);
res.send(body)
});
})
//172.16.230.127
app.listen(8080, 'localhost', function (err) {
if (err) {
console.log(err)
return
}
console.log('Listening at http://localhost:8080')
})