How do I prevent inactive/suspended users from logging in to dashboard?

Hello, I have a column status which takes 3 values active, inactive, suspended. I would like to only let users login which have status = 'active'. Please let me know how do I make it work? Thank you
14 Replies
Michal Čabala
Michal Čabala7mo ago
Recently i made this, but right now on smsrtphone, so remind me or PM me, i will share a code for you. Basically, implenent softdeletes to your user model and make custom login page with custom login behaviour/credentials. It works perfectly. It also works if you have made it as you described, but softdeletes works better in my opinion.
mohdaftab
mohdaftab7mo ago
@Michal Čabala thanks for the reply. So we can't do it using the default login page? I mean do we really have to make custom login behavior/credentials for this to work ?
Michal Čabala
Michal Čabala7mo ago
As I am total noob, i can't answer for that. But according to docs, you have to make a custom login page and function to customize login process in it. It's not so difficult and it works great for me. I don't have time to test it if it works with softdeletes without having custom login page and process. But recently i've made login with personal number instead of email and also check that user is active or not by softdeletes and it works perfectly
mohdaftab
mohdaftab7mo ago
Ok thank you so much, I am also a noob which is why asking these questions, I am sure there is a very easy way to do that in Filamentphp and will wait for someone in the team for help.
Michal Čabala
Michal Čabala7mo ago
No problem. Just remind me or PM, as i've said to not forget about you. I've tested it right now...if you want to avoid custom login page and process, just implement softdeletes to your user model, if you are happy with that solution. If you need more states, you need custom login page and process. It just depends on what you need.
DariusIII
DariusIII7mo ago
I have done it much simpler, through canAccessPanel function:
if ($this->is_disabled) {
return false;
}
if ($this->is_disabled) {
return false;
}
You can alter and expand that to cases you need.
Michal Čabala
Michal Čabala7mo ago
Also an option, but softdeletes works fine for me and it's also super easy. No need to have extra fields for user state (if you don't need some multiple conditions) and you can easily restore user.
DariusIII
DariusIII7mo ago
@mohdaftab already has three states, so nothing needs to be added, user will be unable to access any panel and will receive message These credentials do not match our records. I don't think it can be simpler than that 🙂
Michal Čabala
Michal Čabala7mo ago
Just thinking why three states if you just want to prevent inactive user to login. For that it's easier to implement softdeletes and it works out of the box, no extra coding needed. Yeah, i'm doing it same way, but the more i'm digging deeper to Laravel and Filamenty the more i realize that some things can be done much easier way. And like I've said, it just depends on needs. If you just need to "disable" user, use softdeletes. Or make a Custom login page and credentials and customize it your way.
DariusIII
DariusIII7mo ago
Now that i think about it a bit more, why would you want to make record marked as deleted? Setting a state for user like inactive, banned, disabled...you name it, is much more efficient and makes more sense.
Michal Čabala
Michal Čabala7mo ago
I think softdeletes are the same thing like user status, if you know how softdeletes are working. And if you don't need more states. And you also don't need extra coding for that, that behaviour just works out of the box. Just curious what's the difference between inactive and suspended? Just mark user as inactive/suspended and that's it.
mohdaftab
mohdaftab7mo ago
Well the active/inactive and suspended are for our system to know which users have been inactive for given time and we automatically mark them as inactive and suspended works when we instantly want to suspend some users from accessing the dashboard. this works but the main issue here is the message to show to user for different status, like if the credentials are wrong we show them wrong credential message, if the account is suspended we show them a message that says your account has been suspended, please contact us for more details something like that
DariusIII
DariusIII7mo ago
In that case, custom login logic is a best way.
mohdaftab
mohdaftab7mo ago
true, thank you so much.