-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
163 lines (136 loc) · 3.67 KB
/
app.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require('dotenv').config();
const express = require('express');
const app = express();
const axios = require('axios');
const session = require('express-session');
const uuid= require("uuid");
const PORT = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.static('public'));
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}))
// Client Ui endpoints
app.get('/login', (req,res)=>{
return res.render('login')
})
app.get('/addclass',(req,res)=>{
return res.render('addclass');
})
app.get('/listclasses', (req,res)=>{
return res.render('classes')
})
// Api endpoints
app.get('/', async (req,res)=>{
res.json("Bumps can take request now!");
});
app.get('/classes', async (req,res)=>{
const {username, password} = req.session;
if(!username || !password){
return res.json({
"error": true,
"message": "Forbidden!"
})
}
const config = {
auth: {
username: req.session.username,
password: req.session.password
},
headers: {
'Accept-Encoding': 'application/json',
}
}
try {
const response = await axios.get(process.env.API_BASE_URL,config);
res.json(response.data);
}catch(error){
console.log(error.message)
}
})
app.post('/login', async (req,res)=>{
const {username, password} = req.body;
const config = {
auth: {
username: username,
password: password
},
headers: {
'Accept-Encoding': 'application/json',
}
}
try {
const response = await axios.get(process.env.API_BASE_URL,config);
req.session.username = username
req.session.password = password
return res.json({
"error": false,
"message": "Authenticated",
})
}catch(error){
if(typeof error.response != 'undefined' && error.response.status == 401){
return res.json({
"error": true,
"message": "Check your email or pasword",
})
}else{
console.log(error.message);
}
}
})
app.post('/addclass',async (req,res) => {
const { description,start, end, max_participants, record_class }= req.body;
const {username, password} = req.session;
if(!username || !password){
return res.json({
"error": true,
"message": "Forbidden!"
})
}
const config = {
auth: {
username: req.session.username,
password: req.session.password
},
headers: {
'Accept-Encoding': 'application/json',
}
}
const data = {
"description": description,
"start": start,
"end": end,
"max_participants": max_participants,
"record_class": record_class,
"room_token": uuid.v4()
}
try {
const response = await axios.post(process.env.API_BASE_URL,data,config);
if(response.data){
return res.json({
"error": false,
"message": "Class is successfully created"
})
}
}catch(error){
console.log(error.message)
}
});
app.get('/logout', async (req,res)=>{
try{
const success = await req.session.destroy()
return res.json({
"error": false,
"message": "Logout successful"
})
}catch(error){
console.log(error.message)
}
})
app.listen(PORT,()=>{
console.log(`Server started running at port ${PORT}`);
})