2

I've created a multi auth test in Laravel 5.4. It's using custom middlewares, custom Eloquent providers, etc. The auth flow is working, I can login in both ways. But if the user is signed in, in the home controller when I want to check the user with Auth::user() or Auth::guard()->user(), it's empty. The Auth::guard() is empty as well. But I don't understand, why?! It should contains the signed in user instance, shouldn't it?

Also the $request->getUserResolver() says that the guard is null... o.O

What did I do wrong?

Here it is my test repo, if you want to check my code.

Thank you in advance!

Edit 1:

In the \app\Http\Controllers\Employee\HomeController.php the Auth::guard()->user() and the Auth::user() are empty.

namespace App\Http\Controllers\Employee;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{

    public function __construct(Request $request)
    {
        $this->middleware('auth.employee:employee');
    }

    public function index(Request $request)
    {
        $users[] = Auth::user();
        $users[] = Auth::guard()->user();
        $users[] = Auth::guard('employee')->user();

        dd($users);

        return view('employees.home.index');
    }
}
5
  • Showing the relevant code here is a pretty good idea. Commented Feb 3, 2017 at 16:44
  • 1
    Last time I saw a post with a person just putting a link to their GitHub repo, GitHub was down for about an hour and the question got downvoted. True story. Commented Feb 3, 2017 at 16:46
  • @Chay22 Do you want me to explain all steps (with code snipets) in my question how I implemented the multi auth? Commented Feb 3, 2017 at 16:52
  • That's a good idea. Anyways, how about adding an 's' Auth::guard('employees')->user(); And before jumping into that you have to make sure that your guard is reach out login method inside the Illuminate\Auth\SessionGuard class to populate the session. What I mean was how'd you authenticate the user at the first place? Commented Feb 3, 2017 at 16:58
  • Lurking into your repo, I think the issue comes from the way you register provider Auth::provider('eloquent.employee' ...) you filled out with the driver name which should be Auth::provider('employees' ...) And, doesn't it need to be loaded just before AuthServiceProvider resolved, like registering the provider inside AppServiceProvider instead? Dunno... Commented Feb 3, 2017 at 17:09

2 Answers 2

4

Auth::shouldUse(your_guard_name);
call this in your login function

Sign up to request clarification or add additional context in comments.

1 Comment

This also can be called in the constructor of the controller that is going to use the guard
0

Change the driver name in the config/auth.php as..

'employees' => [
        'driver' => 'eloquent',
        'model' => App\Models\UserEmployee::class,
    ],
    'customers' => [
        'driver' => 'eloquent',
        'model' => App\Models\Customer::class,
    ],

1 Comment

Why should I do that? That's my custom driver with custom conditions for auth.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.