Comments (5)
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.
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.
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.
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.
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)
- grunt server fails on Windows 7 HOT 6
- Failed to install grunt-contrib-handlebars HOT 2
- ReferenceError: _ is not defined HOT 1
- __layout.hbs HOT 1
- does not run on Windows
- Template precompile HOT 3
- initialize model returns error HOT 3
- Some thoughts on the README HOT 1
- clientSync is used on a request when trying to create the server different from the index.js file HOT 2
- Handlebars precompiled versioning error HOT 1
- Broken links in README HOT 1
- apiProxy should handle status code 204 properly HOT 1
- Server side call
- Error on startup HOT 3
- Error: npm install rendr-app-template HOT 1
- Modularization of the site HOT 5
- npm install errors (ubuntu 12.04) HOT 3
- Need to add "npm install" step to README HOT 1
- Unable to start server in Windows 7 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rendr-app-template.