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