That's the first thing that I said :D
lol my brain is broke, sorry about that. Once all the replies starting coming in, I kinda got a lil overwhelmed.
Well I'm happy we got it sorted in the end.
Yeah. The examples from you and the comments did get me thinking in the right direction. I owe you one. :-)
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'));
}
@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 the difference is only for those that dont like facades, in both case we get an object by Illuminate\Contracts\Auth\Guard.
@bestmomo I'm indifferent, but, out of curiosity, why don't people like Facades? Is it because the syntax resembles static method calls?
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.
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
@mstnorris I was going to ask the same question, not because I'm good enough to have a preference, but out of curiosity.
@mstnorris I prefer link Taylor answer for facades detractors.
@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.
@bestmomo I had read that when it was published :)
Thanks. :-)
I think the best place to learn a lot about Laravel is to read this forum posts.
I read a lot @bestmomo.. I'll post my code now. I'm sure I don't need to post Kernel again?
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;
}
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'));
}
Can you show me your database migration for the users table.
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');
}
}
@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';
}]);
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'));
}
@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.
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',
]);
@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?
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?
I usually turn off caches when I'm developing.
How is that done?
Please or to participate in this conversation.