Skip to content

Instantly share code, notes, and snippets.

@leozc
Created November 30, 2022 07:31
Show Gist options
  • Save leozc/e604019d079d2c519b8e4a3a1916b36f to your computer and use it in GitHub Desktop.
Save leozc/e604019d079d2c519b8e4a3a1916b36f to your computer and use it in GitHub Desktop.
//express basic authentication example
const app = require('express')()
const basicAuth = require('express-basic-auth')
app.use(basicAuth({
users: { 'admin': '123456' },
challenge: true,
realm: 'xxxx',
unauthorizedResponse: getUnauthorizedResponse
}))
app.get("/url", (req, res, next) => {
res.json(["Tony", "Lisa", "Michael", "Ginger", "Food"]);
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
function getUnauthorizedResponse(req) {
return req.auth
? ('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected')
: 'GO AWAY KAYDAN No credentials provided'
}
{
"name": "express_server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"express-basic-auth": "^1.2.1",
"express-generator": "^4.14.1",
"install": "^0.13.0",
"npm": "^9.1.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment