0

2 years agoby Renan

route with middleware auth don't redirect and don't return errors on Laravel 5.5

Hello, I'm new on Laravel and I'm finding it difficult in some parts. For example: After long period searching a solution on google, I got create a custom login with table name "tab_employee", field login as "username" and field password as "pass". And every files that i've found with auth using "email" and "password" fiedls, I've changed to my custom "username" and "pass". PS: In this application I won't use the "Register".

Some files that I remember have changed: I config my web.php like Route::get('/', function () { return redirect('/login'); }); Auth::routes(); Route::get('/dashboard', function () { return view('dashboard'); })->name('dashboard');

In LoginController.php I've set protected $redirectTo = '/dashboard'; public function username() { return 'username''; }

in User.php I've set protected $fillable = 'username', 'pass', ; public function getAuthPassword() { return bcrypt($this->pass); } protected $table = 'tab_employee';

in auth.php 'defaults' => 'guard' => 'web', 'passwords' => 'tab_employee', , 'providers' => [ 'users' => 'driver' => 'eloquent', 'model' => App\User::class, 'table' => 'tab_employee', , ],

I've tested and my custom login worked for me! But now i need to validate if user was authenticated to access page "dashboard". Then I done on route: Route::group('middleware' => 'auth', function () { Route::get('/dashboard', function () { return view('dashboard'); })->name('dashboard'); });

PS: I tested this type of route group in a new project using standard laravel authentication (email, password, table users) and it works normally.

That way in my "employee" project, when I put wrong "username" or "pass", it validates correctly. But when i put the correct credentials, the submit returns to login page without errors and I can't access "dashboard". I don't know if I need to configure in some other file, the redirection or the custom authentication fields.

I think it would be nice if you could make a video teaching how to login with custom authentication and table fields, using 'middleware' => 'auth'.

22

Sarthak replied 2 years ago

0

Thanks for writing here.

Have you watched my authentication series. ( Click here to watch now)

Please watch it, I have even explained how you can create custom login.

Ask Question
Most Visited
Categories