I have looked for days, but my user never gets authenticated when using the auth middleware.
We don’t use the standard laravel way, but using the standard auth middleware to block access to a route group. (in this example the dashboards)
The data is correct as it doesnt give any error message, but the user itself never gets authenticated. I’ve been stuck at it since wednesday and I hope any of you can finally fix this problem.
There isn’t really any specific code, but my (try to) login user always get directed to the unauthenticated method. (which leads back to another route of mine, index)
Important database columns for login are called ‘username’ and ‘password’ (the standard name laravel uses)
So, please, if any of you can help, I’d be hella grateful!
Inside LeerlingController (where login function is)
public function postLogin(Request $request) { $this->validate($request, [ 'username' => 'required', 'password' => 'required' ]); if (!Auth::attempt(['username' => $request->username, 'password' => $request->password])){ return redirect()->back()->with(['fail' => 'We konden je niet inloggen als '.$request->username]); } return redirect()->route('leerling.dashboard'); }
Inside Leerling Model (the part to make it authenticatable)
use IlluminateAuthAuthenticatable; class Leerling extends Model implements IlluminateContractsAuthAuthenticatable { use Authenticatable;
Method that keeps happening while logging in a valid user as no fail message comes up Which is located in AppExceptionsHandler.php
protected function unauthenticated($request, AuthenticationException $exception) { return $request->expectsJson() ? response()->json(['message' => $exception->getMessage()], 401) : redirect()->route('index'); }
The dashboard routes protected by auth middleware
Route::group(['middleware' => 'auth'], function () { Route::get('/leerling/dashboard', [ 'uses' => 'LeerlingController@getDashboard', 'as' => 'leerling.dashboard' ]); // Don't mind this one, it's for the admin dashboard Route::get('/admin/dashboard', [ 'uses' => 'AdminController@getDashboard', 'as' => 'admin.dashboard' ]); });
Auth.php
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => AppLeerling::class, ],
Advertisement
Answer
Laravel models default to id
as a column name in Models
. If you use anything different that id
, you should set a protected
property $primaryKey
with the name of your table’s primary key. Eg:
protected $primaryKey = 'Leerling_ID';
Documentation: Eloquent: #Primary Keys