Laravel Authenticate Login and Logout Functions

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
public function login(Request $request)
{
$this->validateLogin($request);

if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
$seconds = $this->limiter()->availableIn(
$this->throttleKey($request)
);
return throw ValidationException::withMessages([
$this->username() => [trans('auth.throttle', [
'seconds' => $seconds,
'minutes' => ceil($seconds / 60),
])],
])->status(Response::HTTP_TOO_MANY_REQUESTS);
}

if ($this->attemptLogin($request)) {
$request->session()->regenerate();
$this->clearLoginAttempts($request);

if ($response = $this->authenticated($request, $this->guard()->user())) {
return $response;
}
return $request->wantsJson() ? new JsonResponse([], 204) : redirect()->route('/');
}

$this->incrementLoginAttempts($request);

return throw ValidationException::withMessages([
$this->username() => [trans('auth.failed')],
]);
}

public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
if ($response = $this->loggedOut($request)) {
return $response;
}

return $request->wantsJson() ? new JsonResponse([], 204) : redirect()->to('http://laravel-sub-domain.com:8000/');
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store