35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
module.exports = (req, res, next) => {
|
|
res.set('Access-Control-Allow-Origin', ['*']);
|
|
if (req.method === 'OPTIONS') {
|
|
res.status(200).send();
|
|
return next();
|
|
}
|
|
const token = req.headers.authorization.split(' ')[1];
|
|
const reqToken = token.split(':')[0];
|
|
const reqUid = token.split(':')[1];
|
|
admin
|
|
.auth()
|
|
.verifyIdToken(reqToken)
|
|
.then((user) => {
|
|
if (user.uid === reqUid) {
|
|
console.info('Verified Token - UID - ', reqUid);
|
|
if (req.method === 'POST') {
|
|
const bodydata = JSON.parse(req.body);
|
|
bodydata.uid = user.uid;
|
|
req.body = JSON.stringify(bodydata);
|
|
}
|
|
if (req.method === 'GET' || req.method === 'DELETE') {
|
|
req.body = JSON.stringify({ uid: user.uid });
|
|
}
|
|
return next();
|
|
} else {
|
|
return res.status(401).send({ message: 'Unauthorized' });
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.error('Failed Auth - ', req.headers.authorization, '\n', err);
|
|
return res.status(500).send({ message: 'Unable to verify user token', ...err });
|
|
});
|
|
return null;
|
|
};
|