如何在 Laravel 中为自定义注册表单设置自定义自动登录

这是 laravel 中的用户注册自定义表单。


public function register(RegisterTalentRequest $request) 

{

    $talent = $this->create($request->except('_method', '_token'));


    //up till here the registration values are successfully saved in the 

    //database table, but it's not logged in following the below code

    //and is directing to the custom login page.


    Auth::attempt(['email' => $request->email, 'password' => $request->password]);


    return redirect()->route('dashboard'); //directing to login and not dashboard

}


茅侃侃
浏览 135回答 2
2回答

慕田峪9158850

默认情况下,Auth外观(或auth()辅助函数)将使用文件guard中的默认设置config/auth.php。如果您想在登录或注册方法中使用不同的保护,您需要显式设置它:Auth::guard('guard-name')->attempt(...);或者,当使用authorguest中间件时,您可以传递您想要使用的保护,例如 (auth:guard-name或guest:guard-name) 这会将默认保护设置guard-name为请求的其余部分,因此您不需要显式设置它。

牧羊人nacy

我假设在您注册时它返回了一个用户。那么你可以使用这个Auth::login($user);
打开App,查看更多内容
随时随地看视频慕课网APP