diff --git a/app/Http/Controllers/Api/Admin/AuthController.php b/app/Http/Controllers/Api/Admin/AuthController.php index 7ff1a9c..52b4049 100644 --- a/app/Http/Controllers/Api/Admin/AuthController.php +++ b/app/Http/Controllers/Api/Admin/AuthController.php @@ -43,10 +43,32 @@ public function login(Request $request): JsonResponse 'password' => 'required|string', ]); + // 添加請求日誌 + Log::info('Login attempt details', [ + 'email' => $validated['email'], + 'request_data' => $request->all() + ]); + /** @var Admin|null $admin */ $admin = Admin::where('email', $validated['email'])->first(); + // 添加用戶查詢日誌 + Log::info('Admin query result', [ + 'admin_found' => $admin ? 'yes' : 'no', + 'admin_data' => $admin ? [ + 'id' => $admin->id, + 'email' => $admin->email, + 'role' => $admin->role + ] : null + ]); + if (!$admin || !Hash::check($validated['password'], $admin->password)) { + // 添加密碼驗證日誌 + Log::info('Password verification failed', [ + 'has_admin' => $admin ? 'yes' : 'no', + 'password_check' => $admin ? Hash::check($validated['password'], $admin->password) : 'admin not found' + ]); + return $this->error( ErrorCode::INVALID_CREDENTIALS, ErrorCode::getMessage(ErrorCode::INVALID_CREDENTIALS) @@ -71,6 +93,9 @@ public function login(Request $request): JsonResponse ]); } catch (ValidationException $e) { + Log::error('Validation error during login', [ + 'errors' => $e->errors(), + ]); return $this->error( ErrorCode::VALIDATION_ERROR, ErrorCode::getMessage(ErrorCode::VALIDATION_ERROR), @@ -80,6 +105,8 @@ public function login(Request $request): JsonResponse Log::error('Error during admin login', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), + 'file' => $e->getFile(), + 'line' => $e->getLine() ]); return $this->error( diff --git a/app/Models/Admin.php b/app/Models/Admin.php index d1f851a..d075b48 100644 --- a/app/Models/Admin.php +++ b/app/Models/Admin.php @@ -6,9 +6,12 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Foundation\Auth\User as Authenticatable; +use Laravel\Sanctum\HasApiTokens; class Admin extends Authenticatable { + use HasApiTokens; + /** * 管理员角色常量 */ diff --git a/composer.json b/composer.json index e67359e..294879a 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ "require": { "php": "^8.2", "laravel/framework": "^11.31", + "laravel/sanctum": "^4.0", "laravel/tinker": "^2.9", "predis/predis": "^2.3" }, diff --git a/composer.lock b/composer.lock index ba6c650..4b1bb17 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3c2add9f85c3cab994b57b6b8ff1eb72", + "content-hash": "e7dd48d806ba5d560c773b49c46386e5", "packages": [ { "name": "brick/math", @@ -1326,6 +1326,70 @@ }, "time": "2024-11-12T14:59:47+00:00" }, + { + "name": "laravel/sanctum", + "version": "v4.0.5", + "source": { + "type": "git", + "url": "https://github.com/laravel/sanctum.git", + "reference": "fe361b9a63407a228f884eb78d7217f680b50140" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/sanctum/zipball/fe361b9a63407a228f884eb78d7217f680b50140", + "reference": "fe361b9a63407a228f884eb78d7217f680b50140", + "shasum": "" + }, + "require": { + "ext-json": "*", + "illuminate/console": "^11.0", + "illuminate/contracts": "^11.0", + "illuminate/database": "^11.0", + "illuminate/support": "^11.0", + "php": "^8.2", + "symfony/console": "^7.0" + }, + "require-dev": { + "mockery/mockery": "^1.6", + "orchestra/testbench": "^9.0", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^10.5" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Laravel\\Sanctum\\SanctumServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Laravel\\Sanctum\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.", + "keywords": [ + "auth", + "laravel", + "sanctum" + ], + "support": { + "issues": "https://github.com/laravel/sanctum/issues", + "source": "https://github.com/laravel/sanctum" + }, + "time": "2024-11-26T14:36:23+00:00" + }, { "name": "laravel/serializable-closure", "version": "v2.0.0", diff --git a/config/auth.php b/config/auth.php index 0ba5d5d..2ee904e 100644 --- a/config/auth.php +++ b/config/auth.php @@ -40,6 +40,10 @@ 'driver' => 'session', 'provider' => 'users', ], + 'admin' => [ + 'driver' => 'sanctum', + 'provider' => 'admins', + ], ], /* @@ -62,13 +66,12 @@ 'providers' => [ 'users' => [ 'driver' => 'eloquent', - 'model' => env('AUTH_MODEL', App\Models\User::class), + 'model' => App\Models\User::class, + ], + 'admins' => [ + 'driver' => 'eloquent', + 'model' => App\Models\Admin::class, ], - - // 'users' => [ - // 'driver' => 'database', - // 'table' => 'users', - // ], ], /* diff --git a/database/sql/init.sql b/database/sql/init.sql index b328a68..64b648c 100644 --- a/database/sql/init.sql +++ b/database/sql/init.sql @@ -4,8 +4,27 @@ INSERT INTO `admins` (`username`, `email`, `password`, `role`, `created_at`, `up VALUES ( 'admin', 'admin@cv6.me', - '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', -- 'abc123' 的 bcrypt hash + '$2y$12$co71F.UxUP.TGvI/fMD4JuYS.meR7yoKfPQjQ43hOF.NXIBDn5dRm', -- 'abc123' 的新 hash 'super', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP ); + +-- 插入預設的 LLM Provider +INSERT INTO `llm_providers` ( + `name`, + `service_name`, + `api_url`, + `api_token`, + `status`, + `created_at`, + `updated_at` +) VALUES ( + 'OpenAI', + 'openai', + 'https://api.openai.com/v1', + 'sk-default-token', + 'active', + CURRENT_TIMESTAMP, + CURRENT_TIMESTAMP +);