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
8 changes: 8 additions & 0 deletions rooms-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dependency directory
node_modules

# Debug log from npm
npm-debug.log

# Environment Variables should NEVER be published
.env
42 changes: 42 additions & 0 deletions rooms-app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// ℹ️ 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 authRoutes = require("./routes/auth");
app.use("/auth", authRoutes);

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

const index = require("./routes/index");
app.use("/", index);
const profileRoutes = require("./routes/profile");
app.use("/", profileRoutes);

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

module.exports = app;
21 changes: 21 additions & 0 deletions rooms-app/config/cloudinary.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const multer = require("multer")

const cloudinary = require("cloudinary").v2;
const {CloudinaryStorage} = require("multer-storage-cloudinary")

cloudinary.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_KEY,
api_secret: process.env.CLOUDINARY_SECRET,

})

const storage = new CloudinaryStorage({
cloudinary,
params:{
folder: "cappuccino",
allowed_formats: ["svg", "png", "jpg", "tif", "webp", "pdf"]
}
})

module.exports = multer({storage});
64 changes: 64 additions & 0 deletions rooms-app/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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({
secret: process.env.SESSION_SECRET || "super hyper secret key",
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: MONGO_URI,
}),
})
);
};
19 changes: 19 additions & 0 deletions rooms-app/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ℹ️ 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 rooms-app/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");
}
});
};
11 changes: 11 additions & 0 deletions rooms-app/middleware/isLoggedIn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = (req, res, next) => {
// checks if the user is logged in when trying to access a specific page
if (!req.session.user) {
if(req.path !== "/") return res.redirect("/auth/login");
else return res.render("index")

//return res.redirect("/auth/login");
}
req.user = req.session.user;
next();
};
8 changes: 8 additions & 0 deletions rooms-app/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.user) {
return res.redirect('/');
}
next();
};
19 changes: 19 additions & 0 deletions rooms-app/models/Room.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { Schema, model } = require("mongoose");


const roomSchema = new Schema({
name: { type: String },
description: { type: String },
imgUrl: {
type: String,
default: "https://www.generationsforpeace.org/wp-content/uploads/2018/03/empty.jpg"
},
owner: { type: Schema.Types.ObjectId, ref: "User" },
reviews: []
});



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

module.exports = Room;
25 changes: 25 additions & 0 deletions rooms-app/models/User.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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(
{
email: String,
password: String,
fullName: String,
imgUrl:{
type: String,
default: "https://icon-library.com/images/anonymous-user-icon/anonymous-user-icon-2.jpg"
},
// slack login - optional
slackID: String,
// google login - optional
googleID: String
},
{
timestamps: true
}
);

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

module.exports = User;
Loading