2016-05-30 05:50:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2016-05-30 05:50:59 +00:00
|
|
|
use App\Http\Requests\API\UserLoginRequest;
|
2023-08-20 22:35:58 +00:00
|
|
|
use App\Services\AuthenticationService;
|
2024-04-19 13:25:08 +00:00
|
|
|
use App\Values\CompositeToken;
|
|
|
|
use Closure;
|
2021-05-21 11:50:23 +00:00
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
2022-11-16 17:57:38 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-09-06 18:21:39 +00:00
|
|
|
use Illuminate\Http\Response;
|
2024-04-19 13:25:08 +00:00
|
|
|
use Throwable;
|
2016-05-30 05:50:59 +00:00
|
|
|
|
|
|
|
class AuthController extends Controller
|
|
|
|
{
|
2021-05-21 11:50:23 +00:00
|
|
|
use ThrottlesLogins;
|
|
|
|
|
2024-04-18 11:27:07 +00:00
|
|
|
public function __construct(private readonly AuthenticationService $auth)
|
2023-08-20 22:35:58 +00:00
|
|
|
{
|
2018-08-31 13:47:15 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 05:50:59 +00:00
|
|
|
public function login(UserLoginRequest $request)
|
2024-04-19 13:25:08 +00:00
|
|
|
{
|
|
|
|
$compositeToken = $this->throttleLoginRequest(
|
|
|
|
fn () => $this->auth->login($request->email, $request->password),
|
|
|
|
$request
|
|
|
|
);
|
|
|
|
|
|
|
|
return response()->json($compositeToken->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loginUsingOneTimeToken(Request $request)
|
|
|
|
{
|
|
|
|
$compositeToken = $this->throttleLoginRequest(
|
|
|
|
fn () => $this->auth->loginViaOneTimeToken($request->input('token')),
|
|
|
|
$request
|
|
|
|
);
|
|
|
|
|
|
|
|
return response()->json($compositeToken->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function throttleLoginRequest(Closure $callback, Request $request): CompositeToken
|
2016-05-30 05:50:59 +00:00
|
|
|
{
|
2023-08-20 22:35:58 +00:00
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
|
|
|
$this->fireLockoutEvent($request);
|
|
|
|
$this->sendLockoutResponse($request);
|
|
|
|
}
|
2020-09-06 18:21:39 +00:00
|
|
|
|
2023-08-20 22:35:58 +00:00
|
|
|
try {
|
2024-04-19 13:25:08 +00:00
|
|
|
return $callback();
|
|
|
|
} catch (Throwable) {
|
2023-08-20 22:35:58 +00:00
|
|
|
$this->incrementLoginAttempts($request);
|
2020-09-06 18:21:39 +00:00
|
|
|
abort(Response::HTTP_UNAUTHORIZED, 'Invalid credentials');
|
|
|
|
}
|
2016-05-30 05:50:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 11:27:07 +00:00
|
|
|
public function logout(Request $request): Response
|
2016-05-30 05:50:59 +00:00
|
|
|
{
|
2023-08-20 22:35:58 +00:00
|
|
|
attempt(fn () => $this->auth->logoutViaBearerToken($request->bearerToken()));
|
2016-05-30 05:50:59 +00:00
|
|
|
|
2021-12-06 17:07:43 +00:00
|
|
|
return response()->noContent();
|
2016-05-30 05:50:59 +00:00
|
|
|
}
|
2023-08-20 22:35:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For the throttle middleware.
|
|
|
|
*/
|
|
|
|
protected function username(): string
|
|
|
|
{
|
|
|
|
return 'email';
|
|
|
|
}
|
2016-05-30 05:50:59 +00:00
|
|
|
}
|