Laravel Authenticate Login and Logout Functions

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
  1. Login Function.
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/');
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Fetching a Trip Down Memory Lane — JS

A Tinder Progressive Web App Performance Case Study

API Authentication with Phoenix and React — part 2

How to use React with Symfony 4

CRUD operations with Express.js (Node Js framework), MongoDB & Mongoose

Making user journeys configurable in the GOV prototype kit

•seduzione• Disponibile/Available . . . #tattoo #blackwork #dotwork #blacktattoo…

Short form with a bold first sentence can’t be clicked

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
Shah Md. Iktakhairul Islam

Shah Md. Iktakhairul Islam

More from Medium

How To Use Factory States In Laravel 8

Integrate google reCAPTCHA enterprise with laravel and vue -Part 01

Setting 🆙 Cloudflare Email fo

How To Install Laravel ?