Home Laravel: Successful login redirects to the login page due to auth middleware on even when correct credentials provided
Reply: 0

Laravel: Successful login redirects to the login page due to auth middleware on even when correct credentials provided

user3675
1#
user3675 Published in July 21, 2018, 1:46 pm

I am creating an application for a pre-existing web application with pre-designed schema. This application will use the admins table to authenticate users (only admins can access this application). I cannot use users table because it has data of the clients of the application and do not necessarily need authentication because the application is used by admins only.

I have created the User model with the field protected $table = 'admins'. In order to user Laravel's inbuilt authentication libraries, I created required columns in the admin table by migrating the required fields for authentication such as email, password and remember_token. My User models is as follows:

<?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable, HasRoles;

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

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

I also have a route as follows:

    Route::get('/dashboard' , 'DashboardController@index')->name('dashboard.index')->middleware('auth');

The default LoginController redirects to route /dashboard when user is successfully authenticated. But there seems to be some problem with the auth middleware which does not seem to authenticate the currently logged in user to enter into /dashboard.

Also, I want to tell you that I have made no changes to the config/auth file in the guards key as the User model points to the admins table which has all the necessary fields for authentication which is as follows:

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

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


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

Now each time that I tried to access the /dashboard or any other routes with auth middleware, the application sends me to the login page. Since the login page will redirect to the dashboard ( which has auth middleware), I get redirected to the login page even when I pass correct credentials i.e. email and password.

Note that I checked the authentication of provided credentials through debugging and the controller successfully authenticated the provided credentials.(I checked this in the authenticated() method in the AuthenticatesUsers trait that Laravel's LoginController users. Notify me if this method is not appropriate to check whether the user was authenticated).

Is there some problem with the auth middleware or do I need to modify my guards configuration in the auth config file ?

Or, do I need to remigrate my migration files for admin table?

Please suggest best alternatives. Thank you..

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300431 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO