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

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

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

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

How to authenticate and logging in user in laravel using guard?

I'm trying to make authentication using guard in Laravel 5.8. Authentication is passed but somehow it's not logging in the user.
public function login(Request $request)
{
$email = $request->email;
$password = $request->password;
$credentials = $request->only('email','password');
if (Auth::guard('owner')->attempt($credentials,$request->remember)){
//echo "Authentication is passed";
return redirect()->intended('/owner/dashboard');
}
return redirect('/owner')->with('error','Login failed.');
}
When redirected to route /owner/dashboard which is filtered with $this->middleware('owner'), the user will be redirected to login form and get notification that login failed. Is Auth::guard('owner')->attempt($credentials) only authenticating without logging in user?
in config/auth.php add this
add new guard and the provider for this guard
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'owner' => [
'driver' => 'session',
'provider' => 'owner',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
```
```
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'owner' => [
'driver' => 'eloquent',
'model' => App\Owner::class,
],
],
```
```
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'owner' => [
'provider' => 'owner',
'table' => 'password_resets',
'expire' => 60,
],
],
```
Follow this article
https://medium.com/#sagarmaheshwary31/laravel-multiple-guards-authentication-setup-and-login-2761564da986

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