1

I'm building a real time application in Laravel with custom guards using JWT: person-api for Person and worker-api for Worker. They represent different models, not the same one with roles.

A Person may request a certain Job and a Worker is able to accept that job. I have a dedicated table for each model, jobs being the intermediary table between a Person and a Worker, with a person_id and worker_id. When a Job is requested, the worker_id column is set to null until a Worker takes it.

When a Person requests a Job an event is fired and broadcasted in a private channel called Jobs. I'm able to authorize the person in that channel by checking the person_id of the job in the event. At this moment, the job is available to any Worker until one takes it, and then only the Worker who took it is able to perform actions on that Job.

My question is, how do I achieve multiple authentication with two different guards in one same channel where the authorization method may vary depending on the state of the Job?

For example:

  • When a Job is requested, I can authorize the Person with the person_id of the job, but the worker_id is null, so it's open to any Worker
  • When a Worker takes the job, the authorization for the Person stays the same, but now only the Worker who took it is authorized in the channel
  • When a Worker drops the job, it becomes available again to any Worker

This is the code in my channels routes for broadcasting:

Broadcast::channel('job.{job}', function ($user, \App\Job $job) {
    // authorization condition goes here
});

Where $user might be a Person or a Worker, and the authorization condition might change depending on the event of the job in the same channel.

EDIT: My question was marked as a possible duplicate of this question and I disagree. I'm not just talking about authenticating users with JWT, that's implemented already. I'm talking about authenticating multiple users with different guards in one same broadcast channel and changing the authorization condition according to the type of user and status of the job.

Reque
  • 11
  • 1
  • 2
  • Could you not just do `if (auth('worker')->check())` and `if (auth('person')->check())`? – Ohgodwhy Jan 09 '18 at 19:16
  • @Ohgodwhy where exactly? I know I could write a custom middleware to override the `$user` for the Closure, but still I need to check the authorization depending on the user and "state" of the job. – Reque Jan 09 '18 at 19:32
  • You can just do it inside of the `broadcast` and return true whenever you want. – Ohgodwhy Jan 09 '18 at 20:02
  • So far I don think you can do it with predefined Boardcasting::routes(), u can write your own auth route to do that. or don use with multi-auth but role on user will do – Yu Yenkan Aug 02 '21 at 14:39

0 Answers0