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?
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'
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With