Bài viết Câu hỏi About RongvangIT
profile Pic
0
0

Đăng ngày:

 

Sửa ngày:

474 Lượt xem

Laravel Jetstream: Login bằng username và company_name

LaravelJetstream

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>

Kết quả

Laravel Jetstream Login.png

kiyoshi
Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.

Bình luận

Bài viết chưa có bình luận. Hãy trở thành người bình luận đầu tiên!
Sign up for free and join this conversation.
Sign Up
If you already have a RongvangIT account Login
Danh sách thư mục
Tại sao không đăng ký và nhận được nhiều hơn từ RồngVàngIT ?

Bạn cần đăng nhập để sử dụng chức năng này, cùng hàng loạt các chức năng tuyệt vời khác của RồngVàngIT !

  1. 1. Bạn sẽ nhận được các bài viết phù hợp bằng chức năng theo dõi tag và người dùng.
  2. 2. Bạn có thể đọc lại các thông tin hữu ích bằng chức năng lưu trữ nội dung.
  3. 3. Chia sẻ kiến thức, đặt câu hỏi và ghi lại quá trình trưởng thành của mình cùng RồngVàngIT !
Tạo tài khoản Đăng nhập
profile Pic