Easy Verification with Laravel and VerifierClass
How many times have you come across a situation where you have a system that requires user confirmation through email/SMS? Well, I can count at least a dozen just off the top of my head where I had to manually implement a verification route and controller (along with the logic involved in verification) every time.
In this post, I will walk you through the usage of a nice class I find quite useful and might help you avoid this kind of legwork going back and forth between models and doing a lot of SELECT and DELETE rows. This class leverages many helper classes that you may find useful as well, but this is kind of off-topic.
BACKGROUND
WHAT KIND OF SYSTEM?
I assume that you use a classical verification system that involves creating a temporary user table and model and a permanent user table and model. If the following is the kind of set up you have, I think you’re going to love VerifierClass.
The temporary model (let’s call it TempUser) will store everything about the user plus a confirmation_code column, which is usually a random hashed string that is used to uniquely identify a temporary user.

The permanent model (let’s call it PermanentUser), on the other hand, holds the same user info but after confirmation.

So what happens is:
A- The user fills in a form and hits submit.
B- An email or an SMS with the confirmation code or link is sent to the user.
C- The user visits this link, which may look something like this:
https://www.mysite.com/user/confirm?some_identifier=some_identifier&confirmation_code=As1d5zig80mc7ae
N.B. You may also use a form and a POST request where the user provides the confirmation code and/or any other identifier(s), it doesn’t really matter which way you choose.
D- This route should point to a method inside a controller (let’s call it VerificationController) that does the following:
- Deletes the temporary user row from the table associated with the TempUser model.
- Catches the deleted data.
- Insert it into the table associated with the PermanentUser model.
N.B. Your route might look something like this:
Route::get('/user/confirm', 'VerificationController@verification');SO.. WHERE DOES THE VerifierClass COME IN?
See that part in bold? That’s where the VerifierClass comes in and allows you to do something like this inside your controller! Take a look:
$identifiers = ['some_identifier' => $request->some_identifier,
'confirmation_code'=> $request->confirmation_code];
$verifier->setIdentifiers($identifiers);
$verifier->verify($temp_user, $permanent_user);
return view('your_confirmation_view');(Note that you can add as much identifiers as you want.)
USAGE
First: Leverage the SuperModel class..
This is quite simple. Just make your model extend the SuperModel class, instead of the Model class. Well, I know. That name, it does sound pompous, but don’t worry I’m not rewriting Laravel or anything.
SuperModel is actually nothing but a very ‘thin’ wrapper around the awesome Laravel Model class. It extends Laravel’s Model, adds a few methods and that’s it. It doesn’t override any Model method.
So you just need to replace:
class TempUser extends Model
with:
class TempUser extends SuperModel
and of course import the SuperModel class at the top of your model file, so replace:
use Illuminate\Database\Eloquent\Model;
with:
use App\SuperModel\SuperModel;
Second: Use your VerifierClass..
Just pass your VerifierClass, your TempUser and Permanent user as dependencies inside your controller’s method, and Laravel IoC container will take care of instantiating all of them for you.
class VerificationController extends Controller {public function verifier(Request $request, VerifierClass $verifier, TempUser $temp_user, PermanentUser $permanent_user) {Then, as I showed you at the beginning, you can simply pass your identifiers and call verify() on your verifier instance and we can call it a day!
The verify() method takes your TempUser instance as its first argument and your PermanentUser as its second argument.
$identifiers = ['some_identifier' => $request->some_identifier,
'confirmation_code'=> $request->confirmation_code];
$verifier->setIdentifiers($identifiers);
$verifier->verify($temp_user, $permanent_user);
return view('your_confirmation_view');And of course, don’t forget to reference our VerifierClass at the top of your controller file:
use App\VerificationBox\Verifier\VerifierClass as Verifier;
And then, take a breath, return your confirmation view, redirect or just relax. We’re done.
return view('your_confirmation_view');