Login auth with 2 different tables - laravel

I have the laravel 8 auth login form working perfectly, but i need to create a new athentication system for a different kind of users that are being stored in the database in another table with their own username and password. how can i achieve that?

You can do this using Guard by adding a new guard in "config/auth.php".
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin_users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admin_users' => [
'driver' => 'eloquent',
'model' => App\Models\Admin\AdminUser::class,
],
],
Here I have created a new Auth Guard "admin". Using this guard you can attempt login from a different table.
For login attempts:
For User:
if (Auth::guard('web')->attempt(['email' => $request->email, 'password' =>
$request->password], $request->remember)) {
return redirect()->intended(route('home'));
}
For Admin:
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' =>
$request->password], $request->remember)) {
return redirect()->intended(route('admin.dashboard'));
}

Related

Laravel Sanctum middleware with OR condition

I am using Laravel Sanctum for Token Based Authentication.
I have created a customer guard for customer. For Other users its default users table.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'customer' => [
'driver' => 'sanctum',
'provider' => 'customers',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => Customer::class,
],
],
I was authenticating all other users using middleware' => ['auth:sanctum'] now for the given particular route, I want customer guard can also access this Route. But there is no such Functionality provided by Laravel for OR condition.
Route::group(['as' => 'business', 'prefix' => 'business', 'middleware' => ['auth:sanctum']], function () {
Route::get('/categories', [BusinessCategoryController::class, 'listBusinessCategories'])->name('business-category');
Route::get('/categories/{parentId}', [BusinessCategoryController::class, 'listBusinessSubCategories'])->name('business-sub-category');
});
I just want middleware' => ['auth:sanctum'] or ['auth:customer']

Laravel 8 Authentication doesn't work after upgrade form 7

As the title says, it just stopped working out of sudden. I have a custom guard:
config.auth.php
'guards' => [
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
.....
'providers' => [
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
The user (or shall I say admin?) gets authenticated through login controller:
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password, ])) {
return redirect()->intended('/check');
but the route 'check' returns null
Route::get('/check', function () {
$user = auth()->user();
dd($user);
});
.env session part:
SESSION_DRIVER=file
SESSION_LIFETIME=1200
No any errors logged out, no any clue whats wrong. This app also has laravel Passport for API logins, which work fine
Try this, replace provider with this:
'providers' => [
'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ],

Laravel Login to Admin dashboard redirect to 404 page after enable AuthenticateSession middleware

Im working in simple blog site using laravel which has 2 parts , blog site for users and admin dashboard
It is all works good before but after uncomment below line in Http/Kernal.php
\Illuminate\Session\Middleware\AuthenticateSession::class,
failed to login and redirect to admin dashboard and it is redirect to 404 page.
I just want to use AuthenticateSession middleware because , i want to use
logoutOtherDevices function for website users
Auth::logoutOtherDevices($request->input('new-password'));
when i comment out this line \Illuminate\Session\Middleware\AuthenticateSession::class, again it is working normal .
please help me to solve this issue .
I have users table used for login users and admin table for login admin to dashboard
here is table for admin :
here is config/auth.php :
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Model\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Model\Admin::class,
]
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
]
],
];
here is admin login function :
public function login(Request $request)
{
// Validate the form data
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password,'status'=>1], $request->remember)) {
// if successful, then redirect to their intended location
return redirect()->intended(route('admin.dashboard'));
}else{
// if unsuccessful, then redirect back to the login with the form data
return redirect()->back()->withInput($request->only('email', 'remember'));
}
}

How to verify emails on Laravel 5.7 Multiple Guards?

I am using multiple guards and want to use Laravel inbuilt system for sending verification emails. I am doing following
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'frontEnd' => [
'driver' => 'session',
'provider' => 'customers',
],
...
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
...
],
In the User Model and Customer Model, I am using MustVerifyEmail. Both Model has email_verified_at column. When I am creating a user and when that user tries to login he redirects to email/verify the route. Email does not trigger at the time of creating a User.
For some reasons I am not using Auth::routes(['verify' => true]); insted i put
Route::get('email/verify', 'Auth\VerificationController#show')->name('verification.notice');
Route::get('email/verify/{id}', 'Auth\VerificationController#verify')->name('verification.verify');
Route::get('email/resend', 'Auth\VerificationController#resend')->name('verification.resend');
On route/web.php file

Laravel 5.2 Multi Table Authentication ErrorException in AuthManager.php

I use Laravel 5.2 and I need to use multi table authentication. I read from this link Can anyone explain Laravel 5.2 Multi Auth with example
I modified config/auth.php
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
//User Providers
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
]
],
//Resetting Password
'passwords' => [
'user' => [
'provider' => 'user',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admin' => [
'provider' => 'admin',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
Here is part of the controller for login (post method)
$admindata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($admindata)) {
echo 'SUCCESS!';
} else {
$admin = Auth::admin();
return Redirect::to('/b');
}
But I got this error
ErrorException in AuthManager.php line 288: call_user_func_array()
expects parameter 1 to be a valid callback, class
'Illuminate\Auth\SessionGuard' does not have a method 'admin'
It looks like the error is on Auth::attempt(). How to solve this error?
I believe the error is not in attempt method but here:
$admin = Auth::admin();
You try to run here admin method and obviously there is no such method in Illuminate\Auth\SessionGuard class.

Resources