forked from nash-md/twilio-verify-example
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
61 lines (44 loc) · 1.99 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
require('dotenv').config();
const context = require('./context');
const http = require('http');
const express = require('express');
const session = require('express-session');
const { registerWebsocketServer } = require('./websocket-server');
const register = require('./controllers/register');
const login = require('./controllers/login');
const devices = require('./controllers/devices');
const challenges = require('./controllers/challenges');
const pages = require('./controllers/pages');
const validateSessionWithMultifactor = require('./middlewares/validate-session-with-multifactor');
const validateSession = require('./middlewares/validate-session');
const ChallengeManager = require('./challenge-manager');
const DeviceManager = require('./device-manager');
const UserRepository = require('./user-repository');
context.repository = new UserRepository();
context.devices = new DeviceManager();
context.challenges = new ChallengeManager();
const app = new express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session(context.configuration));
// controllers
app.post('/api/login', login);
app.post('/api/register', register);
// middleware to validate session
app.post('/api/devices/token', validateSession, devices.token);
app.post('/api/devices/register', validateSession, devices.register);
app.post('/api/challenges/update-webhook', challenges.update);
app.get('/api/challenges/status', validateSession, challenges.status);
app.get('/push-challenge-pending', validateSession, pages.pending);
app.get('/profile', validateSessionWithMultifactor, pages.profile);
app.get('/logout', validateSessionWithMultifactor, pages.logout);
app.get('/reject', pages.reject);
app.get('/', pages.login);
app.get('/register', pages.register);
app.use(express.static('public'));
const port = process.env.PORT || '5000';
const server = http.createServer(app);
registerWebsocketServer(server);
server.listen(port, () => {
console.log(`Listening to requests on http://localhost:${port}`);
});