Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using express-basic-auth with router ? #36

Open
Cliff-R-K opened this issue Oct 5, 2020 · 3 comments
Open

Using express-basic-auth with router ? #36

Cliff-R-K opened this issue Oct 5, 2020 · 3 comments

Comments

@Cliff-R-K
Copy link

I can't figure out how to use express-basic-auth correctly. If I make a POST-request to "/abort" with correct authorization everything seams to work correctly. But if I enter the wrong credentials in the header I get the correct "Credentials rejected" message.
But it still triggers the "/abort" endpoint and also gives med console.log outputs and 200 message:sucess

What am I missing ?
App.js

const getUnauthorizedResponse = (req) => {
  return req.auth
    ? `Credentials ${req.auth.user} : ${req.auth.password} rejected`
    : "No credentials provided";
};

app.use(
  basicAuth({
    users:  {"user":"password"} ,
    unauthorizedResponse: getUnauthorizedResponse,
  })
);

app.get("/", (req, res) => res.send("API Running"));

app.use("/api", require("./routes/api/abort").router);

abort.js

const express = require("express");
const router = express.Router();

router.post('/abort', async (req, res) => {
    try {
        const body = await req.body
        const hostname = body.hostname
        console.log(`Abort datacollection endpoint.\nHostname is ${hostname}`)
        return res.status(200).send({message:"success"})
    } catch (error) {
        console.log("error!!!")
        return res.status(404).send({message:"fail"})
    }
})

module.exports = { router };
@coffeeispower
Copy link

coffeeispower commented Feb 5, 2022

@Burton666

app.get("route", basicAuth(....), (req, res) => {.....})

@AlphaJuliettOmega
Copy link

@tiagodinis33 but if you do it that way you can't use the Request parameters to fetch the basicauth details from somewhere.

@sephentos
Copy link

sephentos commented Sep 2, 2022

It would be nice if there was a way to use the request parameter with app.get. Or at least get the req.auth boolean value provided by the middleware.

		this.app.get( `/getToken`, basicAuth( {
			users: {
				uname: 'secret',
			},
			challenge: true,
			unauthorizedResponse: function( req: Request ) 
			{
				console.log( req.auth )
			}
		} ), this.getToken )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants