There are a few ways to achieve this.
The simplest probably is, not to attach the authChecker
globally, but with each request. This of course, bears the risk, of a dev, forgetting to include the authChecker in a request that should be protected.
app.get("/nologin", nologinhandler);
app.get("/withlogin", authChecker, withloginhandler);
Another way exploits the fact, that the order in which you attach routes and middlewares to your app
is important. Ie, if you do something like this
app.get("/nologin", nologinhanlder);
app.get("/req1", ...);
app.get("/req2", ...);
//all requests above this line won't require authenication
app.use(authChecker);
//all requests following this line will require authentication
app.get("/withlogin", withloginhandler);
the all routes created before the app.use(authChecker);
will in fact not go through the authChecker
middleware, whereas all requests attached after app.use(authChecker);
will have to pass the authChecker
. Of course this is still not 100% failsafe, as devs can add new routes whereever they want. But it seems to be easier to handle than rembering to add auth to every single request. Of course, this might also lead to some problems, because the route matching in express also happens in the order the routes are added, and in some cases, similar routes have to be added in a certain order, to be correctly matched.
You could also use two differernt routers to make this even more explicit
let authrouter = express.Router();
let noauthrouter = express.Router();
authrouter.use(authChecker);
authrouter.get("/withlogin", ...);
noauthrouter.get("/nologin", ...);
app.use(noauthrouter);
app.use(authrouter);
So it's clear, that when a request is attached to the noauth
router, it doesn't require authentication, whereas when it's attachted to the auth
router, it does. Again, it's important here, to attach the noauthrouter
to the app first ...
A third possibility is of course, checking the path within the authChecker
. Ie something like this
function canAnonymous(path) {
if (path === "/nologin")
return true;
//any other checks for other paths
...
return false;
}
function authChecker(req, res, next) {
if (canAnonymous(req.path))
return next();
//do default authcheck
...
}
app.use(authChecker);
app.get("/nologin", nologinhandler);
app.get("/withlogin", withloginhandler);
This way, all requests will go through the authChecker
, and without being added as an exception, the will also need authentication. Ie making them accessible for anonymous requires additional work. Of course, matching such path can be complicated if you for instance have path parameters or optional parts.