首页 > 解决方案 > 参数 1 传递给 Illuminate\Auth\SessionGuard::login()

问题描述

我已经尝试了类似问题的答案中的所有内容,但我仍然陷入此错误,请帮助我。我知道它来自哪里,但我不知道如何解决这个问题。

<?php




use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = '/verify';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'mobile' => ['required', 'min:10'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param array $request
 * @return string
 * @throws \Twilio\Exceptions\ConfigurationException
 */
protected function create(array $data)
{
    /* Get credentials from .env */
    $token = getenv("TWILIO_AUTH_TOKEN");
    $twilio_sid = getenv("TWILIO_SID");
    $twilio_verify_sid = getenv("TWILIO_VERIFY_SID");
    $twilio = new Client($twilio_sid, $token);
    $twilio->verify->v2->services($twilio_verify_sid)
        ->verifications
        ->create('+977'.$data['mobile'], "sms");
    User::create([
        'name' => $data['name'],
        'mobile' => $data['mobile'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    return redirect()->route('verify')->with('mobile', $data->mobile);

}
protected function verify(Request $request)
{
    $data = $request->validate([
        'otp' => ['required', 'numeric'],
        'mobile' => ['required', 'string'],
    ]);
    /* Get credentials from .env */
    $token = getenv("TWILIO_AUTH_TOKEN");
    $twilio_sid = getenv("TWILIO_SID");
    $twilio_verify_sid = getenv("TWILIO_VERIFY_SID");
    $twilio = new Client($twilio_sid, $token);
    $verification = $twilio->verify->v2->services($twilio_verify_sid)
        ->verificationChecks
        ->create($data['otp'], array('to' => $data['mobile']));
    if ($verification->valid) {
        $user = tap(User::where('mobile', $data['mobile']))->update(['isVerified' => true]);
        /* Authenticate user */
        Auth::login($user->first());
        return redirect()->route('dashboard')->with(['message' => 'Phone number verified']);
    }
    return back()->with(['mobile' => $data['mobile'], 'error' => 'Invalid verification code entered!']);
}

}

这是我的路线:

    <?php


Route::get('/', 'HomeController@index');

Auth::routes();

Route::get('/verify', function () {
    return view('backend.verify');
})->name('verify');

Route::get('/', function () {
    return view('auth.register');
})->name('register');

Route::get('/dashboard', 'HomeController@index')->name('home');
Route::post('/verify', 'RegisterController@verify')->name('verify');

Route::group(['middleware' => 'auth'], function () {
    Route::resource('user', 'UserController', ['except' => ['show']]);
    Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController@edit']);
    Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController@update']);
    Route::put('profile/password', ['as' => 'profile.password', 'uses' => 'ProfileController@password']);

});

如有必要,我可以添加代码谢谢。我认为问题在于重定向到路由验证的创建函数,错误是: 在此处输入图像描述

标签: laraveltwilio-api

解决方案


创建方法必须返回 $user 对象。您不能从 RegisterController 的 create 方法重定向到任何路由。有一些方法可以处理这个问题。

像这样覆盖 web.php 中的默认注册路由。

Route::post('/register', 'Auth\RegisterController@created')->name('register');

然后在您的注册控制器中将参数从类型数组更改为 Illuminate\Http\Request 像这样。

protected function created(Request $data)
{
/* Get credentials from .env */
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio_sid = getenv("TWILIO_SID");
$twilio_verify_sid = getenv("TWILIO_VERIFY_SID");
$twilio = new Client($twilio_sid, $token);
$twilio->verify->v2->services($twilio_verify_sid)
->verifications
->create('+977'.$data->mobile, "sms");
User::create([
  'name' => $data->name,
  'mobile' => $data->mobile,
  'email' => $data->email,
  'password' => Hash::make($data->password),
]);

return redirect()->route('verify')->with('mobile', $data->mobile);

}

希望这能解决您的问题。:)


推荐阅读