我想使用 Laravel Fortify 和 Livewire 创建一个非常简单的登录表单。我不想使用 Jetstream,因为它有更多我不需要的功能和我需要的功能。
我在整个应用程序中使用 livewire,并希望将它用于我的登录页面以提供实时即时验证。
我遇到的问题是,如果我wire:model
在输入上使用,我无法提交带有值的表单。
我也不能使用auth()->attempt()
,因为它被 Fortify 劫持了。(至少,我认为是。众所周知,当我使用它时,它会返回false
)
如何将 livewire 与 fortify 一起使用?我需要将数据从 livewire 组件发送到POST /login
.
这是我目前的设置:
FortifyServiceProvider.php
public function boot()
{
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
// Custom Fortify Views =============================
// Login Page
Fortify::loginView(function () {
return view('auth.login');
});
}
我的auth/login.blade.php
(只需使用适当的布局模板调用 livewire 组件)
<x-layouts.auth>
@livewire('auth.login')
</x-layouts.auth>
livewire 组件livewire/auth/login.blade.php
:
<form wire:submit.prevent="login" action="#" method="POST">
<div>
<label for="email">Email address</label>
<input wire:model="email" id="email" type="email" required autofocus>
@error('email'){{ $message }}@enderror
</div>
<div>
<label for="password">Password</label>
<input wire:model.lazy="password" id="password" type="password" required>
@error('password'){{ $message }}@enderror
</div>
<div>
<input wire:model.lazy="remember" id="remember_me" type="checkbox">
<label for="remember_me">Remember me</label>
<a href="#">Forgot your password?</a>
</div>
<div>
<button type="submit">Sign in</button>
</div>
</form>
和我Http/Livewire/Auth/Login.php
的 livewire 组件文件:
class Login extends Component
{
public $email = '';
public $password = '';
public $remember = false;
protected $rules = [
'email' => 'required|email|exists:users,email',
'password' => 'required',
];
/**
* Shows Real time validation for email field
*/
public function updatedEmail() {
$this->validate(['email' => 'email|exists:users,email']);
}
/**
* Logs user in when form is submitted
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Support\MessageBag
*/
public function login()
{
// ******* Need to submit the form to Fortify here?? ******
}
/**
* Renders the view
* @return mixed
*/
public function render()
{
return view('livewire.auth.login')
->layout('layouts.auth');
}
}