In my configuration file I created 4 guards for a login where 4 models will participate
I modified the LoginController so that it works with different guards, but so entering the correct values always redirects me to the login, the methods that I overwrote in my LoginController was
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateLogin(Request $request)
{
$this->validate($request, [
'rol' => 'required|exists:roles,name',
$this->username() => 'required|string',
'password' => 'required|string',
]);
}
/**
* Attempt to log the user into the application.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function attemptLogin(Request $request)
{
return $this->guard($request->rol)->attempt(
$this->credentials($request), $request->filled('remember')
);
}
/**
* Get the guard to be used during authentication.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard($nameGuard)
{
return Auth::guard($nameGuard.'s');
}
/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
config(['auth.defaults.guard' => $request->rol.'s']);
return $this->authenticated($request, $this->guard($request->rol)->user())
?: redirect()->intended($this->redirectPath());
}
in the request I send the role that from this name I assign the corresponding guard, if the role is admin the guard is admins.
What other changes should I make to make the login work, when trying to access auth()->user();
I do not receive anything, but if I type auth('admins)->user();
it returns me correctly
my routes
Route::group(['prefix' => 'admin', 'middleware' => ['auth:'.config('auth.defaults.guard')],
'namespace' => 'Admin', 'as' => 'admin.'], function () {
/***/
});
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire