在带有惯性js/vuejs2/fortify的Laravel 8应用程序中,我尝试从vue组件运行带有axios请求的用户注册方法(我需要使用确认码进行注册):
makeRegisterStep1() {
console.log('makeRegisterStep1 this.registerForm::')
console.log(this.registerForm)
Window.axios.post('/register', this.registerForm)
.then(resp => {
console.log('makeRegisterStep1 resp::')
console.log(resp)
...
})
.catch(
function (error) {
console.error(error)
}
)
}, // makeRegisterStep1() {
但是我出错了
POST http://127.0.0.1:8000/register 500 (Internal Server Error)
我在日志文件中看到:
[2021-12-28 10:18:51] local.ERROR: Laravel\Fortify\Http\Controllers\RegisteredUserController::store(): Argument #2 ($creator) must be of type Laravel\Fortify\Contracts\CreatesNewUsers, App\Actions\Fortify\CreateNewUser given, called in /mnt/_work_sdb8/wwwroot/lar/photographers/li/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 {"exception":"[object] (TypeError(code: 0): Laravel\\Fortify\\Http\\Controllers\\RegisteredUserController::store(): Argument #2 ($creator) must be of type Laravel\\Fortify\\Contracts\\CreatesNewUsers, App\\Actions\\Fortify\\CreateNewUser given, called in /mnt/_work_sdb8/wwwroot/lar/photographers/li/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 at /mnt/_work_sdb8/wwwroot/lar/photographers/li/vendor/laravel/fortify/src/Http/Controllers/RegisteredUserController.php:51)
[stacktrace]
#0 /mnt/_work_sdb8/wwwroot/lar/photographers/li/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): Laravel\\Fortify\\Http\\Controllers\\RegisteredUserController->store()
#1 /mnt/_work_sdb8/wwwroot/lar/photographers/li/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction()
#2 /mnt/_work_sdb8/wwwroot/lar/photographers/li/vendor/laravel/framework/src/Illuminate/Routing/Route.php(262): Illuminate\\Routing\\ControllerDispatcher->dispatch()
I modified app/Providers/FortifyServiceProvider.php, but not Create user part :
<?php
namespace App\Providers;
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Fortify;
class FortifyServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
public function boot()
{
Fortify::loginView(function () {
return view('auth.login', [] );
});
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('email', $request->email)->first();
$request = request();
if ($user && Hash::check($request->password, $user->password)) {
if ( $user->status === 'A') {
return $user;
}
else {
throw ValidationException::withMessages([
Fortify::username() => __("Your account is inactive"),
]);
}
}
});
Fortify::registerView(function () {
return view('auth.register', [] );
});
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
RateLimiter::for('login', function (Request $request) {
return Limit::perMinute(5)->by($request->email.$request->ip());
});
RateLimiter::for('two-factor', function (Request $request) {
return Limit::perMinute(5)->by($request->session()->get('login.id'));
});
}
}
如何修复?
修改块: 没有管道是默认的,并检查我看到的路线:
php artisan route:list
| Illuminate\Auth\Middleware\EnsureEmailIsVerified |
| | GET|HEAD | register | register | Laravel\Fortify\Http\Controllers\RegisteredUserController@create | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | POST | register | generated::qWOIdxP9ML8fo3yR | Laravel\Fortify\Http\Controllers\RegisteredUserController@store | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | GET|HEAD | test | test | App\Http\Controllers\HomeController@test | web |
| | DELETE | user | current-user.destroy | Laravel\Jetstream\Http\Controllers\Inertia\CurrentUserController@destroy | web |
| | | | | | App\Http\Middleware\Authenticate |
| | | | | | Illuminate\Auth\Middleware\EnsureEmailIsVerified |
| | GET|HEAD | user/confirm-password | password.confirm | Laravel\Fortify\Http\Controllers\ConfirmablePasswordController@show
app/Models/User.php 的标头:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Spatie\Permission\Traits\HasRoles;
use Spatie\Permission\Traits\HasPermissions;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class User extends Authenticatable implements HasMedia //, MustVerifyEmail
{
use HasRoles;
use HasPermissions;
use HasFactory;
use Notifiable;
use TwoFactorAuthenticatable;
use InteractsWithMedia;
protected $fillable
= [
'name',
'email',
'status',
'password'
];
最初那是基于 bootstrap/jquery 的 laravel 8 管理员入门工具包,没有 Fortify/Jetstream。我使用我需要的登录服务手动安装了 Fortify。
我还在我的用户播种器中使用 CreateNewUser 类: :
app(CreateNewUser::class)->create([
'id' => 1,
'name' => 'Admin',
'email' => 'admin@site.com',
'password' => '11111111',
'status' => 'A',
], true, [PERMISSION_APP_ADMIN]);
我错过了什么?
谢谢!