-
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
1 parent
ec9992d
commit 5fec2b0
Showing
16 changed files
with
1,960 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,14 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const app = express(); | ||
|
||
const userRoute = require('./routes/user'); | ||
const imageRoute = require('./routes/images'); | ||
|
||
app.use(bodyParser.json()); | ||
app.use('/uploads', express.static('uploads')); | ||
|
||
app.use("/user", userRoute); | ||
app.use("/images", imageRoute); | ||
|
||
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,23 @@ | ||
{ | ||
"development": { | ||
"username": "root", | ||
"password": null, | ||
"database": "order_processing_system", | ||
"host": "127.0.0.1", | ||
"dialect": "mysql" | ||
}, | ||
"test": { | ||
"username": "root", | ||
"password": null, | ||
"database": "database_test", | ||
"host": "127.0.0.1", | ||
"dialect": "mysql" | ||
}, | ||
"production": { | ||
"username": "root", | ||
"password": null, | ||
"database": "database_production", | ||
"host": "127.0.0.1", | ||
"dialect": "mysql" | ||
} | ||
} |
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,16 @@ | ||
function upload(req, res){ | ||
if(req.file.filename){ | ||
res.status(201).json({ | ||
mesaage: "Image upload successfully", | ||
url: req.file.filename | ||
}); | ||
}else{ | ||
res.status(500).json({ | ||
mesaage: "Something went wrong!" | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
upload: upload | ||
} |
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,97 @@ | ||
const models = require('../models'); | ||
const bcryptjs = require('bcryptjs'); | ||
const jwt = require('jsonwebtoken'); | ||
const validator = require('fastest-validator'); | ||
|
||
function signUp(req, res){ | ||
|
||
models.User.findOne({where:{email:req.body.email}}).then(result => { | ||
if(result){ | ||
res.status(409).json({ | ||
message: "Email already exists!", | ||
}); | ||
}else{ | ||
const user = { | ||
name: req.body.name, | ||
email:req.body.email, | ||
password: req.body.password | ||
} | ||
|
||
const schema = { | ||
name: { type: "string", optional: false, max: 50, pattern: /^[a-zA-Z\s]+$/ }, | ||
email: { type: "email", optional: false, max: 35 }, | ||
password: { type: "string", optional: false, min: 8 } | ||
}; | ||
|
||
const vldator = new validator(); | ||
const validationResponse = vldator.validate(user,schema); | ||
|
||
if(validationResponse !== true){ | ||
return res.status(400).json({ | ||
message:"Validation failed", | ||
error:validationResponse | ||
}); | ||
} | ||
|
||
bcryptjs.genSalt(10, function(err, salt){ | ||
bcryptjs.hash(user.password, salt, function(err, hash){ | ||
user.password = hash; | ||
|
||
models.User.create(user).then(result => { | ||
res.status(201).json({ | ||
message: "User created successfully", | ||
}); | ||
}).catch(error => { | ||
console.log(error); | ||
res.status(500).json({ | ||
message: "Something went wrong!", | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
}).catch(error => { | ||
console.log(error); | ||
res.status(500).json({ | ||
message: "Something went wrong!", | ||
}); | ||
}); | ||
} | ||
|
||
function login(req, res){ | ||
models.User.findOne({where:{email: req.body.email}}).then(user => { | ||
if(user === null){ | ||
res.status(401).json({ | ||
message: "Invalid credentials!", | ||
}); | ||
}else{ | ||
bcryptjs.compare(req.body.password, user.password, function(err, result){ | ||
if(result){ | ||
const token = jwt.sign({ | ||
email: user.email, | ||
userId: user.id | ||
}, process.env.JWT_KEY, function(err, token){ | ||
res.status(200).json({ | ||
message: "Authentication successful!", | ||
token: token | ||
}); | ||
}); | ||
}else{ | ||
res.status(401).json({ | ||
message: "Invalid credentials!", | ||
}); | ||
} | ||
}); | ||
} | ||
}).catch(error => { | ||
res.status(500).json({ | ||
message: "Something went wrong!", | ||
}); | ||
}); | ||
} | ||
|
||
|
||
module.exports = { | ||
signUp: signUp, | ||
login: login | ||
} |
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,19 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
function authCheck(req, res, next){ | ||
try{ | ||
const token = req.headers.authorization.split(" ")[1]; | ||
const decodedToken = jwt.verify(token, process.env.JWT_KEY); | ||
req.userData = decodedToken; | ||
next(); | ||
}catch(e){ | ||
return res.status(401).json({ | ||
'message': "Invalid or expired token provided!", | ||
'error':e | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
authCheck: authCheck | ||
} |
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,34 @@ | ||
'use strict'; | ||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('Users', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
name: { | ||
type: Sequelize.STRING | ||
}, | ||
email: { | ||
type: Sequelize.STRING | ||
}, | ||
password: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
async down(queryInterface, Sequelize) { | ||
await queryInterface.dropTable('Users'); | ||
} | ||
}; |
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,43 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Sequelize = require('sequelize'); | ||
const process = require('process'); | ||
const basename = path.basename(__filename); | ||
const env = process.env.NODE_ENV || 'development'; | ||
const config = require(__dirname + '/../config/config.json')[env]; | ||
const db = {}; | ||
|
||
let sequelize; | ||
if (config.use_env_variable) { | ||
sequelize = new Sequelize(process.env[config.use_env_variable], config); | ||
} else { | ||
sequelize = new Sequelize(config.database, config.username, config.password, config); | ||
} | ||
|
||
fs | ||
.readdirSync(__dirname) | ||
.filter(file => { | ||
return ( | ||
file.indexOf('.') !== 0 && | ||
file !== basename && | ||
file.slice(-3) === '.js' && | ||
file.indexOf('.test.js') === -1 | ||
); | ||
}) | ||
.forEach(file => { | ||
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); | ||
db[model.name] = model; | ||
}); | ||
|
||
Object.keys(db).forEach(modelName => { | ||
if (db[modelName].associate) { | ||
db[modelName].associate(db); | ||
} | ||
}); | ||
|
||
db.sequelize = sequelize; | ||
db.Sequelize = Sequelize; | ||
|
||
module.exports = db; |
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,25 @@ | ||
'use strict'; | ||
const { | ||
Model | ||
} = require('sequelize'); | ||
module.exports = (sequelize, DataTypes) => { | ||
class User extends Model { | ||
/** | ||
* Helper method for defining associations. | ||
* This method is not a part of Sequelize lifecycle. | ||
* The `models/index` file will call this method automatically. | ||
*/ | ||
static associate(models) { | ||
// define association here | ||
} | ||
} | ||
User.init({ | ||
name: DataTypes.STRING, | ||
email: DataTypes.STRING, | ||
password: DataTypes.STRING | ||
}, { | ||
sequelize, | ||
modelName: 'User', | ||
}); | ||
return User; | ||
}; |
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,5 @@ | ||
{ | ||
"env": { | ||
"JWT_KEY": "I$^%6szhGUMLNv" | ||
} | ||
} |
Oops, something went wrong.