I’m using the express-basic-auth library to protect a route in my Express.js application. I want to ensure that the user’s credentials are not stored in the browser’s cache. Here’s the code snippet I’m using:
const express = require('express');
const basicAuth = require('express-basic-auth');
const path = require('path');
const logger = require('./logger');
const app = express();
// Custom authorizer function
function myAuthorizer(username, password) {
try {
const userMatches = basicAuth.safeCompare(username, 'admin');
const passwordMatches = basicAuth.safeCompare(password, 'amin');
return userMatches && passwordMatches;
} catch (error) {
logger.error('myAuthorizer', error);
}
}
// Applying basic auth to the /readme route
app.use(
`/${routesPrefix}/v1/readme`,
basicAuth({
authorizer: myAuthorizer,
challenge: true
}),
express.static(path.join(__dirname, '/readme'))
);
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Question :-
1.How long are credentials typically stored in a browser’s cache when using HTTP Basic Authentication?
2. Is there a way to prevent the browser from caching these credentials using the express-basic-auth library or any other method?
I understand that the browser usually caches these credentials for the duration of the session, but I want to make sure the credentials are not cached at all. Any advice or alternative approaches are welcome.
Thank you!
Jaskaran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.