mirror of
https://github.com/arnaucube/thoughts.git
synced 2026-02-07 03:36:49 +01:00
tokens fully implemented, get all thoughts from user by user id implemented
This commit is contained in:
@@ -22,6 +22,26 @@ exports.findById = function(req, res) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.findAllThoughtsFromUsername = function(req, res) {
|
||||||
|
thoughtModel.find({
|
||||||
|
authorname: req.params.userid
|
||||||
|
}, function(err, thoughts) {
|
||||||
|
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
if (!thoughts) {
|
||||||
|
res.json({ success: false, message: 'no thoughts for user' });
|
||||||
|
} else if (thoughts) {
|
||||||
|
console.log(thoughts);
|
||||||
|
// return the information including token as JSON
|
||||||
|
res.jsonp(thoughts);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
//POST - Insert a new TVShow in the DB
|
//POST - Insert a new TVShow in the DB
|
||||||
exports.addThought = function(req, res) {
|
exports.addThought = function(req, res) {
|
||||||
console.log('POST new thought, content: ' + req.body.content);
|
console.log('POST new thought, content: ' + req.body.content);
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ exports.findById = function(req, res) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//POST - Insert a new TVShow in the DB
|
//POST - Insert a new TVShow in the DB
|
||||||
exports.addUser = function(req, res) {
|
exports.addUser = function(req, res) {
|
||||||
console.log('POST new user, name: ' + req.body.username);
|
console.log('POST new user, name: ' + req.body.username);
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ var mongoose = require('mongoose'),
|
|||||||
var thoughtSchema = new Schema({
|
var thoughtSchema = new Schema({
|
||||||
time: { type: String },
|
time: { type: String },
|
||||||
content: { type: String },
|
content: { type: String },
|
||||||
authorname: { type: String }
|
authorname: { type: String },
|
||||||
|
user_id: { type: String },
|
||||||
|
fav: { type: String } //array amb els users que posen fav
|
||||||
})
|
})
|
||||||
module.exports = mongoose.model('thoughtModel', thoughtSchema);
|
module.exports = mongoose.model('thoughtModel', thoughtSchema);
|
||||||
|
|||||||
51
server.js
51
server.js
@@ -40,31 +40,70 @@ router.get('/', function(req, res) {
|
|||||||
app.use(router);*/
|
app.use(router);*/
|
||||||
app.use(express.static(__dirname + '/web'));
|
app.use(express.static(__dirname + '/web'));
|
||||||
|
|
||||||
// API routes
|
// API routes ------------------------------------------------------
|
||||||
var apiRoutes = express.Router();
|
var apiRoutes = express.Router();
|
||||||
|
|
||||||
apiRoutes.route('/users')
|
apiRoutes.route('/users')
|
||||||
.get(userCtrl.findAllUsers)
|
.get(userCtrl.findAllUsers)
|
||||||
.post(userCtrl.addUser);
|
.post(userCtrl.addUser);
|
||||||
|
apiRoutes.route('/thoughts/user/:userid')
|
||||||
|
.get(thoughtCtrl.findAllThoughtsFromUsername);
|
||||||
|
|
||||||
|
apiRoutes.route('/auth')
|
||||||
|
.post(userCtrl.login);
|
||||||
|
|
||||||
|
apiRoutes.route('/thoughts')
|
||||||
|
.get(thoughtCtrl.findAllThoughts);
|
||||||
|
|
||||||
|
apiRoutes.route('/thoughts/:id')
|
||||||
|
.get(thoughtCtrl.findById)
|
||||||
|
|
||||||
|
// route middleware to verify a token
|
||||||
|
apiRoutes.use(function(req, res, next) {
|
||||||
|
|
||||||
|
// check header or url parameters or post parameters for token
|
||||||
|
var token = req.body.token || req.query.token || req.headers['x-access-token'];
|
||||||
|
|
||||||
|
// decode token
|
||||||
|
if (token) {
|
||||||
|
|
||||||
|
// verifies secret and checks exp
|
||||||
|
jwt.verify(token, app.get('superSecret'), function(err, decoded) {
|
||||||
|
if (err) {
|
||||||
|
return res.json({ success: false, message: 'Failed to authenticate token.' });
|
||||||
|
} else {
|
||||||
|
// if everything is good, save to request for use in other routes
|
||||||
|
req.decoded = decoded;
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// if there is no token
|
||||||
|
// return an error
|
||||||
|
return res.status(403).send({
|
||||||
|
success: false,
|
||||||
|
message: 'No token provided.'
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
apiRoutes.route('/users/:id')
|
apiRoutes.route('/users/:id')
|
||||||
.get(userCtrl.findById)
|
.get(userCtrl.findById)
|
||||||
.put(userCtrl.updateActivity)
|
.put(userCtrl.updateActivity)
|
||||||
.delete(userCtrl.deleteActivity);
|
.delete(userCtrl.deleteActivity);
|
||||||
|
|
||||||
apiRoutes.route('/auth')
|
|
||||||
.post(userCtrl.login);
|
|
||||||
|
|
||||||
apiRoutes.route('/thoughts')
|
apiRoutes.route('/thoughts')
|
||||||
.get(thoughtCtrl.findAllThoughts)
|
|
||||||
.post(thoughtCtrl.addThought);
|
.post(thoughtCtrl.addThought);
|
||||||
|
|
||||||
apiRoutes.route('/thoughts/:id')
|
apiRoutes.route('/thoughts/:id')
|
||||||
.get(thoughtCtrl.findById)
|
|
||||||
.put(thoughtCtrl.updateActivity)
|
.put(thoughtCtrl.updateActivity)
|
||||||
.delete(thoughtCtrl.deleteActivity);
|
.delete(thoughtCtrl.deleteActivity);
|
||||||
|
|
||||||
app.use('/api', apiRoutes);
|
app.use('/api', apiRoutes);
|
||||||
|
// end of API routes -------------------------------------
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
app.listen(3000, function() {
|
app.listen(3000, function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user