0

I have a pretty complex Laravel application who creates a one to one polymorphic relationship in a middleware if it does not exists. The fact is that I am unable to retrieve the relationship in the same request (create in middleware, then pass it and retrieve it), I have an error 500 when I try to do this. BUT, if I a make an other request I can retrieve it... When I look into my database, I have the userable_id and type defined, I have no idea where this can probably occur.

My middleware look like this:

public function handle(Request $request, Closure $next): mixed
{
    if (!Storage::disk('cecurity')->exists(config('coffre_fort.ssl_certificate.name')) ||
        !config('coffre_fort.reverse_proxy.username') ||
        !config('coffre_fort.reverse_proxy.password') ||
        !config('coffre_fort.encryption_key')) {
        return response()->json([
            'error' => 'Cecurity is not configured correctly.',
        ], 500);
    } elseif (!Auth::user()) {
        return response()->json([
            'error' => 'You are not authenticated.',
        ], 401);
    }
    if (!Auth::user()->cecurityAccount) {
        try {
            $userType = Auth::user()::class;
            /** @phpstan-ignore-next-line  */
            if ($userType == "App\Models\Admin") {
                $this->cecurityRepository->createAdminUser(Auth::user());
            } else {
                // It enter in this function for creating relationship
                $this->cecurityRepository->createFullCustomer(Auth::user());
            }
        } catch (Exception $e) {
            return response()->json([
                'error' => $e->getMessage(),
            ], 500);
        }
    } elseif (!$this->cecurityRepository->checkConnection()) {
        $this->cecurityRepository->connect(
            Auth::user()->cecurityAccount->cecurity_user_id,
            openssl_decrypt(
                Auth::user()->cecurityAccount->password,
                'AES-256-CBC',
                config('coffre_fort.encryption_key'),
                0,
                (int) Auth::user()->cecurityAccount->encryption_iv
            ),
            Auth::user()->cecurityAccount->coffre_id
        );
    }
    return $next($request);
}

Then it creates the relationship as this (in the createFulCustomer() function):

$user = new Cecurity;
$user->userable_id = $customer->id_customer;
$user->userable_type = Customer::class;

And then pass the middleware to go to listFiles() function in a controller:

public function listFiles(ListFilesRequest $request): mixed
{
    try {
        return $this->cecurityRepository->listFiles(
            $request->get('nbRowByPage'),
            $request->get('pageIndex'),
        );
    } catch (\Exception $e) {
        return response()->json([
            'message' => $e->getMessage()
        ], 500);
    }
}

Just after the middleware has passed, my database is completed (Cecurity table related):

Cecurity table related

dolor3sh4ze
  • 925
  • 1
  • 7
  • 25

0 Answers0