javascript - Can't set headers after they are sent...no issue until I added in authentication? -


i'm having issues trying send axios requests custom headers app can use them authentication on server.

i have react native app uses firebase authentication , node/express backend server.

when user signs in, grab firebase idtoken , store it.

i send idtoken header whenever make axios request server.

 const { currentuser } = firebase.auth();    const url = ${serverurl}/api/users/${user.uid`;    const idtoken = await currentuser.gettoken(true);      var config = {       headers: { idtoken },     };  const response = await axios(url, config); 

in server code, there express middleware checks whether idtoken sent in request header valid or not. if is, call next() , proceed execute logic. if not, it'll fail , send 401 status.

this in app.js:

const app = express();     app.use(bodyparser.json());    app.use(firebaseauthentication);   router(app); 

this firebaseauthentication middleware:

const firebaseauthentication = async (req, res, next) => {   console.log(req);   try {     const { idtoken } = req.headers;      await admin.auth().verifyidtoken(idtoken);     next();   } catch (e) {     res.status(401).send({ error: 'unauthorized request. must signed in via firebase' });   } }; 

this router:

module.exports = app => {      app.get('/api/users/:firebaseuid', usercontroller.getuser); } 

the issue seems go through until tries send data user...but 404 , error: can't set headers after sent

this specific request failing:

  async getuser(req, res, next) {     const firebaseuid = req.params.firebaseuid;     try {       const user = await user.findone({ firebaseuid });        res.send(user);     } catch (e) {       next(e);     }   }, 

what happening here , how can fix it? thanks!


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -