Auth::user() returns null

I faced a situation where Auth::user() always returns null, it was because I was trying to get the User in a controller’s constructor. I realized that you can’t access the authenticated user in your controller’s constructor because the middleware has not run yet. As an alternative, you can define a Closure based middleware directly in … Read more

Exclude route from express middleware

Even though there is no build-in middleware filter system in expressjs, you can achieve this in at least two ways. First method is to mount all middlewares that you want to skip to a regular expression path than includes a negative lookup: // Skip all middleware except rateLimiter and proxy when route is /example_route app.use(/\/((?!example_route).)*/, … Read more

What is the difference between ‘session’ and ‘cookieSession’ middleware in Connect/Express?

The session middleware implements generic session functionality with in-memory storage by default. It allows you to specify other storage formats, though. The cookieSession middleware, on the other hand, implements cookie-backed storage (that is, the entire session is serialized to the cookie, rather than just a session key. It should really only be used when session … Read more

ASP.NET MVC 6 AspNet.Session Errors – Unable to resolve service for type?

Unable to resolve service for type ‘Microsoft.AspNetCore.Session.ISessionStore’ while attempting to activate ‘Microsoft.AspNetCore.Session.SessionMiddleware’ If you get this error message in ASP.NET Core, you need to configure the session services in Startup.cs: public void ConfigureServices(IServiceCollection services) { services.AddMvc() .AddSessionStateTempDataProvider(); services.AddSession(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseSession(); app.UseMvcWithDefaultRoute(); }

Chaining multiple pieces of middleware for specific route in ExpressJS

Consider following example: const middleware = { requireAuthentication: function(req, res, next) { console.log(‘private route list!’); next(); }, logger: function(req, res, next) { console.log(‘Original request hit : ‘+req.originalUrl); next(); } } Now you can add multiple middleware using the following code: app.get(“https://stackoverflow.com/”, [middleware.requireAuthentication, middleware.logger], function(req, res) { res.send(‘Hello!’); }); So, from the above piece of code, … Read more