Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sahil1711's avatar

Subscribed middleware isn't working as expected.

So I have middleware which i copied from laravel cashier to check the user is subscribed for certain functions in my application. Here's the middleware.


namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Log;

class Subscribed
{
    /**
     * Handle an incoming request.
     */
    public function handle(Request $request, Closure $next): Response
    {
        Log::info('Checking subscription status for user', ['user_id' => $request->user()->id]);

        // Check if user is not subscribed or if the trial has expired
        if (!$request->user()?->subscribed() || $request->user()?->trialExpired()) {
            $message = !$request->user()->subscribed() ? 'Please subscribe to complete this task.' : 'Your trial period has expired. Please subscribe to continue.';
            Log::info('Access denied', ['user_id' => $request->user()->id, 'reason' => $message]);
            // Add a flash message to the session before redirecting
            $request->session()->flash('alert', $message);
            return redirect()->route('billing');
        }

        return $next($request);
    }
}

However it seem to be not working in my case. I am trying to stop them from creating a new project but it seems to not work at all and redirects them to projects/undefined. How can i resolve this.

    {
        // Define the validation rules for each field
        $rules = [
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'link' => 'nullable|string',
            'statuses' => 'required|array',
            'statuses.*.title' => 'required|string|max:255',
            'statuses.*.slug' => 'required|string|max:255',
            'statuses.*.order' => 'required|integer',
            'statuses.*.tasks' => 'nullable|array',
            'statuses.*.tasks.*.id' => 'nullable|integer',
            'statuses.*.tasks.*.title' => 'required|string|max:255',
            'statuses.*.tasks.*.description' => 'nullable|string',
            'statuses.*.tasks.*.order' => 'nullable|integer',
            'statuses.*.tasks.*.status_id' => 'nullable|integer',
        ];

        // Validate the incoming request data
        try {
            $validatedData = Validator::make($request->all(), $rules)->validate();
        } catch (ValidationException $e) {
            // Handle validation errors
            return response()->json(['errors' => $e->errors()], 422);
        }

        try {
            // Create a new Project instance with the validated data
            $project = auth()->user()->projects()->create([
                'title' => $validatedData['title'],
                'description' => $validatedData['description'],
                'link' => $validatedData['link'],
            ]);

            // Loop through each status and its tasks
            foreach ($validatedData['statuses'] as $status) {
                $newStatus = $project->statuses()->create([
                    'title' => $status['title'],
                    'slug' => $status['slug'],
                    'order' => $status['order'],
                ]);


                // Initialize order for tasks within the status
                $taskOrder = 1;

                // Check if tasks exist for the current status
                if (isset($status['tasks']) && is_array($status['tasks'])) {
                    foreach ($status['tasks'] as $task) {
                        // Create the task with all necessary fields
                        $newTask = $newStatus->tasks()->create([
                            'title' => $task['title'],
                            'description' => $task['description'] ?? null,
                            'order' => $taskOrder++, // Increment order for each task
                            'status_id' => $newStatus->id,
                            'project_id' => $project->id,
                            'user_id' => auth()->id(),
                        ]);
                    }
                }
            }
            // Redirect the user to the projects.index page
            $request->session()->flash('flash.banner', 'Created a new project.');
            $request->session()->flash('flash.bannerStyle', 'success');

            return response()->json(['url' => route('project.show', ['id' => $project->id])]);
        } catch (\Exception $e) {
            return response()->json(['error' => 'An error occurred'], 500);
        }
    }```
0 likes
7 replies
LaryAI's avatar
Level 58

It looks like your middleware is not functioning as expected, and users are being redirected to projects/undefined. This issue might be due to the way the redirection is handled or how the middleware is applied to the routes. Let's go through a few steps to troubleshoot and resolve this issue.

Step 1: Ensure Middleware is Registered

First, make sure that your middleware is registered in the app/Http/Kernel.php file. You can register it either in the $middlewareGroups array or the $routeMiddleware array, depending on how you want to apply it.

// app/Http/Kernel.php

protected $routeMiddleware = [
    // Other middleware
    'subscribed' => \App\Http\Middleware\Subscribed::class,
];

Step 2: Apply Middleware to Routes

Next, ensure that the middleware is applied to the routes where you want to enforce the subscription check. For example, if you want to apply it to the route that handles project creation:

// routes/web.php

Route::group(['middleware' => ['auth', 'subscribed']], function () {
    Route::post('/projects', [ProjectController::class, 'store'])->name('project.store');
});

Step 3: Debug Middleware Logic

Add some additional logging to ensure that the middleware is being executed and to check the values of the conditions.

// app/Http/Middleware/Subscribed.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Log;

class Subscribed
{
    /**
     * Handle an incoming request.
     */
    public function handle(Request $request, Closure $next): Response
    {
        Log::info('Checking subscription status for user', ['user_id' => $request->user()->id]);

        $user = $request->user();
        $isSubscribed = $user ? $user->subscribed() : false;
        $trialExpired = $user ? $user->trialExpired() : true;

        Log::info('Subscription status', [
            'isSubscribed' => $isSubscribed,
            'trialExpired' => $trialExpired,
        ]);

        // Check if user is not subscribed or if the trial has expired
        if (!$isSubscribed || $trialExpired) {
            $message = !$isSubscribed ? 'Please subscribe to complete this task.' : 'Your trial period has expired. Please subscribe to continue.';
            Log::info('Access denied', ['user_id' => $request->user()->id, 'reason' => $message]);
            // Add a flash message to the session before redirecting
            $request->session()->flash('alert', $message);
            return redirect()->route('billing');
        }

        return $next($request);
    }
}

Step 4: Check Redirection Logic

Ensure that the redirection logic in your controller is correct. Specifically, check the URL being returned in the JSON response.

// Your controller method

public function store(Request $request)
{
    // Your validation and project creation logic

    try {
        // Create a new Project instance with the validated data
        $project = auth()->user()->projects()->create([
            'title' => $validatedData['title'],
            'description' => $validatedData['description'],
            'link' => $validatedData['link'],
        ]);

        // Loop through each status and its tasks
        foreach ($validatedData['statuses'] as $status) {
            $newStatus = $project->statuses()->create([
                'title' => $status['title'],
                'slug' => $status['slug'],
                'order' => $status['order'],
            ]);

            // Initialize order for tasks within the status
            $taskOrder = 1;

            // Check if tasks exist for the current status
            if (isset($status['tasks']) && is_array($status['tasks'])) {
                foreach ($status['tasks'] as $task) {
                    // Create the task with all necessary fields
                    $newTask = $newStatus->tasks()->create([
                        'title' => $task['title'],
                        'description' => $task['description'] ?? null,
                        'order' => $taskOrder++, // Increment order for each task
                        'status_id' => $newStatus->id,
                        'project_id' => $project->id,
                        'user_id' => auth()->id(),
                    ]);
                }
            }
        }

        // Redirect the user to the projects.index page
        $request->session()->flash('flash.banner', 'Created a new project.');
        $request->session()->flash('flash.bannerStyle', 'success');

        return response()->json(['url' => route('project.show', ['id' => $project->id])]);
    } catch (\Exception $e) {
        return response()->json(['error' => 'An error occurred'], 500);
    }
}

Step 5: Test the Middleware

Finally, test the middleware by trying to create a new project with a user who is not subscribed or whose trial has expired. Check the logs to ensure that the middleware is being executed and that the correct redirection is happening.

By following these steps, you should be able to identify and resolve the issue with your middleware not working as expected.

sahil1711's avatar

@LaryAI It seems after registering it , the application is breaking and there are way too many redirects to billing route

martinbean's avatar
Level 80

but it seems to not work at all and redirects them to projects/undefined

@sahil1711 Well this sounds like a JavaScript issue and not a Laravel issue. As you usually get undefined in URLs when you’re trying to build URLs in JavaScript, but the variable you’re using in the URL is undefined. For example, something like:

if (project) {
    let projectId = project.id
}

let url = `/projects/${projectId}`;
1 like
Snapey's avatar

how/where have you attached the middleware?

1 like

Please or to participate in this conversation.