Implement Pending Approval Page For Newly Signed up Users
Hi guys, I have been working diligently on the implementation of a feature that will display a pending approval page whenever a new user signs up and then notify the admin of a newly signed up user. The aim is to enhance the onboarding process and introduce an approval step before users are allowed access to the dashboard.
As you might've guessed, I have two panels readily configured: one for the admin user and the other for the regular user. I've gone through the docs in search of a clue that'll help with this, but unfortunately, couldn't find one that helps.
ADDITIONAL INFORMATION:
To achieve this, I've added a "user_approved_at" column in the Users table which is false by default. The idea is to set it to true once an admin approves the new user... and until a user is approved would he gain access to the user's dashboard. This will be some sort of middleware actively checking if the user attempting to login has his "user_approved_at" set to true, if not, he keeps getting an unauthorized message saying that "Your request is currently being reviewed by the administrators. Kindly keep an eye on your email".
CHALLENGE:
I'm struggling with how to achieve the "Pending approval" page after user registration without redirecting them to the dashboard automatically. Please help.
16 Replies
Any help with this please???
You should add a Middleware that always redirects them to a page of your choice when they aren't approved.
Yes, I've tried that. This is what my middleware "CheckedApprovedUser" looks like
public function handle(Request $request, Closure $next): Response
{
if (Auth::user() && Auth::user()->user_approved_at) {
return $next($request);
}
return redirect()->route('/pending-approval');
}
However, I'm getting a "Route [/pending-approval] not defined.". But when I manually navigate to "localhost:8000/pending-approval", it renders my view.
My view is located in the resources/views/pending-approval.blade.phpyou are redirecting to a route by name
Yes.
but your routes name isn't like that
Kindly make me understand better, Please
learn laravel basics please
your have your route in the
routes/web.php
right?Yes.
show it
Route::get('/pending-approval', [PendingApprovalController::class, 'index'])->name('pending-approval');
so it has a name
now check what name you gave in the middleware
are they 100% the same?
Oh! I think I understand now!
I missed such a tiny detail.
Thanks for helping me see that.
No need to tell people "to learn Laravel basics" when it might just be a typo π
@atom I guess this is solved then? You realized that you mixed route path and route name?
Oh, no offense taken at all!
...and yes! It definitely helped. Thank you.
@atom One thing I would recommend, for consistency with all other middleware and to ease your pains in debugging in the future, is always to keep the line return $next($request) as last in your middleware. This is the line that ties your middleware to the next one on the list, and as such, it is a good idea to keep it unchanged at the end of the handle method. Then, you would reverse your if statement to check for the absence of permissions.
Also, for consistency and readability, change the attribute to a method. This way, you can easily replace or extend the logic in your model rather than hunt down all references to user_approved_at. It also reads better, but that's more of a personal preference than a convention.
So, I would rewrite your middleware like so:
These small changes and consistency in the overall code base will make you, or anyone else working on your code, appreciate your efforts in the long run when you have to debug or test something. I hope this helps, and best of luck in your programming voyages.