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.