Why laravel 6 auth returns false after redirecting by using custom guard?

Related searches

I am trying to make auth through laravel package using admins table. In the project directory I added admin guard into config/auth.php

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

And in the guard array

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
    ],

Following is my login controller inside pacakge

class LoginController extends Controller
{

   use AuthenticatesUsers;
   protected $redirectTo = '/admin/dashboard';
   protected function redirectTo()
   {
         return '/admin/dashboard';
   }

   public function __construct()
   {
       $this->middleware('guest')->except('logout');
   }
   public function login(Request $request)
   {   
       if(Auth::guard('admin')->attempt($request->only('email','password'), true)){
           return redirect()
               ->intended(route('dashboard'))
               ->with('status','You are Logged in as Admin!');
       }
   }

}

and following is my dashboard controller

class DashboardController extends Controller
{
    public function __construct()
    {
        /* dd(Auth::check()); */ //return false : just want to show you

          $this->middleware('auth:admin');
    }

    public function index()
    {
        return view('xyz::dashboard');
    }

}

And in my Admin.php Model following script is there

namespace App;

class Admin extends \ABC\xyz\App\Models\Admin
{

}

Which is extending package model

namespace ABC\xyz\App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{

    protected $table = 'admins';
}

And below are the routes from my package

    $namespace = 'ABC\Xyz\App\Http\Controllers';
    Route::group([    
    'namespace' => $namespace,
    'middleware' => ['web'], 
    'prefix' => 'admin'
], function () {
    Route::get('login', function(){
        return view('xyz::auth.login');
    })->name('login');

    Route::post('/login', 'Auth\LoginController@login')->name('customLogin');
});

Route::group(['namespace' => $namespace,'prefix' => 'admin',  'middleware' => ['auth']  ], function () {
    Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});

When I try to login, after submitting valid details it does not redirecting me to dashboard, nothing happening. Also when I try for open forcefully /dashboard it take me to login page.

Also right after login attempt when I try Auth::check() it's returns true but same thing returning false in dashboardController.php construct function. In the same way Auth::guard('admin')->user() returns user's info while on dashboardController.php it's returns null.

Strange Result of php artisan route:list

As you can see in DashboardController.php construct I added $this->middleware('auth:admin');

So when I try to add dd(Auth::guard('admin')->user()) and then check in terminal php artisan route:list it returns null and sometime false, any idea why it is happening?

I don't know what and where I am missing something.

I would like to request you kindly guide me about it. I would appreciate.

Thank you

The problem is in your routes file:

Route::group(['namespace' => $namespace,'prefix' => 'admin',  'middleware' => ['auth']  ], function () {
    Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});

You are using the default guard with auth middleware. After you are logged in with admin guard you may not be logged in by your default web guard. That is why it fails and tries to redirect you to login page:

When I try to login, after submitting valid details it does not redirecting me to dashboard, nothing happening. Also when I try for open forcefully /dashboard it take me to login page.

Instead, you should specify in your group that you are using the admin guard:

Route::group(['namespace' => $namespace,'prefix' => 'admin',  'middleware' => ['auth:admin']], function () {
    Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});

However, you already specified in your DashboardController to use $this->middleware('auth:admin');, so there is no need to specifiy it in the route group again. The following is enough and reduces the likelihood to create an error:

Route::group(['namespace' => $namespace,'prefix' => 'admin'], function () {
    Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});

laravel 6 auth returns false after redirecting by using custom guard , Also right after login attempt when I try Auth::guard('admin')check() it's returns true but same thing returning false in DashboardController.php� why laravel 6 auth:“guard” returns false after successful login and redirecting by using custom guard? Posted 1 month ago by rezanm2 I added a crvskp guard in de providers in config/auth.php.

An extraction sample of the how you should define your admin model:

 // app/Admin.php
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = 'admin';

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

For more on multiple authentications guards see: How to use multiple authentication guards

why laravel 6 auth:“guard” returns false after successful login and , why laravel 6 auth:“guard” returns false after successful login and redirecting by using custom guard? Posted 3 months ago by rezanm2. I added a crvskp guard� In this tutorial, we'll learn how to customize the auth system in our Laravel 6 CRM app to redirect users after they register or login to a different route depending on their role. Most of the times, the authentication system provided by Laravel 6 is enough for adding login and registration to your web application.

