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

garethdaine's avatar

Redirect After Registration/Subscription

Hey Folks,

When registering for a subscription/plan, after successful registration it should log in the user and redirect to the dashboard, but it just redirects to the log in screen. Any ideas?

Thanks

0 likes
12 replies
EventFellows's avatar

Normally spark redirects to /home when logged in (or from some Controllers to /settings. If you are being redirected to the /login it does sound like the redirect of the auth middleware because user was not logged in.

But hard to say without seening anything in code.

garethdaine's avatar

@EventFellows Thanks, what code would you like to see.

In my routes.php I'm listening for 'home' and redirecting to dashboard using:

Route::get('home', function() {
    return redirect()->route('dashboard');
});
EventFellows's avatar

So I assume you have a dashboard route defined? Is your home route wrapped in any Route::group?

From which user action exactly does the wrong redirect happen and what plan is choosen (free vs. paid)?

garethdaine's avatar

Hi @EventFellows ,

Yes, a dashboard route is defined within a Route Group that uses ['domain' => config('app.url')]. The dashboard route simply points to '/', which will go to https://app.example.com.

The wrong redirect happens directly after someone subscribes to a plan. As far as I'm aware, once subscribed and the user is created, it should log them in and redirect them to /home (or the main dashboard of the app).

The plans are paid plans. Thanks.

EventFellows's avatar

@garethdaine You are right, redirect to dashboard should be the default setting.

I am afraid you have no other option than following the registration flow through the application. My bet would be that is has something to do with the Auth Middleware...

One other ideas that comes to mind would be to run route:list to ensure that web middleware is only applied max 1 time per route (there were some changes during 5.1 or 5.2 versions, now it is applied by default again.)

1 like
garethdaine's avatar

@EventFellows Annoying, as after registration and successful payment, there is no success message or any indication that the payment was successful. This could be very confusing to users and cause support issues.

Do you think using a middleware would help here, as in capturing the user and performing a redirect to a thank you page, or logging them in to the dashboard?

garethdaine's avatar

This should do it:

public function handle(UserSubscribed $event)
{
    Auth::attempt($event->user);

    return redirect()->route('dashboard');
}
EventFellows's avatar

I would not go for middleware here. Technically speaking there is no successful payment on the $user request and this probably is also the reason why there is not message (I suppose).

The payment is only successful after the webhook from stripe is received and handled (it does send out an invoice email to the user by default which should limit your concerns about customer inquiries). On this payment.succeeded webhook from stripe the invoice entry in your DB is created which I catch to fire an SubscriptionInvoicePaid event (which always fires on payment every month, also on renewal) in my AppServiceProvicer like this:

        // Fire Event about new invoice being created
        LocalInvoice::created(function ($localInvoice) {
            SystemEvent::fire(new SubscriptionInvoicePaid($localInvoice));
        });

This might also be a good setup for you.

1 like
garethdaine's avatar
garethdaine
OP
Best Answer
Level 4

Hey @EventFellows ,

So I did it using an event listener:

<?php

namespace App\Listeners\Subscription;

use Laravel\Spark\Events\Subscription\UserSubscribed;
use Auth;

class LoginAndRedirect
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserSubscribed  $event
     * @return void
     */
    public function handle(UserSubscribed $event)
    {
        Auth::login($event->user);

        return redirect()->route('dashboard');
    }
}

Please or to participate in this conversation.