I am using Larave Jetstream livewire and I want to modify the login.
login from having a hidden input field "is_admin" with an initial value of 1
when a user submits login form backend check with is_admin = 1 of the database table field
table structure: name, email, password, is_admin
is_admin = 0 or 1
I want to check the is_admin flag, If provided credentials match email, password, and is_admin=1 then only the user can log.
I think you intend to customise authentication. Since you are using Jetstream most probably you are using Fortify out of the box (by default).
There are 2 ways. These are to help you sent extra data from your authentication form and not just the hidden field. However, if the is_admin field is default, then I don't think you should add it as hidden field. You could be compromised.
Example 1. Edit the User.php model and add a boot method
Fortify::authenticateUsing
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Fortify;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Fortify::authenticateUsing(function (Request $request) {
$is_admin = $request->is_admin ?? 1;
// Your code here ... Example for login in below
$user = User::where('email', $request->email)->first();
if ($user &&
Hash::check($request->password, $user->password)) {
return $user;
}
});
// ...
}
https://laravel.com/docs/8.x/fortify#customizing-user-authentication
Example 2. Also you may edit the app/Providers/FortifyServiceProvider.php
And in the boot method add
Fortify::authenticateUsing(function (Request $request){
$is_admin = $request->is_admin ?? 1;
// Other codes here
});
Also unless I did not understand you correctly but just want to be sure the user is admin before allowing login, then you could tweak the auth code in Example 1 to do that.
Fortify::authenticateUsing(function (Request $request) {
$user = User::where(['email' => $request->email, 'is_admin' => 1])->first();
if ($user && Hash::check($request->password, $user->password)) {
return $user;
}
});