Authentication on laravel echo server

  laravel, laravel-echo, php, redis, socket.io

I’m working on a Laravel backend application that manages real-time work, the backend architecture is described like this:

Backend architecture

So users can post a message via Laravel API then Redis processed it in queue work, create an event and publish it. After that Laravel-echo-server read events and broadcast on the appropriate channel to the client. This use case works fine but I need to create a currently authenticated user before that the clients listen to events on a private channel, so in the AppServiceProvider.php file we have the code in the boot() function:

AppServiceProvider.php 

<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateSupportFacadesAuth;
use AppUser;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $user = AppModelsUser::factory()->make();
        Auth::login($user);
    }
}

The problem is actually that the message model class has the following structure:

Message.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Message extends Model
{
   use HasFactory;

   protected $fillable = [
       'description',
       'receiverId',
       'senderId',
       'serviceId'    
   ];
}

and this backend doesn’t need to have a list of authenticated users in its database. It communicates with another Laravel module (we will call it user management module) to have information about users so when a post request is sent about the message, I just verify that senderId, receiverId and serviceId attributes in the message JSON object exist in the user management module.

So what can be a good way to create a currently authenticated user without having tables of authenticated users in the database of our backend ( Because that information is already in the database of a user management module)?

Source: Laravel

Leave a Reply