Auth::guard('admin')->attempt($request->only('email','password') its returning true or false? If returning false then maybe toy didnt hashed your password Try add this in your Model

 public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = Hash::make($password);
    }

Auth middleware for custom guard always return false, While trying to authenticate with custom guard throws me an error Trying But Auth::guard('delimin')->check() return false after redirecting from� I'm trying to implements authentication system with Laravel 5.8 version. I have run php artisan make:auth and made some litlle changes in the code to adapt with what i need. I have created users with my Eloquent Model, with hashed password created with the Illuminate\Support\Facades\Hash class.

Please note that the Auth::check doesn't work on construct. this is because the middleware hasn't run yet, so Auth::check() should return false or null when you try to check in construct.

In your login controller, why are you using two redirectto?

   protected $redirectTo = '/admin/dashboard';
   protected function redirectTo()
   {
     return '/admin/dashboard';
   }

it is better to stick with one :-)

inside your Admin.php , add this:

protected $guard = 'admin';

for your web.php routes, replace

Route::group(['namespace' => $namespace,'prefix' => 'admin',  'middleware' => ['auth']  ], function () {
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});

with

Route::group(['namespace' => $namespace,'prefix' => 'admin',  'middleware' => ['auth:admin']  ], function () {
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});

finally, in DashboardController.php

replace the

        /* dd(Auth::check()); */ //return false : just want to show you

With:

    $this->middleware(function ($request, $next) {
        dd(Auth::check());  //return false : just want to show you
        die;    
    });

Auth::check() should return true!

Authentication, Social Authentication; Adding Custom Guards. Closure Request Guards. Adding Custom User Providers After migrating your database, navigate your browser to and modifying your route declaration: Auth::routes(['register' => false]); . You can customize the post-authentication redirect path using the HOME constant� In Laravel 5.6 you need to do the following. There is an authorization form - processes the LoginController, in my case I changed the email to name, and the password remained. If such a user is with such a password, then I authorize it, and if the user does not have such a name and password, then it is necessary to register with the data that

Laravel 5 Auth:attempt() always returns false, I am trying to make a custom login with multi auth. 'OPassword' => 'required| min:6|confirmed', ]); } /** * Create a new user instance after a valid registration. { return redirect('admin/dashboard'); } if (Auth::guard($guard)->check()) { return� Laravel has a great out-of-the-box Auth system, but surely we need to customize things here and there. For some of them, no need to look for external packages or write a lot of custom code, let&#8217;s explore what interesting abilities are hiding under the hood of Auth.

use Illuminate\Support\Facades\Auth; protected function guard() { return Auth::guard('guard-name'); } Validation / Storage Customization To modify the form fields that are required when a new user registers with your application, or to customize how new users are stored into your database, you may modify the RegisterController class.

In this article, we’re going to cover the authentication system in the Laravel framework. The main aim of this article is to create a custom authentication guard by extending the core

Comments
  • You must have defined the admin guard in the admin model otherwise, no admin will be authenticated
  • @Johhn you mean like this protected $guard = 'admin';
  • yes absolutely protected $guard = 'admin';
  • are you using custom primary key ?
  • @Nandakumar the primary key is id
  • I appreciate your time and followed your instructions but still session does not passing to dashboardController.php when I try $this->middleware('auth:admin'); dd(Auth::guard('admin')->check()); in construct function it returns false however in login() it returns true. It is so strange. May be because of package ?
  • @ImranAbbas this is normal behavior. If you call Auth::guard('admin')->check() in the constructor of your controller it will always return false. This is, because the controller may be added to the service container before authentication is working. You may find more about it here: laravel.com/docs/6.x/lifecycle However, redirect to dashboard should now work as expected, I guess?
  • @ImranAbbas try to add dd(Auth::guard('admin')->check()); to your index() method of DashboardController and it will return true (it has to be removed from constructor method though)
  • bro if I remove dd() in construct it does not letting me in index(), it is redirecting back to localhost:8000/login
  • @ImranAbbas What happens if you add a new route and try to access it like this: Route::get('dashboardtest', 'DashboardController@index'); . Does youdomain.dev/dashboardtest work or is it also redirecting you?
  • I just tried it does not working, still I am getting nothing in Dashboard's Controller
  • I am using bcrypt() function
  • That will also work Auth::guard('admin')->attempt($request->only('email','password') its returning true then?
  • After using your instructions I can't access DashboardController's constructor