Skip to content

Commit

Permalink
added auth route
Browse files Browse the repository at this point in the history
  • Loading branch information
Amritesh Srivastava committed Apr 7, 2020
1 parent d18716c commit 83a9c72
Show file tree
Hide file tree
Showing 23 changed files with 980 additions and 2 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified captain-definition
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
'secret': 'thisissomethingsecrete',
'database': 'mongodb://127.0.0.1:27017/auth'
};
Empty file modified config/index.js
100644 → 100755
Empty file.
9 changes: 9 additions & 0 deletions controllers/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const passport = require('passport');
const express = require('express');
const dashboardService = require('../services/dashboard');

let router = express.Router();

router.get('/', passport.authenticate('jwt', { session: false }), dashboardService.getDashboard);

module.exports = router;
Empty file modified controllers/getMLResponse.js
100644 → 100755
Empty file.
8 changes: 8 additions & 0 deletions controllers/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function index(request, response) {
console.log('home controller');
response.json('This is home route');
}

module.exports = {
index: index
};
17 changes: 17 additions & 0 deletions controllers/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const express = require('express');
const loginService = require('../services/login');

let router = express.Router();


// router.post('/', loginService.loginUser);
// loginService.loginUser(req, res);

function login(request, response) {
console.log('login controller');
loginService.loginUser(request, response);
}

module.exports = {
login: login
};
8 changes: 8 additions & 0 deletions controllers/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const registerService = require('../services/register');

let router = express.Router();

router.post('/', registerService.registerUser);

module.exports = router;
Empty file modified controllers/upload.js
100644 → 100755
Empty file.
13 changes: 12 additions & 1 deletion index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@ const bodyParser = require('body-parser');
const cors = require('cors')
require('dotenv').config()
const server = express()
const passport = require('passport');
const cookieParser = require('cookie-parser');
// const mongoose = require('mongoose');

const port = process.env.PORT
const imageUploadRoute = require('./routes/upload')
const getMLResponseRoute = require('./routes/getMLResponse')
const login = require('./routes/login')
const fileUpload = require('express-fileupload')

server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
server.use(fileUpload())
server.use(cors())
server.use(express.static(path.join(__dirname, '../client/public')))

server.use(passport.initialize());
server.use(cookieParser());
// mongoose.connect('mongodb://127.0.0.1:27017/auth', {
// useNewUrlParser: true,
// useCreateIndex: true
// });

server.use('/api/v1/upload', imageUploadRoute)
server.use('/api/v1/getMLResponse', getMLResponseRoute)
server.use('/api/v1/login', login)

http.createServer(server).listen(port, () => {
console.log(`Express server listening on port ${port}`);
Expand Down
58 changes: 58 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');

const UserSchema = new mongoose.Schema({
email: {
type: String,
lowercase: true,
unique: true,
required: true
},
password: {
type: String,
required: true
},
role: {
type: String,
enum: ['Client', 'Manager', 'Admin'],
default: 'Client'
}
});

UserSchema.pre('save', function(next) {
let user = this;

if (this.isModified('password') || this.isNew) {
bcrypt.genSalt(10, (err, salt) => {
if (err) {
console.log(err);
return next(err);
}

bcrypt.hash(user.password, salt, (err, hash) => {
if (err) {
console.log(err);
return next(err);
}

user.password = hash;
next();
});
});
} else {
return next();
}
});

// Create method to compare password input to password saved in database
UserSchema.methods.comparePassword = function(pw, cb) {
bcrypt.compare(pw, this.password, function(err, isMatch) {
if (err) {
return cb(err);
}

cb(null, isMatch);
});
};

module.exports = mongoose.model('User', UserSchema);
Loading

0 comments on commit 83a9c72

Please sign in to comment.