Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback #1

Open
wants to merge 2 commits into
base: feedback
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PORT=3000
SESSION_SECRET='Cappuccino'

MG_USERNAME=SantiGaray
MG_PWD=pwg8hIRc1xilbwzc

MONGODB_URI=`mongodb+srv://SantiGaray:[email protected]/lab-rooms-app`
39 changes: 39 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
require("dotenv/config");

// ℹ️ Connects to the database
require("./db");

// Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const express = require("express");

// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require("hbs");

const app = express();

// ℹ️ This function is getting exported from the config folder. It runs most pieces of middleware
require("./config")(app);

const projectName = "rooms-app";
const capitalized = (string) => string[0].toUpperCase() + string.slice(1).toLowerCase();

app.locals.title = `${capitalized(projectName)} created with IronLauncher`;

// 👇 Start handling routes here
const index = require("./routes/index");
app.use("/", index);

const roomRoutes = require("./routes/room.routes");
app.use("/room", roomRoutes);

const authRoutes = require("./routes/auth");
app.use("/auth", authRoutes);

// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require("./error-handling")(app);

module.exports = app;
67 changes: 67 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// We reuse this import in order to have access to the `body` property in requests
const express = require("express");

// ℹ️ Responsible for the messages you see in the terminal as requests are coming in
// https://www.npmjs.com/package/morgan
const logger = require("morgan");

// ℹ️ Needed when we deal with cookies (we will when dealing with authentication)
// https://www.npmjs.com/package/cookie-parser
const cookieParser = require("cookie-parser");

// ℹ️ Serves a custom favicon on each request
// https://www.npmjs.com/package/serve-favicon
const favicon = require("serve-favicon");

// ℹ️ global package used to `normalize` paths amongst different operating systems
// https://www.npmjs.com/package/path
const path = require("path");

// ℹ️ Session middleware for authentication
// https://www.npmjs.com/package/express-session
const session = require("express-session");

// ℹ️ MongoStore in order to save the user session in the database
// https://www.npmjs.com/package/connect-mongo
const MongoStore = require("connect-mongo");

// Connects the mongo uri to maintain the same naming structure
const MONGO_URI = require("../utils/consts");

// Middleware configuration
module.exports = (app) => {
// In development environment the app logs
app.use(logger("dev"));

// To have access to `body` property in the request
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

// Normalizes the path to the views folder
app.set("views", path.join(__dirname, "..", "views"));
// Sets the view engine to handlebars
app.set("view engine", "hbs");
// AHandles access to the public folder
app.use(express.static(path.join(__dirname, "..", "public")));

// Handles access to the favicon
app.use(
favicon(path.join(__dirname, "..", "public", "images", "favicon.ico"))
);

// ℹ️ Middleware that adds a "req.session" information and later to check that you are who you say you are 😅
app.use(
session({
cookie: {
maxAge: 24 * 60 * 60 * 1000
},
secret: process.env.SESSION_SECRET || "super hyper secret key",
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: MONGO_URI,
}),
})
);
};
20 changes: 20 additions & 0 deletions db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ℹ️ package responsible to make the connection with mongodb
// https://www.npmjs.com/package/mongoose
const mongoose = require("mongoose");

// ℹ️ Sets the MongoDB URI for our app to have access to it.
// If no env has been set, we dynamically set it to whatever the folder name was upon the creation of the app

const MONGO_URI = require("../utils/consts");


mongoose
.connect(MONGO_URI)
.then((x) => {
console.log(
`Connected to Mongo! Database name: "${x.connections[0].name}"`
);
})
.catch((err) => {
console.error("Error connecting to mongo: ", err);
});
17 changes: 17 additions & 0 deletions error-handling/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = (app) => {
app.use((req, res, next) => {
// this middleware runs whenever requested page is not available
res.status(404).render("not-found");
});

app.use((err, req, res, next) => {
// whenever you call next(err), this middleware will handle the error
// always logs the error
console.error("ERROR: ", req.method, req.path, err);

// only render if the error ocurred before sending the response
if (!res.headersSent) {
res.status(500).res.render("error");
}
});
};
8 changes: 8 additions & 0 deletions middleware/isLoggedIn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = (req, res, next) => {
// checks if the user is logged in when trying to access a specific page
if (!req.session.currentUserId) {
return res.redirect("/auth/login");
}
req.user = req.session.currentUserId;
next();
};
8 changes: 8 additions & 0 deletions middleware/isLoggedOut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = (req, res, next) => {
// if an already logged in user tries to access the login page it
// redirects the user to the home page
if (req.session.currentUserId) {
return res.redirect('/');
}
next();
};
13 changes: 13 additions & 0 deletions models/Room.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { Schema, model } = require("mongoose");

const roomSchema = new Schema({
name: { type: String },
description: { type: String },
imageUrl: { type: String },
owner: { type: Schema.Types.ObjectId, ref: "User" },
reviews: [] // we will update this field a bit later when we create review model
});

const Room = model("Room", roomSchema);

module.exports = Room;
33 changes: 33 additions & 0 deletions models/User.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { Schema, model } = require("mongoose");

// TODO: Please make sure you edit the user model to whatever makes sense in this case
const userSchema = new Schema(
{
fullName: {
type: String
},
password: {
type: String,
unique: true,
},
email: {
type: String,
unique: true,
},
// slack login - optional
slackID: {
type: String
},
// google login - optional
googleID: {
type: String
}
},
{
timestamps: true
}
);

const User = model("User", userSchema);

module.exports = User;
Loading