Hi_TecH
Hi_TecH

Reputation: 427

Email confirmation using Fortify Laravel

I am using Fortify without Jetstream for authentication. I read in the documentation about email verifycation at the time of registration. But what about confirmation the email address when the user changes the email address? Is it difficult to implement using this package or does it not have a ready-made functionality for this, and I have to do it from scratch?

In user pannel it's possible to change username, email, phone and img(I will add it later):

public function user_update(Request $request){

      $user = Auth::user();

      $user->name = $request->input('name')?$request->input('name'):Auth::user()->name;
      $user->email = $request->input('email')?$request->input('email'):Auth::user()->email;
      $user->phone = $request->input('phone')?$request->input('phone'):(Auth::user()->phone?Auth::user()->phone:NULL);

      $user->update();

      return redirect()->back();
    }

Route:

Route::post('/user-panel/user-update', [AdminController::class, 'user_update'])->name('user_update');

Upvotes: 0

Views: 2011

Answers (1)

Peppermintology
Peppermintology

Reputation: 10210

When you enable the Features::emailVerification() feature in the fortify.php config file you get access to the verified middleware which confirms that a user has verified their email by asserting the email_verified_at field in the database for that user is not null.

So to trigger a requirement for a user to verify a new email if they change theirs, what you want to do is change the value of the email_verified_at field to null if the user alters their email address.

You could write out this logic yourself, however, Fortify actually provides this out of the box. In the app/Actions/Fortify folder you'll see a file named UpdateUserProfileInformation.php which does exactly what you require and implements the entire email verification workflow including generating a new verification email to the user.

You may need to alter this slightly for any changes you have made to the structure of the default user.

Thereafter, all you will need to do in order to use this is configure your <form> appropriately.

<form action="{{ route('user-profile-information.update') }}" method="POST">
    @csrf
    @method("PUT")
</form>

You could use the url if you don't like named routes, but the end result would be the same.

Upvotes: 2

Related Questions