This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
seepur/app/Middleware/WsCallAuth.js

34 lines
939 B
JavaScript
Raw Normal View History

2020-04-12 14:25:42 +00:00
'use strict'
/** @typedef {import('@adonisjs/framework/src/Request')} Request */
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
/** @typedef {import('@adonisjs/framework/src/View')} View */
const Call = use('App/Models/Call');
class WsCallAuth {
/**
* @param {object} ctx
* @param {Request} ctx.request
* @param {Function} next
*/
async wsHandle(ctx, next) {
const {request, auth, socket} = ctx;
const callId = Number(socket.topic.split(':')[1]);
const user = auth.user;
const call = await Call.find(callId);
if (!call) {
throw new Error('Call not found');
}
2020-04-12 23:33:24 +00:00
if (call.state === 'ENDED') throw new Error('This call has ended');
if (user.id === call.parent_id || user.id === call.guest_id) {
2020-04-12 14:25:42 +00:00
ctx.call = call;
await next()
}
// call next to advance the request
else
throw new Error('Not allowed');
}
}
module.exports = WsCallAuth