I'm having trouble sending requests using custom headers from my application so that I can use them for authentication on my server.
I have a built-in response application that uses firebase validation and a node / express backend server.
When the user writes, I will take their firebase idtoken and save it.
Then I will send this idtoken as a header whenever I make an axiom request to the 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);
, , idtoken, , . , next() . , 401.
app.js:
const app = express();
app.use(bodyParser.json());
app.use(firebaseAuthentication);
router(app);
firebaseAuthentication:
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 be signed in via firebase' });
}
};
:
module.exports = app => {
app.get('/api/users/:firebaseUID', UserController.getUser);
}
, , , , ... 404 Error: Can't set headers after they are sent
, :
async getUser(req, res, next) {
const firebaseUID = req.params.firebaseUID;
try {
const user = await User.findOne({ firebaseUID });
res.send(user);
} catch (e) {
next(e);
}
},
? !