Gabotronix
4 weeks ago
121
2
General

Issue with sending data from my backend to frontend after redirect

Posted 4 weeks ago by Gabotronix

Hi everybody, I have an auth middleware which redirects back to home page if you are not logged in, once the user is redirected to home page I want to open a modal for him to log in via checking if showLoginModal is true or something like that, currently in order to send data from my laravel backend to my vue frontend I created a singleton from my Javascript helper class, this has been working all right so far but when redirected I can't access my backend variables in my vue components.

How would you guys go about this?

Here is my Middleware:

<?php
namespace App\Http\Middleware;


use Closure;
use Illuminate\Http\Request;
use App\Helpers\Javascript;
use Auth;



class AuthAdmin
{


    public function handle($request, Closure $next)
    {
        $user = Auth::user();

        if($user && $user['privileges'] > 2)
        {
            return $next($request);   
        }

        app(Javascript::class)->put(['showLoginModal' => true]);

        return redirect('/');
        
    }
}

And my Javascript helper class:

<?php
namespace App\Helpers;


use Illuminate\Http\Request;


class Javascript
{
    public $app = [];

    public function put($array)
    {
        $this->app = array_merge($this->app, $array);
    }
  
}

In my blade master layout I have something like this:

<script type="text/javascript">window.$app = {!! json_encode(app(App\Helpers\Javascript::class)->app) !!};</script>

Please sign in or create an account to participate in this conversation.