F
Filamentβ€’5mo ago
atom

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
atom
atomβ€’5mo ago
Any help with this please???
Dennis Koch
Dennis Kochβ€’5mo ago
You should add a Middleware that always redirects them to a page of your choice when they aren't approved.
atom
atomβ€’5mo ago
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.php
krekas
krekasβ€’5mo ago
you are redirecting to a route by name
atom
atomβ€’5mo ago
Yes.
krekas
krekasβ€’5mo ago
but your routes name isn't like that
atom
atomβ€’5mo ago
Kindly make me understand better, Please
krekas
krekasβ€’5mo ago
learn laravel basics please your have your route in the routes/web.php right?
atom
atomβ€’5mo ago
Yes.
krekas
krekasβ€’5mo ago
show it
atom
atomβ€’5mo ago
Route::get('/pending-approval', [PendingApprovalController::class, 'index'])->name('pending-approval');
krekas
krekasβ€’5mo ago
so it has a name now check what name you gave in the middleware are they 100% the same?
atom
atomβ€’5mo ago
Oh! I think I understand now! I missed such a tiny detail. Thanks for helping me see that.
Dennis Koch
Dennis Kochβ€’5mo ago
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?
atom
atomβ€’5mo ago
Oh, no offense taken at all! ...and yes! It definitely helped. Thank you.
keenminded
keenmindedβ€’5mo ago
@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:
public function handle(Request $request, Closure $next): Response
{
abort_if(! Auth::user(), 403)

if (Auth::user()->isNotYetApproved()) {
return redirect()->route('pending-approval');
}

return $next($request);
}
public function handle(Request $request, Closure $next): Response
{
abort_if(! Auth::user(), 403)

if (Auth::user()->isNotYetApproved()) {
return redirect()->route('pending-approval');
}

return $next($request);
}
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.
Want results from more Discord servers?
Add your server
More Posts
Controlling edit access to resource form based on column dataI have an resources, i wanna limit edit access to data, the condition is if column a in that data trI'm trying to add a download button in a extended Custom FieldHello, I'm trying to add a download button for a media that use Spatie Media Library, I have the funHow to make table update after a livewire variable update outside of filamentI have a filament table inside a livewire component, with a search input and a button with wire:clicBringing and Saving Previous Selections When Editing Page with 'Select' FieldHello good afternoon, I needed help On one of my screens I have a Select field where your options Display TextColumn based on model functionWhat is the best apporach for displaying a TextColumn in my table that references a function on my mAction in panels::sidebar.footer opens modal in sidebar instead of center of screenHi all I've added an action to the bottom of the sidebar using the `panels::sidebar.footer` render Can u disable that everything is by default added with a extra S?How can I disable that everything is standard added with a S? In my database the table name is sameResize image in SpatieMediaLibraryFileUploadHello, I find Spatie's package excellent, especially for defining conversions to resize uploaded imaTest failingMy code - ```test('user cannot view create inspection form if they do not have permission', functionCan we get the inserted $record in the afterCreate() when a record is newly added?The purpose is when a new record gets created, I need to add a default relation record as well. Is t