FREDERIC LD's avatar

authentication question - 302 code

Hi,

I am having issues with my authentication system. I am using laravel 7 and Laravel/UI

I had the following routes:

Route::prefix('/admin/')->name('admin.')->namespace('Admin\Auth')->group(function(){

    Route::get('login', 'LoginController@showLoginForm')->name('login');
    Route::post('login', 'LoginController@login')
    Route::post('logout', 'LoginController@logout')->name('logout');

    Route::get('password/reset', 'ForgotPasswordController@showLinkRequestForm')->name('password.request');
    Route::post('password/email', 'ForgotPasswordController@sendResetLinkEmail')->name('password.email');
    Route::get('password/reset/{token}', 'ResetPasswordController@showResetForm')->name('password.reset');
    Route::post('password/reset', 'ResetPasswordController@reset')->name('password.update');

    Route::get('password/confirm', 'ConfirmPasswordController@showConfirmForm')->name('password.confirm');
    Route::post('password/confirm', 'ConfirmPasswordController@confirm');

    Route::get('email/verify', 'VerificationController@show')->name('verification.notice');
    Route::get('email/verify/{id}/{hash}', 'VerificationController@verify')->name('verification.verify');
    Route::post('email/resend', 'VerificationController@resend')->name('verification.resend');

});

I have a dashboard module with

Route::group(['middleware' => ['web', 'auth:admin'], 'as' => 'admin.', 'prefix' => '/admin', 'namespace' => 'RF\RFCmsDashboard\Http\Controllers'], function()
{
    
    App::setLocale(Session::get('lang'));

    Route::redirect('/', '/admin/dashboard');
    
    Route::get('dashboard','RFCmsDashboardController@index')->name('dashboard');
    
});

I can not figure what is gong on. When I submit the login form. I get redirected to my dashboard upon authentication but I can see in my browser inspector a 302 error related to the login page with POST. The type of the request is type/html. It is followed by the dashboard page with a code 200.

Request URL: http://127.0.0.1:8000/admin/login
Request Method: POST
Status Code: 302 Found
Remote Address: 127.0.0.1:8000

I can display the name of user which indicates the user must have logged in successfuly. I use this code to display the user name

{{ Auth::guard('admin')->user()->first_name }}
0 likes
4 replies
bugsysha's avatar
bugsysha
Best Answer
Level 61

302 response status code is not an error. Everything is OK. That means that route to which successful login should be redirected has been found.

1 like
FREDERIC LD's avatar

@bugsysha Thanks for that. On the back if this, I am getting a little confused with the middleware of my dashboard routes

Route::group(['middleware' => ['web', 'auth:admin'], 'as' => 'admin.', 'prefix' => '/admin', 'namespace' => 'RF\RFCmsDashboard\Http\Controllers'], function()

The above works, but if I remove the 'web' middleware, the login page does not forward me to the dashboard page and only reloads the login page. That's confusing me.

Should 'web' always be added in route middleware? I have read in posts (https://laracasts.com/discuss/channels/laravel/middleware-web) that the web middleware was added automatically.

Snapey's avatar

routes within web.php already have the web middleware, and you don't need it again.

You can confirm this by running php artisan route:list. the last column lists the middleware applied.

Only routes using the web middleware group have sessions enabled.

1 like
tonnytipper's avatar

Probably somewhere in your code, you're trying to access a route that's restricted by the middleware.

Please or to participate in this conversation.