2
votes

When trying to go to an auth required middleware page I'm getting 'Route [login] not defined' the problem is, my login route isn't called 'login' and I don't want it to be called login.

Here are my routes...

Route::group(['middleware' => 'auth', 'namespace' => 'User'], function() {
    Route::get('/home', ['uses' => 'HomeController@getView', 'as' => 'frontend.user.home']);
});

Route::group(['middleware' => 'guest', 'namespace' => 'Guest'], function() {
    Route::get('/login', ['uses' => 'LoginController@getView', 'as' => 'frontend.guest.login']);
    Route::post('/login', ['uses' => 'LoginController@onPost', 'as' => 'frontend.guest.login']);
});

How can I get it to stop requiring route 'login' and start requiring my custom one 'frontend.guest.login' ??

2
change your auth middleware - Sohel0415
Share code, where you are calling get route on your laravel template - Niklesh Raut
Are you calling as route("login") ? - Niklesh Raut

2 Answers

1
votes

First of all, you should fix your following route names:

Route::get('/login', ['uses' => 'LoginController@getView', 'as' => 'frontend.guest.login']);
Route::post('/login', ['uses' => 'LoginController@onPost', 'as' => 'frontend.guest.login']);

Notice that, you've used frontend.guest.login for both (get/post) routes which is wrong, instead you should use unique names for example: frontend.guest.get.login for Route::get() and frontend.guest.post.login for Route::post().

Then, in your App\Exceptions\Handler class, create/override the following method:

protected function unauthenticated($request, AuthenticationException $exception)
{
    return $request->expectsJson()
                ? response()->json(['message' => 'Unauthenticated.'], 401)
                : redirect()->guest(route('frontend.guest.get.login'));
}

Also, use the use Illuminate\Auth\AuthenticationException; statement at the top of your class to import AuthenticationException class in your App\Exceptions\Handler.

Also, change every use case of frontend.guest.login to appropriate route name, use frontend.guest.post.login for form submission/action and frontend.guest.get.login to show the form or for the redirect.

0
votes

This happens because Laravel's default Authenticate middleware throws an AuthenticationException and Laravel's default way to handle this exception is to do this:

return $request->expectsJson()
            ? response()->json(['message' => $exception->getMessage()], 401)
            : redirect()->guest(route('login'));

You will find this code in the unauthenticated method of Illuminate\Foundation\Exceptions\Handler, which your default App\Exceptions\Handler should extend.

You are able to override this behaviour simply by providing your own unauthenticated method in your App\Exceptions\Handler class. For example:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    // The rest of your Handler class here...

    public function unauthenticated()
    {
        // Add your implementation here
    }
}