daffaprayoga's avatar

Why my custom authentication with guard not working??

my login form only refresh the page when I submit the form, I'm sure I entered the correct email and password!! Please Help I'm still 17, still noob in laravel xD

0 likes
5 replies
daffaprayoga's avatar

My config/auth

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

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
    'admin_daerah' => [
        'driver' => 'session',
        'provider' => 'admin_daerah',
    ],
    'admin_daerah-api' => [
        'driver' => 'token',
        'provider' => 'admin_daerah',
    ],
],


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

My model

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class admin_daerah extends Authenticatable {

protected $guard = 'admin_daerahs';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'email', 'password', 'id_daerah'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

}

daffaprayoga's avatar

my web.php

Route::get('/admindaerahlogging','Auth\AdminDaerahLoginController@showLoginForm')->name('admindaerah.login');

Route::prefix('admindaerah')->group(function() { Route::post('/login', 'Auth\AdminDaerahLoginController@login')->name('admindaerah.login.submit');

Route::get('logout/', 'Auth\AdminDaerahLoginController@logout')->name('admindaerah.logout');

Route::get('/', 'AdminDaerahController@index')->name('admindaerah.dashboard');

});

daffaprayoga's avatar

My this guard's login Controller

public function login(Request $request)

{
  // Validate the form data
  $this->validate($request, [
    'email'   => 'required|email',
    'password' => 'required|min:6'
  ]);
  
  // Attempt to log the user in
  if (Auth::guard('admin_daerah')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
    // if successful, then redirect to their intended location
    return redirect()->intended(route('admindaerah.dashboard'));
  } 
  // if unsuccessful, then redirect back to the login with the form data
  return redirect()->back()->withInput($request->only('email', 'remember'));
}
BezhanSalleh's avatar
Level 25

@daffaprayoga in your model replace protected $guard = 'admin_daerahs'; with protected $guard = 'admin_daerah'; in your config/auth.php make sure that your table name is correct. it should be admin_daerahs. follow the naming convention... laravel can only do so much... change your model name and its reference to it to AdminDaerah from admin_daerah

and complete this course to get yourself up and running https://laracasts.com/series/laravel-from-scratch-2017 next time post all your code together...

1 like

Please or to participate in this conversation.