Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-restful lockit not work with restful lockit #25

Open
idy opened this issue Aug 8, 2014 · 2 comments
Open

Non-restful lockit not work with restful lockit #25

idy opened this issue Aug 8, 2014 · 2 comments

Comments

@idy
Copy link

idy commented Aug 8, 2014

Hi zemirco,
I want to use a restful lockit with a non-restul lockit, which feeds all static pages while listen to ajax requests. There is no option to disable restful lockit from rendering, which make restful lockit won't work with non-restful lockit.
There are two approach to make it work, one is add options to add restful routers to a non-restful lockit, and the other is to add an option to disable rendering. I think the second approach is more clear.

Add one line to Lockit.prototype.rest

if (!this.config.rest.index) return;

This will prevent render pages if config.rest.index is not set.

Then I can use lockit as follow:

var Lockit = require('Lockit');
var express = require('express');
var restConfig;
var config;
var lockit = new Lockit(config);
var restLockit = new Lockit(restConfig);
app = express();
app.use(lockit.router)
app.use(restLockit.router);
@zemirco
Copy link
Owner

zemirco commented Aug 18, 2014

I don't get what you're trying to achieve. Why would you use Lockit twice? Could you give an example?

@idy
Copy link
Author

idy commented Aug 19, 2014

I want to impl a passport center, which support static pages while a restiful api. So I need to create two lockit instances and make express use their routers in the same time.

This is my code:

var router = exports.router = express.Router();

if (settings.lockit) {
  var lockit = new Lockit(settings.lockit);
  lockit.config.login.handleResponse = false;
  lockit.on('signup', onSignup);
  lockit.on('login', function(user, res) {
    var req = res.req;
    var returnTo;
    if (req.session) {
      req.session.user = user;
      if (req.session.returnTo) {
        res.redirect(req.session.returnTo);
        delete req.session.returnTo;
        return;
      }
    }
    if (req.query && req.query.redirect) {
      res.redirect(req.query.redirect);
      return;
    }
    res.end();
  });
  lockit.on('logout', function() {
    var req = res.req;
    var returnTo;
    if (req.session && req.session.returnTo) {
      res.redirect(req.session.returnTo);
      delete req.session.returnTo;
      return;
    }
    if (req.query && req.query.redirect) {
      res.redirect(req.query.redirect);
      return;
    }
    res.end();
  });
  router.use(lockit.router);
  exports.lockit = lockit;
}

if (settings.lockitRest) {
  settings.lockitRest.rest = true;
  var lockitRest = new Lockit(settings.lockitRest);
  lockitRest.config.login.handleResponse = false;
  lockitRest.on('signup', onSignup);
  lockitRest.on('login', function(user, res) {
    var req = res.req;
    if (req.session) {
      req.session.user = user;
    }
    res.status(200).json({
      id: user.id,
      name: user.name,
      email: user.email
    });
  });
  lockitRest.on('logout', function(user, res) {
    res.status(204).end();
  });
  router.use(lockitRest.router);
  exports.lockitRest = lockitRest;
}

function onSignup(user, res) {
  user.id = utils.uid(24);
  lockit.adapter.update(user, function(err) {
    if (err) logger.error('Save user error', err);
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants