Socket
Socket
Sign inDemoInstall

seguir-express-middleware

Package Overview
Dependencies
2
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.1.1

173

index.js

@@ -18,14 +18,44 @@ /**

*
* authApi is middleware that checks that the user is authorised to use the API, it returns a JSON response with a 403.
* authForm is middleware that checks the user is authorised to post data via a form, and so redirects to a login page.
* authApi is middleware that checks that the user is authorised to use the API.
*
* The app is passed in to avoid this project having a dependency on Express.
* This middleware assumes that the user exists on 'req.user', with property 'seguirId' (TODO: can be over-ridden with options.seguirIdProperty);
*
* Express and Seguir are passed in to avoid the express and seguir dependency outside of testing.
*
*/
var express = require('express');
var _ = require('lodash');
module.exports = function(seguir, authApi, authForm) {
module.exports = function(options, express, seguir, authApi) {
var router = express.Router();
var router = express.Router();
var defaults = {
userProperty: 'user',
seguirIdProperty: 'seguirId',
post: true,
friend: true,
follow: true,
like: true
}
options = _.defaults(options, defaults);
var respondWithError = function(err, res) {
res.status(err.statusCode || 500);
res.send(err);
}
function getSeguirId(req) {
if(options.userProperty && options.seguirIdProperty && req[options.userProperty] && req[options.userProperty][options.seguirIdProperty]) {
return req[options.userProperty][options.seguirIdProperty];
}
if(options.seguirIdProperty && req[options.seguirIdProperty]) {
return req[options.seguirIdProperty];
}
return null;
}
/**

@@ -37,3 +67,3 @@ * @apiDefine ApiPosts Posts

* @api {post} /post Add a post
* @apiName Posts
* @apiName AddPost
* @apiGroup ApiPosts

@@ -48,9 +78,32 @@ * @apiVersion 1.0.0

*/
router.post('/post', authForm, function(req, res) {
seguir.addPost(req.user.seguirId, req.body.content, Date.now(), req.body.isprivate, function(err, post) {
res.redirect(req.body.returnUrl);
})
router.post('/post', authApi, function(req, res) {
var isprivate = req.body.isprivate === 'true';
var ispersonal = req.body.ispersonal === 'true';
var seguirId = getSeguirId(req);
seguir.addPost(seguirId, req.body.content, Date.now(), isprivate, ispersonal, function(err, post) {
if(err) { return respondWithError(err, res); }
res.send(post);
});
});
/**
* @api {del} /post/:post Remove a post
* @apiName DeletePost
* @apiGroup ApiPosts
* @apiVersion 1.0.0
*
* @apiDescription Deletes a post
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} post the guid of the post
*
*/
router.delete('/post/:post', authApi, function(req, res) {
var seguirId = getSeguirId(req);
seguir.removePost(seguirId, req.params.post, function(err, result) {
if(err) { return respondWithError(err, res); }
res.send(result);
});
});
/**
* @apiDefine ApiFriends Friends

@@ -72,3 +125,5 @@ */

router.post('/friend', authApi, function(req, res) {
seguir.addFriendRequest(req.user.seguirId, req.body.user, req.body.message, Date.now(), function(err, friend) {
var seguirId = getSeguirId(req);
seguir.addFriendRequest(seguirId, req.body.user, req.body.message, Date.now(), function(err, friend) {
if(err) { return respondWithError(err, res); }
res.send(friend);

@@ -90,3 +145,5 @@ });

router.delete('/friend/:user', authApi, function(req, res) {
seguir.removeFriend(req.user.seguirId, req.params.user, function(err, result) {
var seguirId = getSeguirId(req);
seguir.removeFriend(seguirId, req.params.user, function(err, result) {
if(err) { return respondWithError(err, res); }
res.send(result);

@@ -109,2 +166,3 @@ });

seguir.acceptFriendRequest(req.user.seguirId, req.body.friend_request, function(err, friend_request) {
if(err) { return respondWithError(err, res); }
res.send(friend_request);

@@ -130,5 +188,7 @@ });

router.post('/follow', authApi, function(req, res) {
seguir.followUser(req.user.seguirId, req.body.user, Date.now(), function(err, follow) {
var seguirId = getSeguirId(req);
seguir.followUser(seguirId, req.body.user, Date.now(), function(err, follow) {
if(err) { return respondWithError(err, res); }
res.send(follow);
})
});
});

@@ -148,3 +208,5 @@

router.delete('/follow/:user', authApi, function(req, res) {
seguir.unFollowUser(req.user.seguirId, req.params.user, function(err, result) {
var seguirId = getSeguirId(req);
seguir.unFollowUser(seguirId, req.params.user, function(err, result) {
if(err) { return respondWithError(err, res); }
res.send(result);

@@ -154,4 +216,83 @@ });

/**
* @apiDefine ApiLikes Likes
*/
/**
* @api {post} /like Add a like
* @apiName AddLike
* @apiGroup ApiLikes
* @apiVersion 1.0.0
*
* @apiDescription Creates a like
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} item the url of the item they like
*
*/
router.post('/like', authApi, function(req, res) {
var seguirId = getSeguirId(req);
seguir.addLike(seguirId, req.body.item, function(err, like) {
if(err) { return respondWithError(err, res); }
res.send(like);
});
});
/**
* @api {get} /like/item Check if a user likes an item
* @apiName GetLike
* @apiGroup ApiLikes
* @apiVersion 1.0.0
*
* @apiDescription Checks a like
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} item the url of the item they like
*
*/
router.get('/like/:item', authApi, function(req, res) {
var seguirId = getSeguirId(req);
seguir.checkLike(seguirId, req.params.item, function(err, like) {
if(err) { return respondWithError(err, res); }
res.send(like);
});
});
/**
* @api {del} /like/:item Add a like
* @apiName AddLike
* @apiGroup ApiLikes
* @apiVersion 1.0.0
*
* @apiDescription Creates a like
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
* @apiParam {String} item the url of the item they like
*
*/
router.delete('/like/:item', authApi, function(req, res) {
var seguirId = getSeguirId(req);
seguir.removeLike(seguirId, req.params.item, function(err, like) {
if(err) { return respondWithError(err, res); }
res.send(like);
});
});
/**
* @api {del} /feed Get feed for logged in user
* @apiName GetFeed
* @apiGroup ApiFeeds
* @apiVersion 1.0.0
*
* @apiDescription Gets a user feed
* @apiParam {Object} user expects req.user to be present, with req.user.seguirId
*
*/
router.get('/feed', authApi, function(req, res) {
var seguirId = getSeguirId(req);
seguir.getUserFeed(seguirId, seguirId, 50, function(err, feed) {
if(err) { return respondWithError(err, res); }
res.send(feed);
});
});
return router;
}

4

package.json
{
"name": "seguir-express-middleware",
"version": "0.1.0",
"version": "0.1.1",
"description": "Experss middleware for seguir self-hosted social network backend and API",

@@ -28,3 +28,2 @@ "main": "index.js",

"async": "^0.9.0",
"express": "^4.11.0",
"lodash": "^2.4.1"

@@ -34,2 +33,3 @@ },

"apidoc": "^0.12.1",
"express": "^4.11.0",
"expect.js": "^0.3.1",

@@ -36,0 +36,0 @@ "istanbul": "^0.3.5",

@@ -7,2 +7,25 @@ # Seguir Express Middleware

## Middleware
This is express middleware that you can add to your own application, that will expose a set of local endpoints that you can use to build a front end for Seguir.
We have done it this way to enable you to wire up whatever authentication mechanism you like for users, with the only requirement being that the request hits the seguir middleware with a req.user object that has a seguirID (this can be over-ridden).
```
req.user = {
seguirId: '92029c7b-0ded-4d4f-b782-b2514c3dbb47'
}
```
## Adding to your application
Assuming your express app is running on /app, you can add the endpoints via:
```
var Seguir = require('seguir/client');
var seguir = new Seguir(config);
var seguirMiddleware = require('seguir-express-middleware');
app.use('/social', seguirMiddleware(seguir, authApi));
```
[Pronounced: seh-geer]
'use strict';
var express = require('express');
var expect = require('expect.js');
var mw = require('../../index.js');
var apiFn = function() {};
describe('Basic Middleware', function() {
it('returns a router', function(done) {
expect(typeof mw({})).to.be('function');
expect(mw({}).toString()).to.contain('router');
var middleware = mw({}, express, {}, apiFn);
expect(typeof middleware).to.be('function');
expect(middleware.toString()).to.contain('router');
done();
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc