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

Lots of tools updated as many things were not working #24

Open
wants to merge 1 commit into
base: 13-controllers
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
6 changes: 3 additions & 3 deletions api/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const bcryptjs = require("bcryptjs");
const jwt = require("jsonwebtoken");

const User = require("../models/user");
Expand All @@ -13,7 +13,7 @@ exports.user_signup = (req, res, next) => {
message: "Mail exists"
});
} else {
bcrypt.hash(req.body.password, 10, (err, hash) => {
bcryptjs.hash(req.body.password, 10, (err, hash) => {
if (err) {
return res.status(500).json({
error: err
Expand Down Expand Up @@ -53,7 +53,7 @@ exports.user_login = (req, res, next) => {
message: "Auth failed"
});
}
bcrypt.compare(req.body.password, user[0].password, (err, result) => {
bcryptjs.compare(req.body.password, user[0].password, (err, result) => {
if (err) {
return res.status(401).json({
message: "Auth failed"
Expand Down
1 change: 1 addition & 0 deletions api/middleware/check-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const jwt = require('jsonwebtoken');

module.exports = (req, res, next) => {
try {
console.log(req);
const token = req.headers.authorization.split(" ")[1];
const decoded = jwt.verify(token, process.env.JWT_KEY);
req.userData = decoded;
Expand Down
2 changes: 1 addition & 1 deletion api/models/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
price: { type: Number, required: true },
productImage: { type: String, required: true }
productImage: { type: String, required: false }
});

module.exports = mongoose.model('Product', productSchema);
24 changes: 23 additions & 1 deletion api/routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ const router = express.Router();
const multer = require('multer');
const checkAuth = require('../middleware/check-auth');
const ProductsController = require('../controllers/products');
const GridFsStorage = require('multer-gridfs-storage');

const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './uploads/');
},
filename: function(req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
cb(null, Math.floor(Math.random() * Math.floor(10000)) + file.originalname);
}
});

Expand All @@ -30,6 +31,27 @@ const upload = multer({
fileFilter: fileFilter
});

// const MONGO_DB_URI = 'mongodb+srv://nodeshopnew:[email protected]/testnew?retryWrites=true&w=majority';

// var storage = new GridFsStorage({
// url:MONGO_DB_URI,
// options: { useNewUrlParser: true, useUnifiedTopology: true },
// file: (req, file) => {
// const match = ["image/png", "image/jpeg"];

// if (match.indexOf(file.mimetype) === -1) {
// const filename = `${Date.now()}-bezkoder-${file.originalname}`;
// return filename;
// }

// return {
// bucketName: "photos",
// filename: `${Date.now()}-bezkoder-${file.originalname}`
// };
// }
// });


router.get("/", ProductsController.products_get_all);

router.post("/", checkAuth, upload.single('productImage'), ProductsController.products_create_product);
Expand Down
18 changes: 10 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ const productRoutes = require("./api/routes/products");
const orderRoutes = require("./api/routes/orders");
const userRoutes = require('./api/routes/user');

mongoose.connect(
"mongodb://node-shop:" +
process.env.MONGO_ATLAS_PW +
"@node-rest-shop-shard-00-00-wovcj.mongodb.net:27017,node-rest-shop-shard-00-01-wovcj.mongodb.net:27017,node-rest-shop-shard-00-02-wovcj.mongodb.net:27017/test?ssl=true&replicaSet=node-rest-shop-shard-0&authSource=admin",
{
useMongoClient: true
}
);
const MONGO_DB_URI = 'mongodb+srv://nodeshopnew:[email protected]/testnew?retryWrites=true&w=majority';

mongoose.connect(MONGO_DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.on('connected', () => {
console.log('Mongoose is connected!')

});
mongoose.Promise = global.Promise;

app.use(morgan("dev"));
Expand Down
Loading