I have 4 types of users in my application and details are stored in 4 different tables, so how can I implement Laravel's Authentication?
Route::post('/adlogin', 'mainController@adminlogin');
Route::get('/sellerlogin', function () {
return view('seller.pages.login');
});
Route::post('/sellerlog_in', 'mainController@sellerlogin');
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//Use Dependencies
use Auth;
class AdminLoginController extends Controller
{
Middleware used here is
adminwhich you have to create inconfig/auth.php
public function __construct()
{
$this->middleware('guest:admin', ['except'=>'logout']);
}
Create a view for your admin Login
public function showLoginForm()
{
return view('auth.admin_login');
}
public function login(Request $request)
{
//Validate the Form Data
$this->validate($request, [
'email'=>'required|email',
'password'=>'required|min:5'
]);
//Attempt to log the Admin In
$email= $request->email;
$password= $request->password;
$remember= $request->remember;
After Successfully login where you want to redirect like here I am redirecting to
admin.dashboard
//If Successful redirect to intended location
if (Auth::guard('admin')->attempt(['email' => $email, 'password' => $password], $remember)) {
return redirect()->intended(route('admin.dashboard'));
}
//If Unsuccessful redirect back to login form with form data
return redirect()->back()->withInput($request->only('email', 'remember'));
}
/**
* Log the Admin out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function logout()
{
Auth::guard('admin')->logout();
return redirect()->route('admin.login');
}
}
After logout redirect back to login page
Make sure you are using right
guardfor right User. Create same functionality for more user types as you want createguardsfor them.
In config/app.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
],
As admin and admin-api created here create for your user types
Add providers
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
At last for resetting passwords use this.
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 10,
],
],
In http/middleware/redirectIfAuthenticated.php add this to your handle function
//Check for admin login guard
switch ($guard) {
case 'admin':
if (Auth::guard($guard)->check()) {
return redirect()->route('admin.dashboard');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect('/dashboard');
}
break;
}
You can add more cases for your user types.
LoginController for all user types or use your logic to log them in.guards for every user type in auth.phpRedirectIfAuthenticatedIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With