This repository has been archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (127 loc) · 3.71 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* eslint no-console: 0 */
import path from 'path';
import express from 'express';
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import config from './webpack.config.js';
import jwt from 'express-jwt';
import cors from 'cors';
var bodyParser = require('body-parser')
import request from 'request';
import serverConfig from './server_config.js';
const isDeveloping = process.env.NODE_ENV !== 'production';
console.log('isDeveloping=' + isDeveloping)
const port = isDeveloping ? 3000 : process.env.PORT;
const app = express();
var authenticate = jwt({
secret: new Buffer(serverConfig.auth0ClientSecret, 'base64'),
audience: serverConfig.auth0ClientID
});
if (isDeveloping) {
const compiler = webpack(config);
const middleware = webpackMiddleware(compiler, {
publicPath: config.output.publicPath,
contentBase: 'src',
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false
}
});
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
app.get('/', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
} else { // production
app.use(express.static(__dirname + '/dist'));
app.get('/', function response(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
// other express configuration
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// set up the secured api using the jwt verfivier
app.use('/secured', authenticate);
app.get('/ping', function(req, res) {
res.send(200, {text: "All good. You don't need to be authenticated to call this"});
});
app.get('/secured/ping', function(req, res) {
var url = 'https://' + serverConfig.auth0Domain + '/api/v2/users/' + encodeURIComponent(req.user.sub)
request({
'url': url,
'auth': {
'bearer': serverConfig.auth0ApiToken
}
}, function(err, response, body) {
res.send(200, {text: "All good. You only get this message if you're authenticated"});
})
})
app.get('/secured/getUsers', function(req, res) {
var url = 'https://' + serverConfig.auth0Domain + '/api/v2/users'
request({
'url': url,
'auth': {
'bearer': serverConfig.auth0ApiToken
}
}, function(err, response, body) {
res.json(body)
})
})
app.post('/secured/updateUser', function(req, res) {
var url = 'https://' + serverConfig.auth0Domain + '/api/v2/users/' + req.body.user_id
request({
'url': url,
'method': 'patch',
'auth': {
'bearer': serverConfig.auth0ApiToken
},
'body' : {
'app_metadata': {
'datatools' : req.body.data
}
},
'json' : true
}, function(err, response, body) {
res.send(200);
})
})
app.post('/secured/createUser', function(req, res) {
var user = {
'connection': "Username-Password-Authentication",
'email': req.body.email,
'password': req.body.password,
'app_metadata': {
'datatools' : req.body.permissions
}
}
console.log(user)
var url = 'https://' + serverConfig.auth0Domain + '/api/v2/users'
console.log('create: ' + url)
request({
'url': url,
'method': 'post',
'auth': {
'bearer': serverConfig.auth0ApiToken
},
'body' : user,
'json' : true
}, function(err, response, body) {
res.send(200);
})
})
app.listen(port, '0.0.0.0', function onStart(err) {
if (err) {
console.log(err);
}
console.info('==> 🌎 Listening on port %s. Open up http://0.0.0.0:%s/ in your browser.', port, port);
});