-
Notifications
You must be signed in to change notification settings - Fork 2
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
11 changed files
with
1,062 additions
and
31 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,7 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
'config': path.resolve('config', 'database_sequelize.js'), | ||
'models-path': path.resolve('models', 'sql'), | ||
'migrations-path': path.resolve('migrations','sql') | ||
}; |
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 @@ | ||
// const fs = require('fs'); | ||
|
||
module.exports = { | ||
test: { | ||
username: process.env.CI_DB_USERNAME, | ||
password: process.env.CI_DB_PASSWORD, | ||
database: process.env.CI_DB_NAME, | ||
host: '127.0.0.1', | ||
port: 3306, | ||
dialect: 'mysql', | ||
dialectOptions: { | ||
bigNumberStrings: true | ||
} | ||
}, | ||
production: { | ||
username: process.env.POSTGRES_USER, | ||
password: process.env.POSTGRES_PASSWORD, | ||
database: process.env.POSTGRES_DB, | ||
host: process.env.POSTGRES_HOST, | ||
port: 5432, | ||
dialect: 'postgres' | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
module.exports = { | ||
'secret': 'thisissomethingsecrete', | ||
'database': 'mongodb://127.0.0.1:27017/auth' | ||
}; | ||
const {Sequelize}=require('sequelize'); | ||
const config=require('./index'); | ||
const POSTGRES_HOST= config.POSTGRES_HOST; | ||
const POSTGRES_DB=config.POSTGRES_DB; | ||
const POSTGRES_USER=config.POSTGRES_USER; | ||
const POSTGRES_PASSWORD=config.POSTGRES_PASSWORD; | ||
const db=new Sequelize(POSTGRES_DB,POSTGRES_USER,POSTGRES_PASSWORD,{ | ||
host:POSTGRES_HOST, | ||
dialect:'postgres', | ||
}); | ||
module.exports=db; | ||
//NOT USING THIS!! |
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
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,49 @@ | ||
const bcrypt = require('bcrypt'); | ||
const jwt = require('jsonwebtoken'); | ||
const db =require('../models/sql'); | ||
const User = db.User; | ||
|
||
const createUser = (req, res) => { | ||
// const { errors, isValid } = validateRegisterForm(req.body); | ||
let { | ||
name, | ||
role, | ||
email, | ||
password, | ||
} = req.body; | ||
|
||
// check validation | ||
// if(!isValid) { | ||
// return res.status(400).json(errors); | ||
// } | ||
|
||
User.findAll({ where: { email } }).then(user => { | ||
if (user.length) { | ||
return res.status(400).json({ email: 'Email already exists!' }); | ||
} else { | ||
let newUser = { | ||
name, | ||
role, | ||
email, | ||
password, | ||
}; | ||
bcrypt.genSalt(10, (err, salt) => { | ||
bcrypt.hash(newUser.password, salt, (err, hash) => { | ||
if (err) throw err; | ||
newUser.password = hash; | ||
User.create(newUser) | ||
.then(user => { | ||
res.json({ user }); | ||
}) | ||
.catch(err => { | ||
res.status(500).json({ err }); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports={ | ||
createUser:createUser | ||
}; |
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
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,37 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Sequelize = require('sequelize'); | ||
const basename = path.basename(__filename); | ||
const env = 'production'; | ||
const config = require(__dirname + '/../../config/database_sequelize.js')[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'); | ||
}) | ||
.forEach(file => { | ||
const model = sequelize['import'](path.join(__dirname, file)); | ||
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,14 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const User = sequelize.define('User', { | ||
user_id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true}, | ||
email: {type: DataTypes.STRING, allowNull: false}, | ||
password: {type: DataTypes.STRING, allowNull: false}, | ||
name: {type: DataTypes.STRING}, | ||
role: {type: DataTypes.STRING}, | ||
verified:{type: DataTypes.INTEGER,defaultValue:0} //Can be used to setup email verification | ||
}, {tableName: 'users'}); | ||
User.associate = function (models) { | ||
// associations can be defined here | ||
}; | ||
return User; | ||
}; |
Oops, something went wrong.