koel/app/Http/Controllers/API/AuthController.php

51 lines
1.3 KiB
PHP
Raw Normal View History

2016-05-30 05:50:59 +00:00
<?php
namespace App\Http\Controllers\API;
2023-08-20 22:35:58 +00:00
use App\Exceptions\InvalidCredentialsException;
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;
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;
2016-05-30 05:50:59 +00:00
class AuthController extends Controller
{
2021-05-21 11:50:23 +00:00
use ThrottlesLogins;
2023-08-20 22:35:58 +00:00
public function __construct(private AuthenticationService $auth)
{
2018-08-31 13:47:15 +00:00
}
2016-05-30 05:50:59 +00:00
public function login(UserLoginRequest $request)
{
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 {
return response()->json($this->auth->login($request->email, $request->password)->toArray());
} catch (InvalidCredentialsException) {
$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
}
2022-11-16 17:57:38 +00:00
public function logout(Request $request)
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
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
}