@@ -1,12 +1,18 @@
var express = require ( 'express' ) ;
var router = express . Router ( ) ;
var mongoose = require ( 'mongoose' ) ;
var User = mongoose . model ( 'User' ) ;
var jwt = require ( 'express-jwt' ) ;
var auth = jwt ( { secret : 'SECRET' , userProperty : 'payload' } ) ;
//put secret here...payload try npm start here
/* GET home page. */
router . get ( '/' , function ( req , res , next ) {
res . render ( 'index' , { title : 'Express' } ) ;
} ) ;
var mongoose = require ( 'mongoose' ) ;
var passport = require ( 'passport' ) ;
var Post = mongoose . model ( 'Post' ) ;
var Comment = mongoose . model ( 'Comment' ) ;
@@ -17,9 +23,11 @@ router.get('/posts', function(req, res, next) {
res . json ( posts ) ;
} ) ;
} ) ;
router . post ( '/posts' , function ( req , res , next ) {
//create a new comment
router . post ( '/posts' , auth , function ( req , res , next ) {
var post = new Post ( req . body ) ;
post . author = req . payload . username ;
//putting our post.author here = req.payload.usernme;
post . save ( function ( err , post ) {
if ( err ) { return next ( err ) ; }
@@ -80,16 +88,16 @@ router.param('comment', function(req, res, next, id) {
} ) ;
//
router . put ( '/posts/:post/upvote' , function ( req , res , next ) {
// upvoting a new post
router . put ( '/posts/:post/upvote' , auth , function ( req , res , next ) {
req . post . upvote ( function ( err , post ) {
if ( err ) { return next ( err ) ; }
res . json ( post ) ;
} ) ;
} ) ;
router . put ( '/posts/:post/comments/:comment/upvote' , function ( req , res , next ) {
//upvoting a comment
router . put ( '/posts/:post/comments/:comment/upvote' , auth , function ( req , res , next ) {
req . comment . upvote ( function ( err , comment ) {
if ( err ) { return next ( err ) ; }
@@ -98,9 +106,11 @@ router.put('/posts/:post/comments/:comment/upvote', function(req, res, next) {
} ) ;
//56cb6cca8d2aaaff04d7030c
router . post ( '/posts/:post/comments' , function ( req , res , next ) {
router . post ( '/posts/:post/comments' , auth , function ( req , res , next ) {
var comment = new Comment ( req . body ) ;
comment . post = req . post ;
comment . author = req . payload . username ;
//creating a username here
comment . save ( function ( err , comment ) {
if ( err ) { return next ( err ) ; }
@@ -110,6 +120,44 @@ router.post('/posts/:post/comments', function(req, res, next) {
if ( err ) { return next ( err ) ; }
res . json ( comment ) ;
} ) ;
} ) ;
} ) ;
} ) ;
router . post ( '/register' , function ( req , res , next ) {
if ( ! req . body . username || ! req . body . password ) {
return res . status ( 400 ) . json ( { message : 'Please fill out all fields' } ) ;
}
var user = new User ( ) ;
user . username = req . body . username ;
user . setPassword ( req . body . password ) ;
user . save ( function ( err ) {
if ( err ) { return next ( err ) ; }
return res . json ( { token : user . generateJWT ( ) } ) ;
} ) ;
} ) ;
router . post ( '/login' , function ( req , res , next ) {
if ( ! req . body . username || ! req . body . password ) {
return res . status ( 400 ) . json ( { message : 'Please fill out all fields' } ) ;
}
passport . authenticate ( 'local' , function ( err , user , info ) {
if ( err ) { return next ( err ) ; }
if ( user ) {
return res . json ( { token : user . generateJWT ( ) } ) ;
} else {
return res . status ( 401 ) . json ( info ) ;
}
} ) ( req , res , next ) ;
} ) ;
module . exports = router ;