-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (73 loc) · 1.97 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
var express =require("express"),
app =express(),
bodyParser =require("body-parser"),
User =require("./models/user"),
firebase = require("firebase");
// var serviceAccount = require("./glugmvit68-firebase-adminsdk-nl3o0-01de15f20e.json");
var config = {
apiKey: "AIzaSyA5ZAhTKqKAu4g0bNjxUn5Xmz2yPj_Pulc",
authDomain: "glugmvit68.firebaseapp.com",
databaseURL: "https://glugmvit68.firebaseio.com",
projectId: "glugmvit68",
storageBucket: "glugmvit68.appspot.com",
messagingSenderId: "438399420917"
};
firebase.initializeApp(config);
var db = firebase.database();
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname+"/public"));
// app.use(flash());
app.use(function(req,res,next){
res.locals.user=firebase.auth().currentUser;
next();
});
// ROUTES
// HOME
app.get("/",function(req,res){
res.render("home");
});
app.get("/author",function(req,res){
res.render("author");
});
// AUTH ROUTES
// REGISTER FORM
app.get("/register",function(req,res){
res.render("register");
});
app.post("/register",function(req,res){
firebase.auth().createUserWithEmailAndPassword(req.body.email.toString(),req.body.password)
.then(function(userRecord) {
res.redirect("/");
})
.catch(function(err){
console.log(err.code);
console.log(err.message);
})
})
app.get("/login",function(req,res){
res.render("login");
});
app.post("/login",function(req,res){
firebase.auth().signInWithEmailAndPassword(req.body.email.toString(), req.body.password)
.then(function(userRecord){
res.redirect("/");
})
.catch(function(err) {
console.log(err);
// Handle errors
});
})
app.get("/logout",function(req,res){
firebase.auth().signOut()
.then(function(){
res.redirect("/");
})
.catch(function (err) {
res.redirect("/");
// Handle errors
});
})
app.listen(3000,function(){
console.log("SERVER STARTED!!!");
});