mirror of
https://github.com/koel/koel
synced 2024-12-25 12:03:06 +00:00
50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\API\UserLoginRequest;
|
|
use App\Models\User;
|
|
use App\Repositories\UserRepository;
|
|
use App\Services\TokenManager;
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
|
use Illuminate\Hashing\HashManager;
|
|
use Illuminate\Http\Response;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
use ThrottlesLogins;
|
|
|
|
/** @param User $user */
|
|
public function __construct(
|
|
private UserRepository $userRepository,
|
|
private HashManager $hash,
|
|
private TokenManager $tokenManager,
|
|
private ?Authenticatable $user
|
|
) {
|
|
}
|
|
|
|
public function login(UserLoginRequest $request)
|
|
{
|
|
/** @var User|null $user */
|
|
$user = $this->userRepository->getFirstWhere('email', $request->email);
|
|
|
|
if (!$user || !$this->hash->check($request->password, $user->password)) {
|
|
abort(Response::HTTP_UNAUTHORIZED, 'Invalid credentials');
|
|
}
|
|
|
|
return response()->json([
|
|
'token' => $this->tokenManager->createToken($user)->plainTextToken,
|
|
]);
|
|
}
|
|
|
|
public function logout()
|
|
{
|
|
if ($this->user) {
|
|
$this->tokenManager->destroyTokens($this->user);
|
|
}
|
|
|
|
return response()->noContent();
|
|
}
|
|
}
|