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

dmcglone's avatar

lol my brain is broke, sorry about that. Once all the replies starting coming in, I kinda got a lil overwhelmed.

mstnorris's avatar

Well I'm happy we got it sorted in the end.

1 like
dmcglone's avatar

Yeah. The examples from you and the comments did get me thinking in the right direction. I owe you one. :-)

bestmomo's avatar

@dmcglone

I just wanted to say you can use the helper :

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

@bestmomo I thought that's what I had that wasn't working, but I'm going to try again, I may have made a typo.

mstnorris's avatar

@bestmomo what is the difference? Is it just syntactic sugar? Or is it more efficient?

Anyway, @dmcglone it won't make a difference to your implementation, so you're all good.

1 like
bestmomo's avatar

@mstnorris the difference is only for those that dont like facades, in both case we get an object by Illuminate\Contracts\Auth\Guard.

2 likes
mstnorris's avatar

@bestmomo I'm indifferent, but, out of curiosity, why don't people like Facades? Is it because the syntax resembles static method calls?

dmcglone's avatar

I tried it like you suggested @mstnorris and it does refuse to work. It seems like it ignores everything and just logs the user in.

mstnorris's avatar

Then (without meaning to sound rude) you haven't set it up correctly as that code is tried and tested. As I mentioned, post everything that you currently have and together we can sort it out. :D

dmcglone's avatar

@mstnorris I was going to ask the same question, not because I'm good enough to have a preference, but out of curiosity.

1 like
mstnorris's avatar

@dmcglone don't put yourself down, everyone starts somewhere. Everyone is learning all of the time (at least they should be). No one person knows everything, that's why we are all here.

You'll see, in a couple of weeks, you'll be on here and someone will ask about Middleware and protecting routes, and you'll be able to lend a hand.

2 likes
bestmomo's avatar

I think the best place to learn a lot about Laravel is to read this forum posts.

2 likes
dmcglone's avatar

Code that works: Routes

Route::group([
    'prefix' => '/',
    'namespace' => 'Employee',
    'middleware' => 'employee'
], function() {
    Route::resource('employee', 'EmployeeController');
});

middleware: EmployeeAuthentication.php

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\RedirectResponse;

class EmployeeAuthentication {

    protected $auth;

    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

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

User model:

 public function isEmployee()
    {
        return $this->is_employee ? true : false;
    }
dmcglone's avatar

Ok switching the middlewear to your suggestion earlier in the discussion and following your example, here is the EmployeeAuthentication and it seems to be ignored and follows through with login normally.

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

Can you show me your database migration for the users table.

dmcglone's avatar
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->boolean('is_admin')->default(false);
            $table->boolean('is_employee')->default(false);
               $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }

}
mstnorris's avatar

@dmcglone do you have any other routes?

What happens when you visit this route?

Route::get('protected', ['middleware' => ['auth', 'employee'], function() {
   return 'this route is protected, if you can see this then access has been granted';
}]);
dmcglone's avatar

Ok Trying @bestmomo's suggestion and slight variation of mine also allows login.

bestmomo's suggestion:

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

Slight variation of mine.

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


mstnorris's avatar

@dmcglone try sticking to just one. Now I don't know where you are up to again as you've changed it. The code I gave you works, if you had changed my implementation of admin to verified as you originally asked you'd be fine.

dmcglone's avatar

My routes are the stock stuff except the 2 I added for this exercise

Route::get('/', 'WelcomeController@index');
Route::get('home', 'HomeController@index');

Route::group([
    'prefix' => '/',
    'namespace' => 'Admin',
    'middleware' => 'admin'
], function() {
    Route::resource('admin', 'AdminController');
});

Route::group([
    'prefix' => '/',
    'namespace' => 'Employee',
    'middleware' => 'employee'
], function() {
    Route::resource('employee', 'EmployeeController');
});

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);
mstnorris's avatar

@dmcglone why are you using resources in this case. We want to get it working first, right?

Did you add the route I posted above?

dmcglone's avatar

Heh, after changing the middleware to make the replies, I changed it back to the way it was working and now it isn't working.. lol Wonder if I need to clear the cache?

Please or to participate in this conversation.