kphcdr 1 year ago
parent
commit
73e03f0ae5

+ 22 - 0
app/Base/BaseController.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Base;
+
+use App\Http\Controllers\Controller;
+
+class BaseController extends Controller
+{
+    public function ok($data = null)
+    {
+        return [
+            "data" => $data,
+        ];
+    }
+
+    public function error(string $msg)
+    {
+        return [
+            "error" => $msg,
+        ];
+    }
+}

+ 10 - 2
app/Exceptions/Handler.php

@@ -3,6 +3,7 @@
 namespace App\Exceptions;
 
 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
+use Illuminate\Validation\ValidationException;
 use Throwable;
 
 class Handler extends ExceptionHandler
@@ -22,7 +23,6 @@ class Handler extends ExceptionHandler
      * @var array<int, class-string<\Throwable>>
      */
     protected $dontReport = [
-        //
     ];
 
     /**
@@ -44,7 +44,15 @@ class Handler extends ExceptionHandler
     public function register()
     {
         $this->reportable(function (Throwable $e) {
-            //
+            return response()->json([
+                "error" => $e->getMessage(),
+            ], 400);
+        });
+        $this->renderable(function (ValidationException $e) {
+
+            return response()->json([
+                "error" =>json_encode($e->errors())
+            ], 400);
         });
     }
 }

+ 8 - 0
app/Exceptions/ValidateException.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace App\Exceptions;
+
+class ValidateException extends \Exception
+{
+
+}

+ 22 - 0
app/Http/Controllers/Admin/AuthController.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Http\Controllers\Admin;
+
+use App\Base\BaseController;
+use Illuminate\Http\Request;
+
+class AuthController extends BaseController
+{
+    public function login(Request $request)
+    {
+//        try {
+            $request->validate([
+                "username" => "required",
+                "password" => "required"
+            ]);
+//        } catch (\Exception $e) {
+//        }
+
+        return $this->ok();
+    }
+}

+ 1 - 1
config/app.php

@@ -169,7 +169,7 @@ return [
         Illuminate\Encryption\EncryptionServiceProvider::class,
         Illuminate\Filesystem\FilesystemServiceProvider::class,
         Illuminate\Foundation\Providers\FoundationServiceProvider::class,
-//        Illuminate\Hashing\HashServiceProvider::class,
+        Illuminate\Hashing\HashServiceProvider::class,
         Illuminate\Mail\MailServiceProvider::class,
         Illuminate\Notifications\NotificationServiceProvider::class,
         Illuminate\Pagination\PaginationServiceProvider::class,

+ 1 - 1
config/cors.php

@@ -15,7 +15,7 @@ return [
     |
     */
 
-    'paths' => ['api/*', 'sanctum/csrf-cookie'],
+    'paths' => ['api/*'],
 
     'allowed_methods' => ['*'],
 

+ 1 - 10
routes/api.php

@@ -3,13 +3,4 @@
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Route;
 
-/*
-|--------------------------------------------------------------------------
-| API Routes
-|--------------------------------------------------------------------------
-|
-| Here is where you can register API routes for your application. These
-| routes are loaded by the RouteServiceProvider within a group which
-| is assigned the "api" middleware group. Enjoy building your API!
-|
-*/
+Route::post("/admin/auth/login",[\App\Http\Controllers\Admin\AuthController::class,"login"]);