简体   繁体   中英

How do I get which user sent a request in Laravel 5.6?

I have two login panels. One is for admin and one is for vendor. Both are using one common file for one of the calculations.

My problem is, if both users log in at the same time in the same browser, then how can I identify which user sent a request to me for calculation?

I am already using multiAuth of Laravel and it is working very well.

To identify which user is logged in I am using below code.

if (Auth::guard('admin')->check()) {
  // Calculations
}elseif(Auth::guard('merchant')->check()){
  // Calculations
}

But when both users log in on the same browser then it will always send me into the first condition.

So is there any way that I can get which Auth is sending the request to me? So based on that I will manage my calculation?

You can check your route middlewares to detect which guard is currently being used in authentication.

Example

$middlewares = request()->route()->gatherMiddleware();

foreach ($middlewares as $m) {
    if (preg_match("/auth:/", $m)) {
        list($mid, $guard) = explode(":", $m);
    }
}

The $guard variable will host your current guard name. Use the same in your if statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM