ソースを参照

fix: arrumando para funcionar auth

DenLopes 1 年間 前
コミット
53a8d48e9d

+ 1 - 3
app/Http/Controllers/AuthController.php

@@ -9,15 +9,13 @@
 use App\Http\Resources\AuthResource;
 use App\Services\AuthService;
 use App\DataTransferObjects\RefreshTokenDto;
-use Illuminate\Http\Request;
 
 class AuthController extends Controller
 
 {
     public function __construct(
         protected AuthService $authService,
-    ) {
-    }
+    ) {}
 
     public function login(AuthRequest $request): JsonResponse
     {

+ 0 - 2
app/Models/User.php

@@ -2,8 +2,6 @@
 
 namespace App\Models;
 
-// use Illuminate\Contracts\Auth\MustVerifyEmail;
-
 use App\Enums\UserLanguageSource;
 use App\Enums\UserTypeSource;
 use Illuminate\Database\Eloquent\Factories\HasFactory;

+ 2 - 3
app/Repositories/AuthRepository.php

@@ -12,7 +12,6 @@
 class AuthRepository implements AuthRepositoryInterface
 {
     public function __construct(
-        protected Auth $model,
         protected User $userModel,
         protected PersonalAccessToken $personalAccessTokenModel
     ) {}
@@ -59,7 +58,7 @@ public function deleteUserTokensByDevice(User $user, string $deviceId): void
 
     public function attemptLogin(AuthDto $credentials): bool
     {
-        return $this->model->attempt($credentials->toArray());
+        return Auth::attempt($credentials->toArray());
     }
 
     public function refreshToken(PersonalAccessToken $tokenModel, User $user, string $deviceId): array
@@ -76,4 +75,4 @@ public function refreshToken(PersonalAccessToken $tokenModel, User $user, string
             ];
         });
     }
-}
+}

+ 1 - 2
app/Repositories/UserRepository.php

@@ -14,14 +14,13 @@ class UserRepository implements UserRepositoryInterface
 
     public function __construct(
         protected User $model,
-        protected Auth $auth
     )
     {
     }
 
     public function me(): ?User
     {
-        return $this->auth->user();
+        return Auth::user();
     }
 
     public function all(): ?Collection

+ 7 - 5
app/Services/AuthService.php

@@ -6,13 +6,13 @@
 use App\DataTransferObjects\AuthDto;
 use App\DataTransferObjects\RefreshTokenDto;
 use Illuminate\Support\Str;
+use Illuminate\Support\Facades\Auth;
 
 class AuthService
 {
     public function __construct(
         protected AuthRepositoryInterface $authRepository,
-    ) {
-    }
+    ) {}
 
     public function login(AuthDto $credentials): ?array
     {
@@ -57,13 +57,15 @@ public function refresh(RefreshTokenDto $refreshToken): ?array
         ]);
     }
 
-    public function logout(string $deviceId): void
+    public function logout(): void
     {
-        $user = auth()->user();
+        $user = Auth::user();
+        $tokenName = $user->currentAccessToken()->name;
+        $deviceId = Str::afterLast($tokenName, '_');
         if (!$user) {
             return;
         }
 
         $this->authRepository->deleteUserTokensByDevice($user, $deviceId);
     }
-}
+}

+ 2 - 0
bootstrap/app.php

@@ -6,6 +6,7 @@
 use Illuminate\Console\Scheduling\Schedule;
 use App\Http\Middleware\SetUserLanguage;
 use App\Http\Middleware\CheckPermission;
+use Laravel\Sanctum\Http\Middleware\CheckForAnyAbility;
 use App\Tasks\DeleteExpiredTokens;
 
 
@@ -21,6 +22,7 @@
         $middleware->append(SetUserLanguage::class);
         $middleware->alias([
             'permission' => CheckPermission::class,
+            'ability' => CheckForAnyAbility::class,
         ]);
     })
     ->withSchedule(function (Schedule $schedule) {

+ 1 - 1
routes/api.php

@@ -9,7 +9,7 @@
     }
 });
 
-Route::middleware(['auth:sanctum'])->group(function () {
+Route::middleware(['auth:sanctum', 'ability:access'])->group(function () {
     $authRoutes = glob(__DIR__ . "/authRoutes/*.php");
     foreach ($authRoutes as $authRoute) {
         Route::group([], $authRoute);