-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.js
145 lines (132 loc) · 4.19 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');
const users = require('./routes/api/users');
const User = require('./models/User');
const jwt = require('jsonwebtoken');
const auth = require('./routes/api/auth');
const profile = require('./routes/api/profile');
const messages = require('./routes/api/messages');
const admin = require('./admin/api');
const task_work = require('./routes/api/work');
const search = require('./routes/api/search');
const notifications = require('./routes/api/notifications');
const app = express();
const socket = require('socket.io');
const keys = require('./config/keys');
var path = require('path');
const tasks = require('./routes/api/tasks');
// Bodyparser middleware
app.use(
bodyParser.urlencoded({
extended: false,
})
);
app.use(bodyParser.json());
// DB Config
const db = require('./config/keys').mongoURI;
// Connect to MongoDB
mongoose
.connect(db, { useNewUrlParser: true, useFindAndModify: false })
.then(() => console.log('MongoDB successfully connected'))
.catch((err) => console.log(err));
// Passport middleware
app.use(passport.initialize());
// Passport config
require('./config/passport')(passport);
// Google Login:
require('./config/googlePassport')(passport);
app.get(
'/auth/google',
passport.authenticate('google', { scope: ['openid', 'profile', 'email'] })
);
app.get(
'/auth/google/callback',
passport.authenticate('google', {
failureRedirect: '/login?v=3',
session: false,
}),
function (req, res) {
if (req.user.isRegistered) {
var token = req.user.token;
res.redirect('/login?token=' + token);
} else if (!req.user.isRegistered) {
res.redirect(
`/register?ref=google&t=${req.user.token}&e=${req.user.email}&n=${req.user.name}`
);
}
}
);
// Facebook Login:
require('./config/facebookPassport')(passport);
app.get(
'/auth/facebook',
passport.authenticate('facebook', { scope: 'email' })
);
app.get(
'/auth/facebook/callback',
passport.authenticate('facebook', {
failureRedirect: '/login?v=3',
session: false,
}),
function (req, res) {
if (req.user.isRegistered) {
var token = req.user.token;
res.redirect('/login?token=' + token);
} else if (!req.user.isRegistered) {
res.redirect(
`/register?ref=facebook&t=${req.user.token}&e=${req.user.email}&n=${req.user.name}`
);
}
}
);
// Routes
app.use('/api/users', users);
//get verification response
app.get('/confirmation/:token', async (request, response) => {
try {
const {
user: { id },
} = jwt.verify(request.params.token, keys.jwtSecret);
let nUser = await User.findOneAndUpdate(
{ _id: id },
{ $set: { isEmailVerified: true } },
{ new: true }
);
return response.redirect('/login?v=2');
} catch (e) {
console.log(e);
return response.send('Unable to verify your email err: ' + e.message);
}
});
app.use('/api/tasks', tasks);
app.use('/api/auth', auth);
app.use('/api/profile', profile);
app.use('/api/messages', messages);
app.use('/api/search', search);
app.use('/api/work', task_work);
app.use('/api/notifications', notifications);
app.use('/api/admin', admin);
// Serve static assets if in production
if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('client/build'));
app.get('/*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
// for socket connections:
const Socket_Connections = require('./functions/socket_connections');
var server = require('http').createServer(app);
//var io = require('socket.io')(server);
global.io = require('socket.io')(server);
global.socket_connections = new Socket_Connections();
global.ioCon = io.on('connection', function (socket) {
require('./sockets/user_socket')(socket, io, socket_connections);
require('./sockets/messages_socket')(socket, io, socket_connections);
require('./sockets/notification_socket')(socket, io, socket_connections);
return io;
});
const port = process.env.PORT || 5000; // process.env.port is Heroku's port
server.listen(port, () => console.log(`TaskBarter First Message on ${port}!`));