2

So i am new comer to keystone CMS and its looking awesome to me i have setup the basic structure and using a default blog project provided by keystone so now i am trying to build the rest API for my admin As the rest API working fine when i am loged in browser in keystone admin panel but when i am testing the same is postman even after setting the basic auth it giving me HTML page I don't know what the wrong with that and how to setup this thing correctly.

Here is my code from index.js

    var _ = require('underscore'),
    keystone = require('keystone'),
    middleware = require('./middleware'),
    // restful = require('restful-keystone-onode')(keystone),
   importRoutes = keystone.importer(__dirname);

 // Common Middleware
 keystone.pre('routes', middleware.initLocals);
 keystone.pre('render', middleware.flashMessages);

 // Import Route Controllers
 var routes = {
   views: importRoutes('./views'),
   api: importRoutes('./api'),
};

// create a route that handles signin

 function signin (req, res) {

   if (!req.body.username || !req.body.password) return res.json({ 
success: false });

    keystone.list('User').model.findOne({ email: req.body.username 
}).exec(function (err, user) {

        if (err || !user) {
         return res.json({
            success: false,
            session: false,
             message: (err && err.message ? err.message : false) || 'Sorry, 
there was an issue signing you in, please try again.',
          });
      }

       keystone.session.signin({ email: user.email, password: 
 req.body.password }, req, res, function (user) {

         return res.json({
            success: true,
            session: true,
            date: new Date().getTime(),
            userId: user.id,
         });

      }, function (err) {

            return res.json({
            success: true,
            session: false,
            message: (err && err.message ? err.message : false) || 'Sorry, 
there was an issue signing you in, please try again.',
         });

      });

    }); 
}

// you'll want one for signout too
function signout (req, res) {
   keystone.session.signout(req, res, function () {
      res.json({ signedout: true });
   });
}

// also create some middleware that checks the current user

// as long as you're using Keystone's session management, the user // will already be loaded if there is a valid current session

function checkAuth (req, res, next) {
      // you could check user permissions here too
   if (req.user) return next();
   return res.status(403).json({ error: 'no access' });
}

// Setup Route Bindings
exports = module.exports = function (app) {

   // Views
   app.get('/', routes.views.index);
   app.get('/blog/:category?', routes.views.blog);
   app.get('/blog/post/:post', routes.views.post);
   app.get('/gallery', routes.views.gallery);
   app.all('/contact', routes.views.contact);

   // add an API endpoint for signing in _before_ your protected routes
   app.post('/api/signin', signin);
   app.post('/api/signout', signout);

 // then bind that middleware in your routes before any paths
 // that should be protected
   app.all('/api*', checkAuth);


   //
   app.get('/api/post/list', keystone.middleware.api, 
routes.api.posts.get);
   app.get('/api/post/:id', keystone.middleware.api, 
routes.api.posts.get);

};

and here is my route/api/post.js /** * Created by nikk on 11/5/17. */ var async = require('async'), keystone = require('keystone');

 var Post = keystone.list('Post');

 /**
 * List Posts
 */
  exports.list = function(req, res) {
    Post.Modal.find(function(err, items) {

       if (err) return res.apiError('database error', err);

       // res.apiResponse({
       //     posts: items
       // });

       res.json(items);

    });
 }

 /**
  * Get Post by ID
 */
  exports.get = function(req, res) {
     Post.model.findById(req.params.id).exec(function(err, item) {

       if (err) return res.apiError('database error', err);
        if (!item) return res.apiError('not found');

       res.apiResponse({
          post: item
       });

       // res.json(item);
    });

 }

I have been trying hard to get this thing done from last day but not able to do working till now please guide me.

Nirmal
  • 939
  • 10
  • 24

0 Answers0