This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 90e3149
Showing
2,494 changed files
with
318,440 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
var express = require("express"), | ||
app = express(), | ||
bodyParser = require("body-parser"), | ||
mongoose = require("mongoose"), | ||
passport = require("passport"), | ||
passportLocal = require("passport-local"), | ||
methodOverride = require("method-override"), | ||
flash = require("connect-flash"), | ||
Pub = require("./models/pub"), | ||
Comment = require("./models/comment"), | ||
User = require("./models/user"), | ||
seedDB = require("./seeds"); | ||
|
||
|
||
|
||
var commentRoutes = require("./routes/comments"), | ||
pubRoutes = require("./routes/pubs"), | ||
indexRoutes = require("./routes/index"); | ||
|
||
var url = process.env.DBURL || "mongodb://localhost/go_pub"; | ||
mongoose.connect(url, {useMongoClient: true}); | ||
|
||
|
||
mongoose.Promise = global.Promise; | ||
app.use(express.static("public")); | ||
app.use(methodOverride("_method")); | ||
app.use(flash()); | ||
app.use(bodyParser.urlencoded({extended:true})); | ||
app.set("view engine", "ejs"); | ||
|
||
seedDB(); | ||
app.locals.moment = require("moment"); | ||
//PASPORT CONFIGURATION | ||
app.use(require("express-session")({ | ||
secret: "thisismysecret", | ||
resave: false, | ||
saveUninitialized: false | ||
})); | ||
|
||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
passport.use(new passportLocal(User.authenticate())); | ||
passport.serializeUser(User.serializeUser()); | ||
passport.deserializeUser(User.deserializeUser()); | ||
|
||
app.use(function(req, res, next){ | ||
res.locals.user = req.user; | ||
res.locals.error = req.flash("error"); | ||
res.locals.success = req.flash("success"); | ||
next(); | ||
}); | ||
|
||
app.use("/", indexRoutes); | ||
app.use("/pubs", pubRoutes); | ||
app.use("/pubs/:id/comments", commentRoutes); | ||
|
||
app.listen(process.env.PORT, process.env.IP, function(){ | ||
console.log("GoPub Server started!"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
var Pub = require("../models/pub"); | ||
var Comment = require("../models/comment"); | ||
var middlewareObj = {}; | ||
|
||
middlewareObj.isLoggedIn = function(req, res, next){ | ||
if(req.isAuthenticated()){ | ||
return next(); | ||
} | ||
req.flash("error", "Please Login First!"); | ||
res.redirect("/login"); | ||
} | ||
|
||
middlewareObj.isAdmin = function(req, res, next) { | ||
if(req.isAuthenticated()){ | ||
Pub.findById(req.params.id, function(err, foundPub){ | ||
if(err){ | ||
req.flash("error", "Pub was not found"); | ||
res.redirect("back"); | ||
} else { | ||
if(req.user.role==="admin") { | ||
next(); | ||
} else { | ||
req.flash("error", "You don't have permisson to do that"); | ||
res.redirect("back"); | ||
} | ||
} | ||
}); | ||
} else { | ||
req.flash("error", "Please Login First!"); | ||
res.redirect("back"); | ||
} | ||
} | ||
|
||
middlewareObj.isOwnerOrAdmin = function(req, res, next) { | ||
if(req.isAuthenticated()){ | ||
Comment.findById(req.params.comment_id, function(err, foundComment){ | ||
if(err){ | ||
req.flash("error", "Comment was not found"); | ||
res.redirect("back"); | ||
} else { | ||
if(foundComment.author.id.equals(req.user._id) || req.user.role==="admin") { | ||
next(); | ||
} else { | ||
req.flash("error", "You don't have permisson to do that"); | ||
res.redirect("back"); | ||
} | ||
} | ||
}); | ||
} else { | ||
req.flash("error", "Please Login First!"); | ||
res.redirect("back"); | ||
} | ||
} | ||
|
||
module.exports = middlewareObj; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var mongoose = require("mongoose"); | ||
|
||
var commentSchema = new mongoose.Schema({ | ||
text: String, | ||
createdAt: { type: Date, default: Date.now }, | ||
author: { | ||
id:{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User" | ||
}, | ||
username: String | ||
} | ||
}); | ||
|
||
|
||
|
||
module.exports = mongoose.model("Comment", commentSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var mongoose = require("mongoose"); | ||
var pubSchema = new mongoose.Schema({ | ||
name: String, | ||
image: String, | ||
description: String, | ||
createdAt: { type: Date, default: Date.now }, | ||
author: { | ||
id:{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User" | ||
}, | ||
username: String | ||
}, | ||
comments: | ||
[ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Comment" | ||
} | ||
] | ||
}); | ||
|
||
|
||
|
||
module.exports = mongoose.model("Pub", pubSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var mongoose = require("mongoose"); | ||
var passportLocalMongoose = require("passport-local-mongoose"); | ||
|
||
var userSchema = new mongoose.Schema({ | ||
username: String, | ||
password: String, | ||
email: String, | ||
role: {type: String, default: "user"} | ||
}); | ||
|
||
userSchema.plugin(passportLocalMongoose); | ||
module.exports = mongoose.model("User", userSchema); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.