Memo cách thay đổi màn hình Laravel Jetstream Login với username và company_name.
Thêm company_name vào Model và migration
App/Models/User.php
class User extends Authenticatable
{
//...
protected $fillable = [
'name',
'company_name',
'email',
'password',
];
}
database\migrations\2014_10_12_000000_create_users_table.php
return new class extends Migration
{
//...
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('company_name');
//...
});
}
}
Sửa FortifyServiceProvider
app/Providers/FortifyServiceProvider.php
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Fortify;
use Illuminate\Support\Facades\Log;
class JetstreamServiceProvider extends ServiceProvider
{
//...
public function boot()
{
$this->configurePermissions();
Jetstream::deleteUsersUsing(DeleteUser::class);
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('name', $request->name)
->where('company_name', $request->company_name)
->first();
if ($user &&
Hash::check($request->password, $user->password)) {
return $user;
}
});
}
}
Sửa config
config/fortify.php
//...
/*
|--------------------------------------------------------------------------
| Username / Email
|--------------------------------------------------------------------------
|
| This value defines which model attribute should be considered as your
| application's "username" field. Typically, this might be the email
| address of the users but you are free to change this value here.
|
| Out of the box, Fortify expects forgot password and reset password
| requests to have a field named 'email'. If the application uses
| another name for the field you may define it below as needed.
|
*/
'username' => 'company_name',
'email' => 'name',
//...
Sửa login blade
resources/views/auth/login.blade.php
//...
<form method="POST" action="{{ route('login') }}">
@csrf
<div>
<x-jet-label for="company_name" value="{{ __('Company Name') }}" />
<x-jet-input id="company_name" class="block mt-1 w-full" type="text" name="company_name" :value="old('company_name')" required autofocus />
</div>
<div>
<x-jet-label for="name" value="{{ __('User Name') }}" />
<x-jet-input id="name" class="block mt-1 w-full" type="text" name="name" :value="old('name')" required autofocus />
</div>
<div class="mt-4">
<x-jet-label for="password" value="{{ __('Password') }}" />
<x-jet-input id="password" class="block mt-1 w-full" type="password" name="password" required autocomplete="current-password" />
</div>
//...
</form>
Bình luận