May Sale! All accounts are 40% off this week.

vincent15000's avatar

Fortify profile update and redirect ?

Hello,

I use Fortify to manage the authentication.

I have to update the user profile, so I try to use the Fortify method update, but I'm not able to redirect the user after the update action. I have added a return redirect()->route(), but it doesn't work.

Any idea ?

Thanks for your help ;).

Vincent

0 likes
6 replies
vincent15000's avatar

Yes ok but is it possible to overload the method to have another redirection ?

ritchy's avatar

@fhsdmh @vincent15000 if you want to redirect somewhere after an update you can try what i have tried:

if you write "php artisan route:list" in a terminal, you will see that the route "user-profile-information.update" (the one you called in your POST form) redirects on the method "update" in ProfileInformationController (vendor/laravel/fortify/src/Http/Controllers/ProfileInformationController.php).

By defalut, it returns the same page with the updtated informations.

You just have to write "return redirect('/home');" or wherever you want to go

<?php

namespace Laravel\Fortify\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
use Laravel\Fortify\Fortify;

<?php

namespace Laravel\Fortify\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
use Laravel\Fortify\Fortify;

class ProfileInformationController extends Controller
{
    /**
     * Update the user's profile information.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Laravel\Fortify\Contracts\UpdatesUserProfileInformation  $updater
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,
                           UpdatesUserProfileInformation $updater)
    {
        $updater->update($request->user(), $request->all());

        // return $request->wantsJson()
        //             ? new JsonResponse('', 200)
        //             : back()->with('status', Fortify::PROFILE_INFORMATION_UPDATED);
        return redirect('/home');
    }
}


I hope it helps

2 likes
staticBanter's avatar

As of writting this I do not believe there is an 'offical' way of instructing Fortify to redirect you back your User profile after you update your route-model-key (ie: name). I notice there was an update to Fortify to allow for the customiztion of some redirects but I do not believe you can customize all of the Actions as-well; (Related GitHub Issue #77 and PR #298).

What I do is more of a work-around and I would also love a more offical way. Here is the method I use;

  1. Open your App/Actions/Fortify/UpdateProfileInformation.php.
  2. After you are done Updating and Saving your User Model insert this line of code:
// Set the referer header to https://example.com/users/USER/edit
request()->headers->set('referer', route('users.edit', $user));

What this is doing is setting the Referer Header of the current request to the updated user models edit route. This works because Fortify will call for a redirect back to the URL the initiated the update, modifying the referer changes that location and Laravel follows the request like normal. We get directed back to the edit view with our new user model data.

Hope this helps!

Please or to participate in this conversation.