Giter Site home page Giter Site logo

Comments (5)

spikebrehm avatar spikebrehm commented on June 11, 2024

This is the perfect use case for Express middleware. Remember, a Rendr app is simply an Express app.

// server/middleware/checkCurrentUser.js
var superagent = require('superagent');

module.exports = function(req, res, next) {
  // You can always access `app` from the `req` as `rendrApp`.
  var app = req.rendrApp;

  var url = 'http://localhost:5000/api/authentication/is_logged_in';
  var http = superagent.agent();
  var handler = http.get(url);
  var request = handler.set('Cookie', req.headers.cookie);
  request.end(function(res) {
    var data = JSON.parse(res.text);
    // Set 'logged_in' on the app, which you can access in __layout.hbs as `_app`.
    app.set('logged_in', data.logged_in);
    next();
  });
};

__layout.hbs:

...
{{#if _app.attributes.logged_in}}
  ...
{{else}}
  ...
{{/if}}
...

But it makes me realize it would be good to expose app so you can do something like app.logged_in instead of _app.attributes.logged_in.

from rendr-app-template.

adeleinr avatar adeleinr commented on June 11, 2024

req.rendrApp is not defined when the checkCurrentUser middleware get executed, I'm adding the checkCurrentUser middleware after the errorHandler middleware.

// set the middleware stack
app.use(express.compress());
app.use(express.static(__dirname + '/../public'));
app.use(express.logger());
app.use(express.bodyParser());
app.use(app.router);
app.use(mw.errorHandler());
app.use(mw.checkCurrentUser());

Where should I include the checkCurrentUser middleware?

from rendr-app-template.

spikebrehm avatar spikebrehm commented on June 11, 2024

Sorry for not being more clear -- notice lower in the file the part about preRendrMiddleware? These are middleware that just run before Rendr routes & API calls. Here you'll see the initApp middleware:

// Insert these methods before Rendr method chain for all routes, plus API.
var preRendrMiddleware = [
  // Initialize Rendr app, and pass in any config as app attributes.
  rendrMw.initApp(env.current.rendrApp)
];

function buildApiRoutes(app) {
  var fnChain = preRendrMiddleware.concat(rendrMw.apiProxy());
  fnChain.forEach(function(fn) {
    app.use('/api', fn);
  });
}

function buildRendrRoutes(app) {
  var routes, path, definition, fnChain;
  // attach Rendr routes to our Express app.
  routes = rendrServer.router.buildRoutes();
  routes.forEach(function(args) {
    path = args.shift();
    definition = args.shift();

    // Additional arguments are more handlers.
    fnChain = preRendrMiddleware.concat(args);

    // Have to add error handler AFTER all other handlers.
    fnChain.push(mw.errorHandler());

    // Attach the route to the Express server.
    app.get(path, fnChain);
  });
}

If you add your mw.checkCurrentUser() middleware after initApp, then you can access req.rendrApp:

var preRendrMiddleware = [
  rendrMw.initApp(env.current.rendrApp),
  mw.checkCurrentUser
];

If you want to access your user information in other, non-Rendr parts of your Express app, you'll want to do it slightly differently -- probably adding the information you want as a property on req, let's say req.user, and then you would add a little middleware in the preRendrMiddleware stack to pull the information from req.user and set it on req.rendrApp.set('user', req.user) so it's available in the client-side. Does that make sense?

from rendr-app-template.

adeleinr avatar adeleinr commented on June 11, 2024

Ah, thanks for clarifying that. It does work. Actually I had tried that because I saw that initApp was adding the app to req.rendrApp but it was failing because of my mistake in another part of the code! So this works for hard page refreshes but not for client (purely ajax calls) from view to view since _layout.hbs only gets refreshed on page reload. I will try the other trick of adding the req.user to see if I can get it to work on the client side as well. Thanks so much!!

from rendr-app-template.

adeleinr avatar adeleinr commented on June 11, 2024

One more question, {{_app.attributes.logged_in}} is exposed in the views, but it doesn't get updated even though the requests are passing though the checkCurrentUser middleware and req.rendrApp.set('logged_in', data.logged_in) is being set again, are you exposing this variable differently in the view?

from rendr-app-template.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.