Laravel 8 Authentication doesn't work after upgrade form 7 - laravel

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, ],

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']

Login auth with 2 different tables

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'));
}

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 Multiple Auth::attempt

I have two models in my laravel v5.4 project, user and admin.
In config/auth.php i added admin to guards and providers as below :
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
],
Now in AdminController class i want to use Auth::attempt function but by default it uses the users table. I can change defaults in config/auth.php as below and it works but in this case i can not use Auth::attempt for users.
'defaults' => [
'guard' => 'admin',
'passwords' => 'users',
],
I want to set user as default but use Auth::attempt function for admin with a method like Auth::attempt('admin',[credentials]). How can i use Auth::attempt for Admin model?
You call the guard directly, like this:
Auth::guard('admin')->attempt($credentials);
Or with the helper:
auth()->guard('admin')->attempt($credentials);
Or, even shorter with the helper:
auth('admin')->attempt($credentials);

Resources