3
votes

I want to change the default Laravel routes from /auth/login to just /login and vis-à-vis with register.

These are my routes:

Route::get('login',     ['as' => 'getLogin', 'uses' => 'Auth\AuthController@getLogin']);
Route::post('login',    ['as' => 'postLogin', 'uses' => 'Auth\AuthController@postLogin']);
Route::get('logout',    ['as' => 'getLogout', 'uses' => 'Auth\AuthController@getLogout']);
Route::get('register',  ['as' => 'getRegister', 'uses' => 'Auth\AuthController@getRegister']);
Route::post('register', ['as' => 'postRegister', 'uses' => 'Auth\AuthController@postRegister']);

Now the problem arises, that when a user tries to access an area that is guarded, the Authentication class kicks in, redirecting the unauthenticated user back to /auth/login instead of just /login.

I though I could solve the problem by setting the $loginPath in my AuthController like this:

protected $loginPath = '/login';

But it seems like the $loginPath is just used for unsuccessful login attempts, instead of unsuccessful authentication attempts, like documented in the AuthenticateUsers Class.

Well I managed to change the redirectURL in the Authenticate Class from this:

return redirect()->guest('auth/login');

to this:

return redirect()->guest('login');

Which solved the issue, yet I would like to set a property for this in my AuthController like this:

protected $redirectIfMiddlewareBlocks = '/login';

For this I check in the Authenticate Class if a property exists, which I set in the AuthController before:

return redirect()->guest(property_exists($this, 'redirectIfMiddlewareBlocks') ? $this->redirectIfMiddlewareBlocks : '/shouldnotbeused');

Yet I get the /shouldnotbeused url, instead of the one set by the redirectIfMiddlewareBlocks property in my AuthController.

How do I set up the path for the login route in my AuthController correctly?

1
Why are you checking if it exists? You know it exists because you just added itandrewtweber
As far as I can remember, those are the only 2 places you need to set the login path. But I don't see why you're overcomplicating things with setting the property and checking if it exists. I would just go with what works redirect()->guest('login')andrewtweber
Well, I was just a little bit refactoring and thought it would be a good idea, to have this path saved in a variable somewhere at the top. And I check if it exist to see if it finds it, which it doesn'tLoveAndHappiness
But why the check? Will the property ever not exist?andrewtweber
To see if it actually accepts a poperty which is set in the authcontroller. Which it doesn't. Also I understand the implications of your question. You are saying why bothering checking if a property exists, if you set it first. Well I was reading some traits and got the idea, that it doesn't hurt to give the user of a class the option set a property, which differs from the default and then maybe not set it at all. But the option should be there. Hope you understood...LoveAndHappiness

1 Answers

4
votes

I think the problem is that you are checking the property on the wrong class. You are checking if it exists on $this, which is an instance of Authenticate when it is set on AuthController

So you should change your check to:

property_exists(AuthController::class, 'redirectIfMiddlewareBlocks')

Also, you can't access a protected property from another class. If you want to do that you'll have to make it public. Finally, I would make it static so that you don't have to instantiate an AuthController object to access it. So the final solution is:

class AuthController {
    public static $redirectIfMiddlewareBlocks = '/here';
}

And in your Authenticate middleware:

use App\Http\Controllers\AuthController;

class Authenticate {
    public function handle() {
        return redirect()->guest(
            property_exists(AuthController::class, 'redirectIfMiddlewareBlocks')
                ? AuthController::$redirectIfMiddlewareBlocks
                : '/shouldnotbeused'
        );
    }
}