Laravel Multiple Authentication using guards, not working!


  • Web Development

    laravel framework එක හොඳට දන්න කෙනෙක් උදව් කරන්න. customers ලටයි Admin users ලටයි වෙන වෙනම guards දෙකක් හඳල තියෙන්නෙ. ඒත් attempt method එකෙන් හරියට login එක වෙලා redirect වෙන්නෙ නැ.

    මේ Logincontroller එක
    dd($status) එකෙනුත් false රිටන් වෙන්නෙ.

    <?php
    
    namespace App\Http\Controllers\Auth;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    use Illuminate\Http\Request;
    use Auth;
    
    class LoginController extends Controller
    {
        /*
        |--------------------------------------------------------------------------
        | Login Controller
        |--------------------------------------------------------------------------
        |
        | This controller handles authenticating users for the application and
        | redirecting them to your home screen. The controller uses a trait
        | to conveniently provide its functionality to your applications.
        |
        */
    
        use AuthenticatesUsers;
    
        /**
         * Where to redirect users after login.
         *
         * @var string
         */
        protected $redirectTo = '/home';
    
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('guest')->except('logout');
            $this->middleware('guest:adminuser')->except('logout');
            $this->middleware('guest:customer')->except('logout');
        }
        
        /*public function showUserLoginForm() //show backend login form for admin users
        {
            return view('auth.login', ['url' => 'admin']); //edit this line to set path for adminLTE login form
        }*/
    
        public function adminLogin()
        {
            return view('admin.adminLogin');
        }
    
        public function adminHome()
        {
            return view('admin.home');
        }
    
        //login for the Admin user
        public function userLogin(Request $request)
        {
            $this->validate($request, [
                'username'   => 'required|min:3',
                'password' => 'required|min:6|max:6'
            ]);
    
            $status = Auth::guard('adminuser')->attempt(['email' => $request->username, 'password' => $request->password]);
            dd($status);
            if ($status == true) {
                return redirect()->intended('/adminhome');
                //$request->session()->flash('success', 'Login Successfully!');
               // return redirect('/adminhome'); // redirect to admin dashboard
            }
            else{
                $request->session()->flash('error', 'Sorry,Login Unuccessfull!');
                return redirect()->back();
            }
            //return back()->withInput($request->only('email', 'remember'));
        }
    
    
        //login for the customer
        public function customerLogin(Request $request)
        {
            $this->validate($request, [
                'username' => 'required|min:3',
                'password' => 'required|min:6|max:6'
            ]);
            
            $status = Auth::guard('customer')->attempt(['email' => $request->username, 'password' => $request->password], $request->get('remember'));
            if ($status == true) {
                $request->session()->flash('success', 'Login Successfully!');
                return redirect('/index');
            }
            else{
                $request->session()->flash('error', 'Sorry,Login Unuccessfull!');
                return redirect()->back();
            }
            //return back()->withInput($request->only('email', 'remember'));
        }
    }
    
    

    AdminUser.php Model එක

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    class AdminUser extends Authenticatable
    {
        protected $table = 'adminusers';
    
        use Notifiable;
    
        protected $guard = 'adminuser';
    
        protected $fillable = [
            'username', 'password',
        ];
    
        protected $hidden = [
            'password', 'remember_token',
        ];
    }
    
    

    auth.php මේකෙ තමා customer , adminuser කියන guards දෙක හදල තියෙන්නෙ

    <?php
    
    return [
    
        /*
        |--------------------------------------------------------------------------
        | Authentication Defaults
        |--------------------------------------------------------------------------
        |
        | This option controls the default authentication "guard" and password
        | reset options for your application. You may change these defaults
        | as required, but they're a perfect start for most applications.
        |
        */
    
        'defaults' => [
            'guard' => 'web',
            'passwords' => 'users',
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Authentication Guards
        |--------------------------------------------------------------------------
        |
        | Next, you may define every authentication guard for your application.
        | Of course, a great default configuration has been defined for you
        | here which uses session storage and the Eloquent user provider.
        |
        | All authentication drivers have a user provider. This defines how the
        | users are actually retrieved out of your database or other storage
        | mechanisms used by this application to persist your user's data.
        |
        | Supported: "session", "token"
        |
        */
    
        'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],
    
            'api' => [
                'driver' => 'token',
                'provider' => 'users',
                'hash' => false,
            ],
            'customer' => [
                'driver' => 'session',
                'provider' => 'customers',
            ],
            'adminuser' => [
                'driver' => 'session',
                'provider' => 'adminusers',
            ],
        ],
    
        /*
        |--------------------------------------------------------------------------
        | User Providers
        |--------------------------------------------------------------------------
        |
        | All authentication drivers have a user provider. This defines how the
        | users are actually retrieved out of your database or other storage
        | mechanisms used by this application to persist your user's data.
        |
        | If you have multiple user tables or models you may configure multiple
        | sources which represent each model / table. These sources may then
        | be assigned to any extra authentication guards you have defined.
        |
        | Supported: "database", "eloquent"
        |
        */
    
        'providers' => [
            'users' => [
                'driver' => 'eloquent',
                'model' => App\Models\User::class,
            ],
            'customers' => [
                'driver' => 'eloquent',
                'model' => App\Models\Customer::class,
            ],
            'adminusers' => [
                'driver' => 'eloquent',
                'model' => App\Models\AdminUser::class, 
            ],
    
            // 'users' => [
            //     'driver' => 'database',
            //     'table' => 'users',
            // ],
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Resetting Passwords
        |--------------------------------------------------------------------------
        |
        | You may specify multiple password reset configurations if you have more
        | than one user table or model in the application and you want to have
        | separate password reset settings based on the specific user types.
        |
        | The expire time is the number of minutes that the reset token should be
        | considered valid. This security feature keeps tokens short-lived so
        | they have less time to be guessed. You may change this as needed.
        |
        */
    
        'passwords' => [
            'users' => [
                'provider' => 'users',
                'table' => 'password_resets',
                'expire' => 60,
            ],
        ],
    
    ];
    
    

    Adminuser login form එක

     <form action="/adminUserLogin" method="POST">
            @csrf
            <div class="input-group mb-3">
              <input type="text" class="form-control" placeholder="Username" name="username" id="username">
              <div class="input-group-append">
                  <span class="fa fa-user input-group-text"></span>
              </div>
            </div>
            <div class="input-group mb-3">
              <input type="password" class="form-control" placeholder="Password" name="password" id="password">
              <div class="input-group-append">
                  <span class="fa fa-lock input-group-text"></span>
              </div>
            </div>
            <div class="row">
              <div class="col-8">
                <div class="checkbox icheck">
                  <label>
                    <input type="checkbox"> Remember Me
                  </label>
                </div>
              </div>
              <!-- /.col -->
              <div class="col-4">
                <button type="submit" class="btn btn-primary btn-block">Sign In</button>
              </div>
              <!-- /.col -->
            </div>
          </form>
    

    කොහොමද මේක හදාගන්නෙ. plz help!



  • This post is deleted!


  • This post is deleted!


  • Ok. balamu


  • Web Development

    @root any answer?


  • Web Development

    @root @dev_lak ara laravel experts




  • Web Development

    For your reference @cody


  • Web Development

    @Anjana_Gihan thanks bro i did not use laravel gurd


Log in to reply