1
+ if ( process . env . NODE_ENV !== "production" ) {
2
+ require ( 'dotenv' ) . config ( ) ;
3
+ }
4
+
1
5
const express = require ( 'express' ) ;
2
6
const path = require ( 'path' ) ;
3
7
const mongoose = require ( 'mongoose' ) ;
4
8
const ejsMate = require ( 'ejs-mate' ) ;
5
- const Joi = require ( 'joi' )
6
- const { campgroundSchema, reviewSchema } = require ( './ErrorSchemas' )
9
+
10
+ const session = require ( 'express-session' ) ;
11
+ const flash = require ( 'connect-flash' ) ;
12
+ const passport = require ( 'passport' ) ;
13
+ const LocalStrategy = require ( 'passport-local' ) ;
14
+ const User = require ( './models/user' ) ;
15
+
7
16
const ExpressError = require ( './utils/ExpressError' )
8
- const CatchAsync = require ( './utils/CatchAsync' )
9
- const Campground = require ( './models/campground' )
10
- const Review = require ( './models/review' )
11
17
const methodOverride = require ( 'method-override' ) ;
12
- const review = require ( './models/review' ) ;
13
18
14
- mongoose . connect ( 'mongodb://localhost:27017/yelp-camp' , {
19
+ const userRoutes = require ( './routes/users' )
20
+ const campgroundRoutes = require ( './routes/campgrounds' )
21
+ const reviewRoutes = require ( './routes/reviews' )
22
+ const mongoSanitize = require ( 'express-mongo-sanitize' ) ;
23
+ const helmet = require ( "helmet" ) ;
24
+
25
+ // setting Mongo Atlas
26
+ const dbUrl = process . env . DB_URL || 'mongodb://localhost:27017/yelp-camp' ;
27
+ const MongoStore = require ( 'connect-mongo' ) ;
28
+
29
+ mongoose . connect ( dbUrl , {
15
30
useNewUrlParser : true ,
16
31
// useCreateIndex: true,
17
32
useUnifiedTopology : true
@@ -28,98 +43,80 @@ const app = express();
28
43
app . engine ( 'ejs' , ejsMate ) ;
29
44
app . set ( 'view engine' , 'ejs' ) ;
30
45
app . set ( 'views' , path . join ( __dirname , 'views' ) )
46
+
31
47
app . use ( express . urlencoded ( { extended : true } ) )
32
48
app . use ( methodOverride ( '_method' ) )
49
+ app . use ( express . static ( path . join ( __dirname , 'public' ) ) )
50
+
51
+ app . use (
52
+ mongoSanitize ( {
53
+ replaceWith : '_' ,
54
+ } ) ,
55
+ ) ;
56
+
57
+ app . use (
58
+ helmet ( {
59
+ contentSecurityPolicy : false ,
60
+ } )
61
+ ) ;
62
+
63
+ const secret = process . env . SECRET || 'thisshouldbeabettersecret!'
64
+ const store = MongoStore . create ( {
65
+ mongoUrl : dbUrl ,
66
+ secret,
67
+ touchAfter : 24 * 60 * 60 ,
68
+ // crypto: {
69
+ // secret: 'thisshouldbeabettersecret!',
70
+ // }
71
+ } ) ;
33
72
34
- const validateCampground = ( req , res , next ) => {
35
- const { error } = campgroundSchema . validate ( req . body )
36
- if ( error ) {
37
- const msg = error . details . map ( el => el . message ) . join ( ',' )
38
- throw new ExpressError ( msg , 400 )
39
- } else {
40
- next ( ) ;
73
+ store . on ( "error" , function ( e ) {
74
+ console . log ( "SESSION STORE ERROR" , e )
75
+ } )
76
+ const sessionConfig = {
77
+ store,
78
+ name : 'session' ,
79
+ secret,
80
+ resave : false ,
81
+ saveUninitialized : true ,
82
+ cookie : {
83
+ httpOnly : true ,
84
+ // secure: true
85
+ expires : Date . now ( ) + 1000 * 60 * 60 * 24 * 7 ,
86
+ maxAge : 1000 * 60 * 60 * 24 * 7
41
87
}
42
88
}
89
+ app . use ( session ( sessionConfig ) )
90
+ app . use ( flash ( ) ) ;
91
+
92
+ // middleware for passport
93
+ app . use ( passport . initialize ( ) ) ;
94
+ app . use ( passport . session ( ) ) ;
95
+ // use static authenticate method of model in LocalStrategy
96
+ passport . use ( new LocalStrategy ( User . authenticate ( ) ) ) ;
97
+ // use static serialize and deserialize of model for passport session support
98
+ passport . serializeUser ( User . serializeUser ( ) ) ;
99
+ passport . deserializeUser ( User . deserializeUser ( ) ) ;
100
+
101
+ // Flash middleware
102
+ app . use ( ( req , res , next ) => {
103
+ // console.log(req.session);
104
+ // checking for the current status of user whether user is already logged in or not
105
+ res . locals . currentUser = req . user ;
106
+ res . locals . success = req . flash ( 'success' ) ;
107
+ res . locals . error = req . flash ( 'error' ) ;
108
+ next ( ) ;
109
+ } )
43
110
44
- const validateReview = ( req , res , next ) => {
45
- const { error } = reviewSchema . validate ( req . body )
46
- if ( error ) {
47
- const msg = error . details . map ( el => el . message ) . join ( ',' )
48
- throw new ExpressError ( msg , 400 )
49
- } else {
50
- next ( ) ;
51
- }
52
- }
111
+ // Defining Routes
112
+ app . use ( '/' , userRoutes )
113
+ app . use ( '/campgrounds' , campgroundRoutes )
114
+ app . use ( '/campgrounds/:id/reviews' , reviewRoutes )
53
115
54
116
app . get ( '/' , ( req , res ) => {
55
117
res . render ( 'home' )
56
118
} )
57
119
58
- app . get ( '/campgrounds' , CatchAsync ( async ( req , res ) => {
59
- const campgrounds = await Campground . find ( { } ) ;
60
- res . render ( 'campgrounds/index' , { campgrounds } )
61
- } ) )
62
-
63
- // Creating New Campground
64
- app . get ( '/campgrounds/new' , ( req , res ) => {
65
- res . render ( 'campgrounds/new' )
66
- } )
67
-
68
- app . post ( '/campgrounds' , validateCampground , CatchAsync ( async ( req , res , next ) => {
69
- // if (!req.body.campground) throw new ExpressError('Invalid campground data', 400);
70
- const campground = new Campground ( req . body . campground )
71
- await campground . save ( ) ;
72
- res . redirect ( `/campgrounds/${ campground . _id } ` )
73
- } ) )
74
-
75
- // To show all campgrounds
76
- app . get ( '/campgrounds/:id' , CatchAsync ( async ( req , res ) => {
77
- const campground = await Campground . findById ( req . params . id ) . populate ( 'reviews' ) ;
78
- // We used .populate method so that review of that particular campground(because of id) can be shown
79
- res . render ( 'campgrounds/show' , { campground } )
80
- } ) )
81
-
82
- // Update and edit
83
- app . get ( '/campgrounds/:id/edit' , CatchAsync ( async ( req , res ) => {
84
- const campground = await Campground . findById ( req . params . id ) ;
85
- res . render ( 'campgrounds/edit' , { campground } )
86
- } ) )
87
-
88
- app . put ( '/campgrounds/:id' , validateCampground , CatchAsync ( async ( req , res ) => {
89
- const { id } = req . params ;
90
- const campground = await Campground . findByIdAndUpdate ( id , req . body . campground ) ;
91
- res . redirect ( `/campgrounds/${ campground . _id } ` )
92
- } ) )
93
-
94
- // Delete
95
- app . delete ( '/campgrounds/:id' , CatchAsync ( async ( req , res ) => {
96
- const { id } = req . params ;
97
- await Campground . findByIdAndDelete ( id )
98
- res . redirect ( '/campgrounds' ) ;
99
- } ) )
100
-
101
- // Review Routes
102
- app . post ( '/campgrounds/:id/reviews' , validateReview , CatchAsync ( async ( req , res ) => {
103
- const campground = await Campground . findById ( req . params . id ) ;
104
- // To instantiate new Review model we need to import the module
105
- const review = new Review ( req . body . review ) ;
106
- // Here req.body.review is what we gave in show.ejs>form>review[rating] and review[body]
107
- // Now push the new review into campground.reviews array
108
- campground . reviews . push ( review ) ;
109
- campground . save ( ) ;
110
- review . save ( ) ;
111
- res . redirect ( `/campgrounds/${ campground . _id } ` ) ;
112
- } ) )
113
-
114
- // delete reviews
115
- app . delete ( '/campgrounds/:id/reviews/:reviewID' , CatchAsync ( async ( req , res ) => {
116
- const { id, reviewID } = req . params ;
117
- // so the problem is our reviewID is assosciated to campgroundId so if we delete using reviewID the whole campground gets deleted. [13213,123123,141324] suupose this is an array of object ID's adn we wamt to delete the specific ID that belogs to our review id so we will use an poerator in mongo called $pull operator
118
- await Campground . findByIdAndUpdate ( id , { $pull : { reviews : reviewID } } ) ;
119
- await Review . findByIdAndDelete ( reviewID ) ;
120
- res . redirect ( `/campgrounds/${ id } ` ) ;
121
- } ) )
122
-
123
120
// handling errors
124
121
125
122
app . all ( '*' , ( req , res , next ) => {
0 commit comments