-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
138 lines (119 loc) · 3.7 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
const express = require('express');
const firebase = require('firebase');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const app = express();
var admin = require("firebase-admin");
require("firebase/auth");
require("firebase/firestore");
var serviceAccount = require("./key.json");
const { auth, firestore } = require('firebase');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
var flag =0;
var firebaseConfig = {
apiKey: "AIzaSyDqq4CrAW3Qh4LNMLt3MWgj9gOhZxsnjzQ",
authDomain: "docmaster-69.firebaseapp.com",
projectId: "docmaster-69",
storageBucket: "docmaster-69.appspot.com",
messagingSenderId: "113213208283",
appId: "1:113213208283:web:e1fb855d6dd36851c4befe",
measurementId: "G-Y7QBHWQ944"
};
// Initialize Firebase
const storageAdd = "docmaster-69.appspot.com";
firebase.initializeApp(firebaseConfig);
const db = admin.firestore();
app.set('view engine','ejs');
app.use(bodyParser.urlencoded({
extended:true
}));
app.use(express.static("public"));
app.route("/")
.get(function (req,res) {
res.render('index')
});
app.route('/signin')
.get(function (req,res) {
res.render('signin');
});
app.post('/signin',(req,res)=>{
var email,password;
email = req.body.loginemail;
password = req.body.loginpassword;
firebase.auth().signInWithEmailAndPassword(email, password)
.then((userCredential) => {
// Signed in
var user = userCredential.user;
// ...
res.redirect('/StudentDashboard/id='+userCredential.user.uid);
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
res.send('<center><h2>'+errorMessage+'</h2></center>');
});
});
app.route('/signup')
.get(function (req,res) {
res.render('signup')
});
app.post('/signup',(req,res)=>{
var arr=[],status,email,password;
email = req.body.email.toString();
password = req.body.password.toString();
firebase.auth().createUserWithEmailAndPassword(email, password)
.then((userCredential) => {
// Signed in
var user = userCredential.user;
const collec = db.collection("Student").doc(userCredential.user.uid);
collec.set({
Name : req.body.name,
Email : req.body.email.toString(),
uid : userCredential.user.uid,
Status : req.body.status.toString(),
Filenames : arr
})
res.redirect('/StudentDashboard/id='+userCredential.user.uid);
// ...
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
console.log("ERROR IS "+ errorMessage);
res.send('<center><h2>'+errorMessage+'</h2></center>');
// ..
});
});
app.get('/StudentDashboard/id=:id',async (req,res)=>{
var doclen=[],docnum,docMap;
var Id = req.params.id;
var ref = db.collection('Student').doc(Id);
const snapshot = await ref.get();
var name = snapshot.data().Name;
doclen = snapshot.data().Filenames;
docnum = doclen.length;
res.render('StudentDashboard',{Id:Id,Name:name, docMap:docMap, doclen:doclen, docnum:docnum });
})
app.post('/StudentDashboard/id=:id', async(req,res)=>{
var id = req.params.id;
var filenamearr=[];
var filename = req.body.filename;
var file = req.body.bblogo;
var ref = db.collection('Student').doc(id);
const snapshot = await ref.get();
var update =ref.update({
Filenames : admin.firestore.FieldValue.arrayUnion(filename)
});
filenamearr = snapshot.data().Filenames;
res.redirect('/StudentDashboard/id='+id);
flag=1;
})
app.route('dashboard')
.get(function (req,res){
res.render('dashboard');
});
app.listen(process.env.PORT||3000,function(){
console.log("Server started on port 3000");
});