1

I've built an app where I have two different models with login functionality. People can either login like normal as a User or they can login as an Employer. Here's the Employer model:

<?php

namespace App;
...

class Employer extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;
    use Billable;

    protected $guard = 'employer';

    /**
     * @var array
     */
    protected $fillable = [
        'email', 'password'
    ];

}

I'd like to implement the MustVerifyEmail interface but it uses some middleware called EnsureEmailIsVerified that checks if the user has a verified email.

    if (! $request->user() ||
        ($request->user() instanceof MustVerifyEmail &&
         ! $request->user()->hasVerifiedEmail())) {
       return $request->expectsJson()
            ? abort(403, 'Your email address is not verified.')
            : Redirect::route('verification.notice');
    }

I need to check if the Employer is verified with some middleware that I make. How would I implement $request->employer() so that I could get the authenticated employer instead of the authenticated user?

Connor Leech
  • 18,052
  • 30
  • 105
  • 150
  • You can use another guard. I believe the `user` method of the `$request` takes in one argument. So `$request->user('employer')` – Adam Rodriguez Nov 05 '18 at 18:58
  • Does that go in the AuthServiceProvider boot method? – Connor Leech Nov 05 '18 at 19:03
  • It can go wherever you need it. As long as you have an authenticated user at the time. It might be easier to just use the middleware: `Route::get('profile',function () {})->middleware('verified');` – Adam Rodriguez Nov 05 '18 at 19:08
  • @ConnorLeech Have you thought about adding a custom middleware for authentication on login route? I.e. Route::get('/login', 'CustomeMethod')->middleware('CUSTOM_MIDLLEWARE'); – Rutvij Kothari Nov 05 '18 at 19:15
  • @RutvijKothari I've built the ability for employers to login. The middleware needs to check from the request if an employer is logged in. The question is how do I check the request in the middleware to see if an employer is logged in? – Connor Leech Nov 05 '18 at 19:22
  • 2
    @ConnorLeech I think I understand your issue now. Maybe just extend the original `EnsureEmailIsVerified` then swap out the middleware in app/Http/Kernel.php `'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class` for yours. Or create two middleware one for a regular user and one for an employer. In the derived or second middleware use `$request->user('employer')` – Adam Rodriguez Nov 05 '18 at 19:31

0 Answers0