-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
136 lines (109 loc) · 3.3 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
const express =require('express')
const passport=require('passport')
const cors = require('cors')
const app= express()
const server= require('http').Server(app)
const bodyParser = require('body-parser')
const cookieSession = require('cookie-session')
require('./passport-setup')
const io=require('socket.io')(server)
const { v4: uuidV4 } = require('uuid') //to generate dynamic url
var nodemailer = require('nodemailer');
const { content } = require('googleapis/build/src/apis/content')
const { profile } = require('console')
require('dotenv').config();
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
});
app.set('view engine', 'ejs') // to render our front end pages,Files from views directory will be rendered
app.use(express.static('public')) //we will keep all static front-end files like javascript and css here in this folder. this function makes them accessible
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cookieSession({
name: 'gmeet',
keys: ['key1', 'key2']
}))
const isLoggedIn = (req, res, next) => {
if (req.user) {
next();
} else {
res.sendStatus(401);
}
}
app.use(passport.initialize());
app.use(passport.session());
username = "";
app.get('/',(req,res)=>{
res.render('home')
})
app.get('/meeting',(req,res)=>{
res.redirect(`/${uuidV4()}`)
}) //This tells server that on getting request through path '/' do the function written inside
app.get('/failed', (req,res)=>{
res.send('Login Failed')
})
app.get('/good',isLoggedIn, (req,res)=>{
res.redirect('/meeting')
})
app.get('/logout',(req,res)=>{
req.session=null;
req.logout();
res.render('logout')
})
app.get('/google',
passport.authenticate('google', { scope: ['profile','email'] }));
app.get('/google/callback',
passport.authenticate('google', { failureRedirect: '/failed' }),
function(req, res) {
res.redirect('/good');
});
app.get('/:room',(req,res)=>{
username = req.user.name.givenName;
console.log(req.user)
res.render('room',{roomId: req.params.room})
})
io.on('connection', socket=>{
socket.on('join-room',(roomId,userId)=>{
socket.join(roomId)
socket.to(roomId).emit('user-connected',userId)
socket.on('message', (message) => {
const formattedMessage = username + ' : ' +message;
console.log(username);
io.to(roomId).emit('createMessage', formattedMessage);
});
socket.on('newStroke', (coordinates) => {
socket.to(roomId).emit('newStroke', coordinates)
});
socket.on('stroke', coordinates => {
io.to(roomId).emit('draw', coordinates)
});
socket.on('mail',content=>{
sendnow(content);
})
socket.on('disconnect',()=>{
socket.to(roomId).emit('user-disconnected',userId)
})
})
})
server.listen('3000')
function sendnow(content)
{
var mailOptions = {
from: '[email protected]',
to: content.address,
subject: 'Meeting Invitiation',
text: content.txt
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}