alexteie's avatar

livewire auth::user()

Why is my Auth::user() NULL ?

in the view its normal


namespace BASE\Livewire\Settings\Users;

use Illuminate\Support\Facades\Auth;
use Livewire\WithPagination;
use Livewire\Component;

use BASE\Tenant;
use BASE\Lbzuser;

class Show extends Component
{

	use WithPagination;

	protected $paginationTheme = 'bootstrap';

	public $Tenants;
	
	public function mount() {
		$this->Tenants = \BASE\Tenant::all();
		$this->Users = \BASE\Lbzuser::orderBy('id', 'desc')->paginate(10);
	}

	public function impersonate($userId) {
		//$originalId = Auth::user()->id;
		//$originalId = auth()->user()->id;

		dd(Auth::user());

		session()->put('impersonate', $originalId);
		
		//auth()->guard('lbenz')->loginUsingId($userId);

		//return redirect('/_erp');
	}

    public function render()
    {
        return view('base::livewire.settings.users.show', ['Users' => $this->Users]);
    }
}
0 likes
14 replies
Sinnbeck's avatar

How are you calling the component? Inside blade or from a route?

Can you show the route?

Sinnbeck's avatar

And the route? Is it inside the auth middleware?

If not, use the request instead

request()->user();
alexteie's avatar

yes the route is in the auth middle ware because in de header.blade it works but in the livewire compent its empty

alexteie's avatar

i get this

use Illuminate\Http\Request;

$request()->user();

Undefined variable: request

< GDB >'s avatar

@snapey Sorry for that, I thought it was necessary to reference to the auth.php file, guess i was wrong, thnx to point that out

alexteie's avatar

@snapey

when i ise public $Users;

i get this

Livewire component's [b-a-s-e.livewire.settings.users.show] public property [Users] must be of type: [numeric, string, array, null, or boolean]. Only protected or private properties can be set as other types because JavaScript doesn't need to access them.

and thats correct because i passin the blade tform an controller he $Users as an collection ;)

  return view('base::settings.users.index', [
        'Tenants'   =>  $Tenants,
        'Users' => $Users
    ]);
Snapey's avatar

You cannot have a public property of a model collection

Get the models you need in the render method.

namespace BASE\Livewire\Settings\Users;

use Illuminate\Support\Facades\Auth;
use Livewire\WithPagination;
use Livewire\Component;

use BASE\Tenant;
use BASE\Lbzuser;

class Show extends Component
{

    use WithPagination;

    protected $paginationTheme = 'bootstrap';

    public function impersonate($userId)
    {
        session()->put('impersonate', request()->user()->id);	
    }

    public function render()
    {
        return view('base::livewire.settings.users.show')
            ->withUsers(\BASE\Lbzuser::orderBy('id', 'desc')->paginate(10));
    }
}

ps, save Titlecase for class names, eg $users not $Users

Snapey's avatar

For impersonating users, best approach is to store the id of the user you want to impersonate, not the id of the person who is logged in.

Then you override the logged in user with the impersonated user via middleware and then when done, clear the impersonation from session. With that in mind, I would change to;

session()->put('impersonate', $userId);	

middleware;

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Impersonate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(session()->has('impersonate')){
            Auth::onceUsingID(session('impersonate'));
        }

        return $next($request);
    }
}

Please or to participate in this conversation.