-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (58 loc) · 2.32 KB
/
server.js
File metadata and controls
68 lines (58 loc) · 2.32 KB
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
const http = require('http');
const express = require('express');
const dotenv = require('dotenv').config();
const passport = require('passport');
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const bodyParser = require('body-parser');
const mongoose = require('./mongoose');
const url = require('url');
const path = require('path');
const cors = require('cors');
const {
checkAuthInfos,
checkToken,
checkEmail,
checkPassword,
checkOauth2IdTokenInAuthorizationHeader,
checkErrors
} = require('./middlewares/validator');
const {
register, validateAccount, login,
getResetToken, checkResetToken, resetPassword
} = require('./controllers/local');
const {handleGoogleLogin} = require('./controllers/google');
const {logout} = require('./controllers/common');
const app = express();
const server = http.createServer(app);
const router = express.Router();
const production = process.env.NODE_ENV === 'production';
const PORT = process.env.PORT || 3000;
const corsOptions = {
origin: (origin, cb) => {
if (!origin) {
cb(null, false);
return;
}
const u = url.parse(origin);
cb(null, u.hostname == 'localhost' || u.hostname == '127.0.0.1' || u.hostname == 'www.streamwave.be' || u.hostname == 'streamwave.be' || u.hostname == 'staging.streamwave.be');
},
allowedHeaders: ['Content-Type', 'Authorization']
};
// middlewares
router.use(cors(corsOptions));
router.use(bodyParser.json());
router.use(passport.initialize());
router.get('/health', (req, res) => res.send('authentication api is up !\n'));
router.post('/local/register', checkAuthInfos, checkErrors, register);
router.get('/local/account/validate', checkToken, checkErrors, validateAccount);
router.post('/local/login', checkAuthInfos, checkErrors, login);
router.post('/local/account/reset/get-reset-token', checkEmail, checkErrors, getResetToken);
router.get('/local/account/reset/check-reset-token', checkToken, checkErrors, checkResetToken);
router.post('/local/account/reset/change-password', checkToken, checkPassword, checkErrors, resetPassword);
router.post('/google/login', checkOauth2IdTokenInAuthorizationHeader, checkErrors, handleGoogleLogin);
router.delete('/logout', logout);
app.use(router);
server.listen(PORT, () => {
console.log(`Node server listening on https://localhost:${PORT}`);
});