PHP Laravel - how to redirect to login if user is not authenticated
Hi,
I don't know how to make an authorization / authentication rule in PHP Laravel FW - if the user is not logged in, he doesn't get anywhere, only for login.
When I go to it via Policy, when I want to set, for example, that it must be logged in for the analyzer () function, then it doesn't work ... Thanks for the advice
class TrackPolicy
Thanks
Hi,
I add how I solved it:
Modify App\Http\Middleware\Authenticate so that it redirects the unlogged-in user to login:
Modify routes/web.php - make a Route: group and place routes in it, where the unregistered user should not get to:
I don't know how to make an authorization / authentication rule in PHP Laravel FW - if the user is not logged in, he doesn't get anywhere, only for login.
When I go to it via Policy, when I want to set, for example, that it must be logged in for the analyzer () function, then it doesn't work ... Thanks for the advice
class TrackPolicy
/**
* Určuje, zdali uživatel může použít analyzer
*
* @param User $user
* @param Track $track
* @return mixed
*/
public function analyzer(User $user)
{
if ($user->hasRole('admin') || $user->hasRole('editor')) {
return Response::allow();
}
return Response::deny('Nemáte dostatečná oprávnění pro použití analyzeru!');
}
Thanks
REPLY
Hi,
I add how I solved it:
Modify App\Http\Middleware\Authenticate so that it redirects the unlogged-in user to login:
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
Modify routes/web.php - make a Route: group and place routes in it, where the unregistered user should not get to:
Route::group( ['middleware' => 'auth' ], function()
{
/**
* Crack
*/
Route::resource('crack', 'CrackController'); // only CRUD support
});