forked from ReenaGo/JamSession
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
243 lines (208 loc) · 7.51 KB
/
Copy pathindex.js
File metadata and controls
243 lines (208 loc) · 7.51 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const path = require("path");
const express = require("express");
const session = require("express-session");
const twilio = require("twilio");
const bodyParser = require("body-parser");
const bcrypt = require("bcrypt");
const SequelizeStore = require("connect-session-sequelize")(session.Store);
const db = require("./models");
const randomName = require("./public/randomname");
const myStore = new SequelizeStore({ db: db.sequelize });
const AccessToken = twilio.jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
const ChatGrant = AccessToken.ChatGrant;
var http = require("http");
//var config = require('./config');
var IpMessagingGrant = AccessToken.IpMessagingGrant;
//var twiliAccntInfoFromFile=config.getTwiliAccountSettingsfromFile ;
// Only load local environment (.env) file if not hosted on Heroku, etc.
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
// Load configuration information from system environment variables.
const TWILIO_ACCOUNT_SID = process.env.TWILIO_ACCOUNT_SID;
const TWILIO_AUTH_TOKEN = process.env.TWILIO_AUTH_TOKEN;
const TWILIO_CHAT_SERVICE_SID = process.env.TWILIO_CHAT_SERVICE_SID;
const TWILIO_API_KEY = process.env.TWILIO_API_KEY;
const TWILIO_API_SECRET = process.env.TWILIO_API_SECRET;
const PORT = process.env.PORT || 3000;
// Create an authenticated client to access the Twilio REST API
const client = twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
const app = express();
app.use(
session({
secret: "mySecret",
resave: false,
saveUninitialized: true,
store: myStore
})
);
myStore.sync();
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "assets")));
// set up ejs
app.set("view engine", "ejs");
app.set("views", "app/views");
// Redirect to Signup page when visiting main URL
app.get("/", function(req, res, next) {
res.redirect("/signup");
});
//***********************************************************************************************/
//Signup functionality
app.get("/signup", (req, res, next) => {
res.render("signup", {});
});
app.post("/signup", (req, res, next) => {
// signup *****
let firstName = req.body.firstName;
let lastName = req.body.lastName;
let email = req.body.email;
let password = req.body.password;
let bio = req.body.Bio1;
let imageURL = req.body.imageURL;
bcrypt.hash(password, 10, (err, hash) => {
db.user
.create({ firstName: firstName, lastName: lastName, email: email, Image: imageURL, Bio: bio, password: hash })
.then(user => {
req.session.user_id = user.id;
req.session.firstName = user.firstName;
res.redirect("/profilePage");
});
});
});
// ***************************************************************************************************
// login Functionality
app.post("/login", (req, res, next) => {
var emailforlogin = req.body.emailforlogin;
var passwordforlogin = req.body.passwordforlogin;
db.user.findOne({ where: { email: emailforlogin } }).then(function(user) {
if (user === null) {
res.render("signup", { error_message: "User Not Found" });
} else {
bcrypt.compare(passwordforlogin, user.password, function(err, matched) {
if (matched) {
// set user_id in the session
req.session.user_id = user.id;
req.session.firstName = user.firstName;
// redirect to welcome page
res.redirect("/profilePage");
} else {
// render the login form
res.render("signup", { error_message: "Bad Password" });
}
});
}
});
});
// ************************************************************************************************
// Profile Page Functionality
app.get("/profilePage", (req, res, next) => {
// res.render('profilePage')
db.user.findByPk(req.session.user_id).then(function(user) {
db.communities.findAll().then(function(communities) {
res.render("profilePage", {
firstName: user.firstName,
lastName: user.lastName,
imageURL: user.Image,
bio: user.Bio,
communities: communities
});
});
//console.log(communities)
});
});
// *************************************************************************************************
// community Page functionality
app.get("/community/:id", (req, res, next) => {
let communityId = req.params.id;
let userId = req.session.user_id;
let firstName = req.session.firstName;
req.session.community_id = communityId;
db.communities.findOne({ where: { id: communityId } }).then(function(communities) {
db.jam.findAll().then(function(jams) {
res.render("community", {
comName: communities.comName,
description: communities.Description,
jams: jams,
userId: userId,
firstName: firstName
});
});
});
});
//**************************************************************************************************/
// community Page create Jams
app.post("/community/:id", (req, res, next) => {
let communityId = req.params.id;
req.session.community_id = communityId;
jamName = req.body.jamName;
jamDescription = req.body.jamDescription;
date = req.body.date;
time = req.body.time;
db.jam.create({ jamName: jamName, jamDescription: jamDescription, date: date, time: time }).then(jam => {
res.redirect(`/community/${communityId}`);
});
});
//**********************************************************************************************/
//logout
app.get("/signOut", function(req, res, next) {
req.session.destroy(function() {
res.redirect("/signup");
});
});
//**********************************************************************************************/
//video route
app.get("/video/:jamName", function(req, res, next) {
let firstName = req.session.firstName;
let jamSession = req.params.jamName;
res.render("video", {
jamSession: jamSession,
firstName: firstName
});
});
// endpoint to procure Twilio Video Token
app.get("/videoToken", (req, res) => {
const identity = req.query.identity || randomName();
// Create an access token which we will sign and return to the client,
// containing the grant we just created.
const token = new AccessToken(TWILIO_ACCOUNT_SID, TWILIO_API_KEY, TWILIO_API_SECRET);
// Assign the generated identity to the token (passed in from client)
token.identity = identity;
// Grant the access token Twilio Video capabilities.
const grant = new VideoGrant();
token.addGrant(grant);
// Serialize the token to a JWT string and include it in a JSON response.
res.send({
identity: identity,
token: token.toJwt()
});
});
// endpoint to procure Twilio Chat Token
/*
Generate an Access Token for a chat application user - it generates a random
username for the client requesting a token, and takes a device ID as a query
parameter.
*/
//let credentials = require("./credentials.json");
app.get("/chatToken", function(req, res) {
let username = req.query.username;
console.log("username is: ", username);
let token = new AccessToken(TWILIO_ACCOUNT_SID, TWILIO_API_KEY, TWILIO_API_SECRET, {
identity: username,
ttl: 40000
});
let grant = new ChatGrant({ serviceSid: TWILIO_CHAT_SERVICE_SID });
token.addGrant(grant);
const tokenJwt = token.toJwt();
console.log("token: " + tokenJwt);
res.send(tokenJwt);
});
app.get("/chat", function(req, res) {
res.sendFile("/public/chat.html", { root: __dirname });
});
app.listen(PORT, () => {
console.log(`Express listening on port ${PORT}`);
});