Reputation: 4491
I have 3 types of users in the application as below:
1. Super Admin
2. Normal User
3. Staff
I did set up for the Super Admin but want to set Login System for the "Staff" user. I created one folder name as Staff
in Controllers folder /var/www/html/projecy_name/app/Http/Controllers
and created one Controller with name SessionsController.php
, here is the content in that file:
<?php
namespace App\Http\Controllers\Staff;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Staff;
class SessionsController extends Controller
{
public function __construct()
{
$this->middleware('guest',['except'=>'destroy']);
}
public function create()
{
return view('staffsession.create');
}
public function store()
{
echo request("email");
$test = auth()->attempt(request(['email','password']));
dd($test);
if(!auth()->attempt(request(['email','password'])))
{
Request()->session()->flash('error_msg', 'Please check your credentials and try again.');
return back()->withErrors([
'message' => 'Please check your credentials and try again.'
]);
}
return redirect()->home();
}
public function destroy()
{
auth()->logout();
return redirect()->home();
}
}
?>
In store() function, it is by default going to check in
admins
table, but I want that it must be check in mytbl_staff
table.
How to achieve that?
Upvotes: 0
Views: 1702
Reputation: 1328
You should define a new guard and provider in the \config\auth.php
file.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'staff' => [
'driver' => 'session',
'provider' => 'staff',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'staff' => [
'driver' => 'database',
'table' => 'tbl_staff',
],
],
After that you need to create a new Guard instance with the new provider. It will use the new table for authentification.
Place a guard()
method into your login controller:
use Illuminate\Support\Facades\Auth;
protected function guard()
{
return Auth::guard('staff');
}
Update:
You have to use your methods differently, so that it works. Use the guard()
method instead of the auth()
method because it uses the default guard. See:
<?php
namespace App\Http\Controllers\Staff;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Staff;
use Illuminate\Support\Facades\Auth;
class SessionsController extends Controller
{
public function __construct()
{
$this->middleware('guest',['except'=>'destroy']);
}
protected function guard()
{
return Auth::guard('staff');
}
public function create()
{
return view('staffsession.create');
}
public function store()
{
if(!$this->guard()->attempt(request(['email','password'])))
{
Request()->session()->flash('error_msg', 'Please check your credentials and try again.');
return back()->withErrors([
'message' => 'Please check your credentials and try again.'
]);
}
return redirect()->home();
}
public function destroy()
{
auth()->logout();
return redirect()->home();
}
}
Upvotes: 1