seepur/app/Middleware/WsCallAuth.js
2020-04-12 10:25:42 -04:00

33 lines
862 B
JavaScript

'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');
}
if (user.id === call.user_1 || user.id === call.user_2) {
ctx.call = call;
await next()
}
// call next to advance the request
else
throw new Error('Not allowed');
}
}
module.exports = WsCallAuth