-
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
Showing
31 changed files
with
9,537 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,61 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Typescript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# next.js build output | ||
.next |
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,65 @@ | ||
var createError = require("http-errors"); | ||
var express = require("express"); | ||
var path = require("path"); | ||
var cookieParser = require("cookie-parser"); | ||
var logger = require("morgan"); | ||
const { redisClient, RedisStore, session } = require("./database/redis"); | ||
// require("./database/mongo"); | ||
var passport = require("passport"); | ||
|
||
var indexRouter = require("./routes/index"); | ||
var usersRouter = require("./routes/users"); | ||
var passportRouter = require("./routes/passport"); | ||
var streamRouter = require("./routes/stream"); | ||
|
||
var app = express(); | ||
|
||
// view engine setup | ||
app.set("views", path.join(__dirname, "views")); | ||
app.set("view engine", "jade"); | ||
|
||
app.use(logger("dev")); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, "public"))); | ||
app.use("/stream", require("./routes/stream")); | ||
|
||
app.use( | ||
session({ | ||
store: new RedisStore({ client: redisClient }), | ||
secret: "secret$1", | ||
resave: false, | ||
saveUninitialized: false, | ||
cookie: { | ||
secure: false, | ||
httpOnly: false, | ||
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days | ||
}, | ||
}) | ||
); | ||
|
||
app.use(passport.initialize()); | ||
require("./middlewares/passport")(passport); | ||
app.use("/", indexRouter); | ||
app.use("/users", usersRouter); | ||
app.use("/passport", passportRouter); | ||
app.use("/products", require("./routes/products")); | ||
|
||
// catch 404 and forward to error handler | ||
app.use(function (req, res, next) { | ||
next(createError(404)); | ||
}); | ||
|
||
// error handler | ||
app.use(function (err, req, res, next) { | ||
// set locals, only providing error in development | ||
res.locals.message = err.message; | ||
res.locals.error = req.app.get("env") === "development" ? err : {}; | ||
|
||
// render the error page | ||
res.status(err.status || 500); | ||
res.render("error"); | ||
}); | ||
|
||
module.exports = app; |
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,87 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var app = require("../app"); | ||
var debug = require("debug")("ecommerceserver:server"); | ||
var http = require("http"); | ||
var { PORT } = require("../config"); | ||
|
||
/** | ||
* Get port from environment and store in Express. | ||
*/ | ||
|
||
var port = normalizePort(PORT || "3000"); | ||
app.set("port", port); | ||
|
||
/** | ||
* Create HTTP server. | ||
*/ | ||
|
||
var server = http.createServer(app); | ||
|
||
/** | ||
* Listen on provided port, on all network interfaces. | ||
*/ | ||
|
||
server.listen(port); | ||
server.on("error", onError); | ||
server.on("listening", onListening); | ||
|
||
/** | ||
* Normalize a port into a number, string, or false. | ||
*/ | ||
|
||
function normalizePort(val) { | ||
var port = parseInt(val, 10); | ||
|
||
if (isNaN(port)) { | ||
// named pipe | ||
return val; | ||
} | ||
|
||
if (port >= 0) { | ||
// port number | ||
return port; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "error" event. | ||
*/ | ||
|
||
function onError(error) { | ||
if (error.syscall !== "listen") { | ||
throw error; | ||
} | ||
|
||
var bind = typeof port === "string" ? "Pipe " + port : "Port " + port; | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case "EACCES": | ||
console.error(bind + " requires elevated privileges"); | ||
process.exit(1); | ||
break; | ||
case "EADDRINUSE": | ||
console.error(bind + " is already in use"); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "listening" event. | ||
*/ | ||
|
||
function onListening() { | ||
var addr = server.address(); | ||
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port; | ||
debug("Listening on " + bind); | ||
} |
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 @@ | ||
require("dotenv").config(); | ||
|
||
module.exports = { | ||
PORT: process.env.PORT, | ||
sequelize_database: process.env.SEQUELIZE_DATABASE, | ||
sequelize_username: process.env.SEQUELIZE_USERNAME, | ||
sequelize_password: process.env.SEQUELIZE_PASSWORD, | ||
sequelize_host: process.env.SEQUELIZE_HOST, | ||
sequelize_dialect: process.env.SEQUELIZE_DIALECT, | ||
app_port: process.env.PORT, | ||
SECRET: process.env.SECRET, | ||
}; |
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,56 @@ | ||
const User = require("../models/user"); | ||
const bcrypt = require("bcrypt"); | ||
// const User = require("../models/mongo"); | ||
|
||
const saltRounds = 10; | ||
const register = async (req, res) => { | ||
const { email, password, fullName } = req.body; | ||
try { | ||
const alreadyExists = await User.findOne({ | ||
where: { | ||
email, | ||
}, | ||
}); | ||
if (alreadyExists) res.status(401).send("Email already exists"); | ||
const salt = bcrypt.genSaltSync(saltRounds); | ||
const hash = bcrypt.hashSync(password, salt); | ||
const newUser = new User({ | ||
email: email.toLowerCase(), | ||
fullName, | ||
password: hash, | ||
}); | ||
|
||
const savedUser = await newUser.save(); | ||
res.status(201).send(savedUser); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send("Something unexpected occurred"); | ||
} | ||
}; | ||
const registerSuperAdmin = async (req, res) => { | ||
const { email, password, fullName } = req.body; | ||
try { | ||
const alreadyExists = await User.findOne({ | ||
where: { | ||
email, | ||
}, | ||
}); | ||
if (alreadyExists) res.status(401).send("Email already exists"); | ||
const salt = bcrypt.genSaltSync(saltRounds); | ||
const hash = bcrypt.hashSync(password, salt); | ||
const newUser = new User({ | ||
email: email.toLowerCase(), | ||
fullName, | ||
password: hash, | ||
role: "superAdmin", | ||
}); | ||
|
||
const savedUser = await newUser.save(); | ||
req.session.User = savedUser; | ||
res.status(201).send(savedUser); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send("Something unexpected occurred"); | ||
} | ||
}; | ||
module.exports = { register, registerSuperAdmin }; |
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,31 @@ | ||
const { Sequelize } = require("sequelize"); | ||
const { | ||
sequelize_database, | ||
sequelize_username, | ||
sequelize_host, | ||
sequelize_dialect, | ||
sequelize_password, | ||
} = require("../config"); | ||
|
||
const sequelize = new Sequelize( | ||
sequelize_database, | ||
sequelize_username, | ||
sequelize_password, | ||
{ | ||
host: sequelize_host, | ||
dialect: sequelize_dialect, | ||
} | ||
); | ||
|
||
sequelize.sync({ alter: true }); | ||
|
||
(async () => { | ||
try { | ||
await sequelize.authenticate(); | ||
console.log("Connection established"); | ||
} catch (err) { | ||
console.log("Unable to connect db"); | ||
} | ||
})(); | ||
|
||
module.exports = sequelize; |
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,8 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const mongodb = "mongodb://127.0.0.1:33017/test"; | ||
mongoose.connect(mongodb, { useNewUrlParser: true, useUnifiedTopology: true }); | ||
|
||
const db = mongoose.connection; | ||
db.on("connect", console.log.bind(console, "connection success")); | ||
db.on("error", console.error.bind(console, "connection error:")); |
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,24 @@ | ||
const redis = require("redis"); | ||
const connectRedis = require("connect-redis"); | ||
const session = require("express-session"); | ||
|
||
const RedisStore = connectRedis(session); | ||
|
||
const redisClient = redis.createClient({ | ||
host: "localhost", | ||
port: 6379, | ||
}); | ||
|
||
redisClient.on("error", function (err) { | ||
console.error("Redis error: ", err); | ||
}); | ||
|
||
redisClient.on("connect", function () { | ||
console.log("Redis client connected"); | ||
}); | ||
|
||
module.exports = { | ||
redisClient, | ||
RedisStore, | ||
session, | ||
}; |
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,9 @@ | ||
const check = (req, res, next) => { | ||
if (req.session.User.role === "superAdmin") { | ||
res.status(200).send("Success"); | ||
} else { | ||
res.status(401).send("Needs to be a super admin"); | ||
} | ||
}; | ||
|
||
module.exports = check; |
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,26 @@ | ||
const User = require("../models/user"); | ||
const { SECRET } = require("../config"); | ||
|
||
const { Strategy, ExtractJwt } = require("passport-jwt"); | ||
|
||
const opts = { | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: SECRET, | ||
}; | ||
|
||
module.exports = (passport) => { | ||
passport.use( | ||
new Strategy(opts, async (payload, done) => { | ||
await User.findByPk(payload.user_id) | ||
.then((user) => { | ||
if (user) { | ||
return done(null, user); | ||
} | ||
return done(null, false); | ||
}) | ||
.catch((err) => { | ||
return done(null, false); | ||
}); | ||
}) | ||
); | ||
}; |
Oops, something went wrong.