Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmcglone's avatar

Wow. This is crazy. I cleared my browser cache, still able to log in. I changed the route to your suggestion and it still logs in. doesn't even echo the message out.

mstnorris's avatar
  1. Then there must be something wrong in the way you handle the check to see if they are verified.
  2. Or, they are actually verified.

What is currently in your database? Post a screen grab.

dmcglone's avatar

took a screen of all 3 of my monitors, so I had to edit it and make sure it was viewable, now I can't figure out where to upload it.

mstnorris's avatar

There is your problem. You need to add the $casts property to your User model.

protected $casts = [
    'is_admin' => 'boolean',
    'is_employee' => 'boolean',
];
dmcglone's avatar

I tried your suggestion, but it's still logging in. LOL

dmcglone's avatar

Ah hah. I logs me out if I try to go to the employee page, but it does let me log in. but logs me out when I hit the employee route

dmcglone's avatar

I forgot a couple times, because it was logging me out right after logging in and wouldn't even let me log in at all.

dmcglone's avatar

but after I experimented with the other suggestion, somehow it either changed or fixed itself.

mstnorris's avatar

I think you were checking properly ;)

But happy it is sorted.

dmcglone's avatar

Yeah, hitting the employee route does automatically log me out. I was hoping for it to not let a login at all, but what I could do is adjust the login to hit the employee route instead of the home route and it should automatically log the user out..

dmcglone's avatar

I can't thank you enough. I have learned something good today. :-)

dmcglone's avatar

@mstnorris check out this lil hack I did. Now when logging in, if your admin bit is true, it redirects to /admin, and if your employee bit is true it redirects to /employee, otherwise it redirects back to auth/login. I know it's not as practical as using a role based system, but if one only needs a couple roles, why not. :-)

AuthenticatesAndRegistersUsers

public function getLogout()
   {
       $this->auth->logout();
       return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/auth/login');
   }
   public function redirectPath()
   {
       if (property_exists($this, 'redirectPath'))
       {
           $this->redirectPath;
       }

       if ($this->auth->user()->is_admin == true)
       {
           return property_exists($this, 'redirectTo') ? $this->redirectTo : '/admin';
       } else {
           return property_exists($this, 'redirectTo') ? $this->redirectTo : '/employee';
       }
   }
mstnorris's avatar

@dmcglone don't edit the AuthenticatesAndRegistersUsers.php file directly as it is part of the Laravel core framework and it's under the _Illuminate\Foundation\Auth_ namespace.

Instead, just put that same method inside AuthController.php which is in your app/Http/Controllers/Auth directory and it will work the same, but it won't be overridden when you update Laravel in the future. The reason why this works is because AuthController uses the AuthenticatesAndRegistersUsers Trait.

<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers;

    public function getLogout() {
        $this->auth->logout();
        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/auth/login');
    }

    public function redirectPath() {
        if ( property_exists($this, 'redirectPath') )
    {
        $this->redirectPath;
    }

    if ( $this->auth->user()->is_admin == true ) {
        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/admin';
    } else {
        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/employee';
    }
}
dmcglone's avatar

Ah Ok. I just skimmed back through my posts because this had come up a while back and others had told me to do it this way also, but I never did figure out why, so this clears it up.

dmcglone's avatar

@mstnorris I was playing around more with this code and thought I'd post this in case others come along and have the same problem. Remember in our discussion this worked for me:

public function handle($request, Closure $next)
    {
        if ($this->auth->check() && $this->auth->user()->is_employee == true)
        {
             return $next($request);
        }
        return new RedirectResponse(url('auth/logout'));
    }

But this didn't:

public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->isEmployee())
        {
            return $next($request);
        }
        return new RedirectResponse(url('auth/logout'));
    }

Turns out I forgot to include the Auth namespace:

use Illuminate\Support\Facades\Auth;
mstnorris's avatar

@dmcglone you can put use Auth; instead. You don't need to include the whole namespace.

Also on that note:

  • (a) I'm sure I mentioned that, and if I didn't, I'm sure others would have.
  • (b) You should be using an IDE like PhpStorm that will give you a warning when you don't import classes.

Check out Jeffrey's series Be Awesome in PHPStorm.

dmcglone's avatar

I'm using PhpStorm, but didn't get a warning or squiggly lines or greyed out lines, so that's weird.

And yeah just using Auth works. :-)

Time22's avatar

Restricting admin not to access user dashboard .

Previous

Please or to participate in this conversation.