Browse Source

added admin model and controller

master
arnaucode 7 years ago
parent
commit
81dd28a7ea
3 changed files with 141 additions and 0 deletions
  1. +119
    -0
      controllers/adminController.js
  2. +17
    -0
      models/adminModel.js
  3. +5
    -0
      server.js

+ 119
- 0
controllers/adminController.js

@ -0,0 +1,119 @@
//File: controllers/userController.js
var mongoose = require('mongoose');
var userModel = mongoose.model('adminModel');
var config = require('../config');
var pageSize = config.pageSize;
/* */
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var express = require("express");
var app = express();
var config = require('../config'); // get our config file
app.set('superSecret', config.secret); // secret variable
var crypto = require('crypto');
/* */
var request = require('request');
//POST - Insert a new User in the DB
exports.signup = function(req, res) {
//get random avatar
var r = getRand(1, 10);
randAvatar = getAvatar(r);
var user = new userModel({
username: req.body.username,
password: crypto.createHash('sha256').update(req.body.password).digest('base64'),
description: req.body.description,
avatar: randAvatar,
email: req.body.email,
phone: req.body.phone,
telegram: req.body.telegram
});
if (user.username == undefined) {
return res.status(500).jsonp("empty inputs");
} else if (user.password == undefined) {
return res.status(500).jsonp("empty inputs");
} else if (user.email == undefined) {
return res.status(500).jsonp("empty inputs");
}
user.save(function(err, user) {
if (err) return res.send(500, err.message);
exports.login(req, res);
});
};
//POST - auth user
exports.login = function(req, res) {
// find the user
userModel.findOne({
username: req.body.username
})
.select('+password')
.exec(function(err, user) {
if (err) throw err;
if (!user) {
res.json({
success: false,
message: 'Authentication failed. User not found.'
});
} else if (user) {
req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64');
// check if password matches
if (user.password != req.body.password) {
res.json({
success: false,
message: 'Authentication failed. Wrong password.'
});
} else {
// if user is found and password is right
// create a token
var token = jwt.sign({
foo: 'bar'
}, app.get('superSecret'), {
//expiresInMinutes: 1440 // expires in 24 hours
//expiresIn: '60m'
});
user.token = token;
user.save(function(err, user) {
if (err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
console.log(user);
// return the information including token as JSON
user.password = "";
res.json({
success: true,
message: 'Enjoy your token!',
token: token,
user: user
});
});
}
}
});
};
exports.changePassword = function(req, res) {
//if(req.body.)
userModel.update({
'token': req.headers['x-access-token']
}, req.body,
function(err) {
if (err) return console.log(err);
exports.getUserByToken(req, res);
});
};

+ 17
- 0
models/adminModel.js

@ -0,0 +1,17 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');
var adminSchema = new Schema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true, select: false },
email: { type: String, required: true },
phone: { type: String },
telegram: { type: String },
token: { type: String, select: false }
})
adminSchema.plugin(mongooseUniqueValidator);
module.exports = mongoose.model('adminModel', adminSchema);

+ 5
- 0
server.js

@ -35,9 +35,11 @@ var userMdl = require('./models/userModel')(app, mongoose);
var notificationMdl = require('./models/notificationModel')(app, mongoose); var notificationMdl = require('./models/notificationModel')(app, mongoose);
var travelMdl = require('./models/travelModel')(app, mongoose); var travelMdl = require('./models/travelModel')(app, mongoose);
var commentMdl = require('./models/commentModel')(app, mongoose); var commentMdl = require('./models/commentModel')(app, mongoose);
var adminMdl = require('./models/adminModel')(app, mongoose);
var userCtrl = require('./controllers/userController'); var userCtrl = require('./controllers/userController');
var searchCtrl = require('./controllers/searchController'); var searchCtrl = require('./controllers/searchController');
var travelCtrl = require('./controllers/travelController'); var travelCtrl = require('./controllers/travelController');
var adminCtrl = require('./controllers/adminController');
/*// Example Route /*// Example Route
var router = express.Router(); var router = express.Router();
@ -73,6 +75,9 @@ apiRoutes.route('/travels/id/:travelid')
.get(travelCtrl.getTravelById); .get(travelCtrl.getTravelById);
apiRoutes.route('/admin/login')
.post(adminCtrl.login);
// OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app // OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
// route middleware to verify a token // route middleware to verify a token
apiRoutes.use(function(req, res, next) { apiRoutes.use(function(req, res, next) {

Loading…
Cancel
Save