v1.3.0-beta/start of voice call and custom code:
This commit is contained in:
@@ -6,6 +6,8 @@ const app = express();
|
||||
app.use(cors({ origin: true }));
|
||||
app.use(FunctionModules.authenticator);
|
||||
|
||||
app.get('/validator', FunctionModules.tokenValidator);
|
||||
app.get('/refresher/:token', FunctionModules.tokenRefresher);
|
||||
app.post('/create', FunctionModules.createRoom);
|
||||
app.post('/join', FunctionModules.joinRoom);
|
||||
app.post('/message', FunctionModules.sendMessage);
|
||||
|
||||
@@ -4,31 +4,37 @@ module.exports = (req, res, next) => {
|
||||
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;
|
||||
if (req.headers.authorization) {
|
||||
try {
|
||||
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 });
|
||||
});
|
||||
} catch (err) {
|
||||
return res.status(401).send({ message: 'Got no user token', ...err });
|
||||
}
|
||||
}
|
||||
return res.status(401).send({ message: 'Got no user token' });
|
||||
};
|
||||
|
||||
@@ -4,16 +4,24 @@ module.exports = async (req, res) => {
|
||||
const bodydata = JSON.parse(req.body);
|
||||
console.log('Requested Create - UID - ', bodydata.uid);
|
||||
if (!bodydata.userName) res.status(400).send({ message: 'Missing user name' });
|
||||
const roomId = await admin
|
||||
if (!bodydata.roomName) res.status(400).send({ message: 'Missing party name' });
|
||||
const roomNameTaken = await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.add({
|
||||
.doc(bodydata.roomName)
|
||||
.get()
|
||||
.then((docSnap) => docSnap.exists);
|
||||
if (roomNameTaken) return res.status(409).send({ message: 'That party name is already taken' });
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(bodydata.roomName)
|
||||
.set({
|
||||
name: bodydata.roomName,
|
||||
player: bodydata.player,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.then((ref) => ref.id)
|
||||
.catch((err) => {
|
||||
res.status(500).send({ message: 'Unable to start party.' });
|
||||
console.error(err);
|
||||
@@ -21,7 +29,7 @@ module.exports = async (req, res) => {
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(roomId)
|
||||
.doc(bodydata.roomName)
|
||||
.collection('messages')
|
||||
.add({
|
||||
uid: '<system>',
|
||||
@@ -30,12 +38,15 @@ module.exports = async (req, res) => {
|
||||
time: new Date(),
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`Failed to add system create party message : \n\t Room: ${roomId}`, err);
|
||||
console.error(
|
||||
`Failed to add system create party message : \n\t Room: ${bodydata.roomName}`,
|
||||
err
|
||||
);
|
||||
});
|
||||
await admin
|
||||
.firestore()
|
||||
.collection('rooms')
|
||||
.doc(roomId)
|
||||
.doc(bodydata.roomName)
|
||||
.collection('users')
|
||||
.doc(bodydata.uid)
|
||||
.set({
|
||||
@@ -44,9 +55,9 @@ module.exports = async (req, res) => {
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(
|
||||
`Failed to set admin when creating party : \n\t Room: ${roomId} \n\t User: ${bodydata.uid}`,
|
||||
`Failed to set admin when creating party : \n\t Room: ${bodydata.roomName} \n\t User: ${bodydata.uid}`,
|
||||
err
|
||||
);
|
||||
});
|
||||
res.status(201).send({ roomId, message: 'Started a party!' });
|
||||
res.status(201).send({ roomId: bodydata.roomName, message: 'Started a party!' });
|
||||
};
|
||||
|
||||
@@ -8,6 +8,8 @@ const leaveRoom = require('./leaveRoom');
|
||||
const deleteRoom = require('./deleteRoom');
|
||||
const timeDiff = require('./timeDiff');
|
||||
const updatePlayer = require('./updatePlayer');
|
||||
const tokenValidator = require('./tokenValidator');
|
||||
const tokenRefresher = require('./tokenRefresher');
|
||||
|
||||
module.exports = {
|
||||
authenticator,
|
||||
@@ -20,4 +22,6 @@ module.exports = {
|
||||
scheduledCleanUp,
|
||||
cleanUpRoom,
|
||||
timeDiff,
|
||||
tokenValidator,
|
||||
tokenRefresher,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
const VoxeetSecret = require('../VoxeetSecret.json');
|
||||
module.exports = async (req, res) => {
|
||||
const { key, secret } = VoxeetSecret;
|
||||
const credentials = btoa(encodeURI(key) + ':' + encodeURI(secret));
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-cache',
|
||||
Authorization: 'Basic ' + credentials,
|
||||
};
|
||||
const oauth = await fetch(VoxeetSecret.refreshURL, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
refresh_token: req.params.token,
|
||||
}),
|
||||
})
|
||||
.then((oauthResponse) => oauthResponse.json())
|
||||
.catch((err) => res.status(500).send(err));
|
||||
if (oauth) res.status(200).send(oauth);
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
const VoxeetSecret = require('../VoxeetSecret.json');
|
||||
module.exports = async (req, res) => {
|
||||
const { key, secret } = VoxeetSecret;
|
||||
const credentials = btoa(encodeURI(key) + ':' + encodeURI(secret));
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-cache',
|
||||
Authorization: 'Basic ' + credentials,
|
||||
};
|
||||
const oauth = await fetch(VoxeetSecret.validateURL, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
grant_type: 'client_credentials',
|
||||
}),
|
||||
})
|
||||
.then((oauthResponse) => oauthResponse.json())
|
||||
.catch((err) => res.status(500).send(err));
|
||||
if (oauth) res.status(200).send(oauth);
|
||||
};
|
||||
Reference in New Issue
Block a user