在laravel 5.4.12中成功注册后如何自动登录?

时间:2017-03-16 07:05:16

标签: php laravel laravel-5.4

我想在注册成功后自动登录并重定向到编辑个人资料页面。我尝试了以下代码,但没有按我的意愿工作

class RegisterController extends Controller
{

use RegistersUsers;

protected $redirectTo = '/edit_profile';


public function __construct()
{
    $this->middleware('guest');
}

public function createUser(Request $request)
{


    $this->validate($request , [
        'firstName'    => 'required',
        'lastName'     => 'required',
        'displayName'  => 'required',
        'email'        => 'required |email',
        'password'     =>'required ',
        'gender'       =>'required',
        'day'          =>'required|max:2',
        'month'       =>'required|max:2',
        'year'       =>'required|max:4',
    ]);
    $email=$request->input('email');
    $password=$request->input('paasword');
    $dob=$request->input('day').'-'.$request->input('month').'-'.$request->input('year');
    $request->request->add(['dob'=>$dob]);
    $request->request->add(['password'=>bcrypt($request->input('password'))]);
    $data = User::create($request->except(['_token', 'submit', 'day','month','year', 'confirm_password' ,'dayInWeek']));

    Auth::login($data);

 }

}

路线

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


    Route::group( ['middleware' => 'auth' ], function()
    {
     Route::get('main', 'PageController@main');

     Route::get('browse', 'PageController@browse');

     Route::get('pickngo', 'PageController@pickngo');

     Route::get('edit_profile', 'PageController@profile');
    }

3 个答案:

答案 0 :(得分:2)

只需修改您的身份验证登录,就像

一样
if(Auth::login($data))   {
   return redirect('/edit_profile');
   }
   else
  {
   return redirect()->back();
  }

答案 1 :(得分:2)

通过传递用户ID来使用Laravel loginUsingId($ id)函数。

$data = User::create($request->except(['_token', 'submit', 'day','month','year', 'confirm_password' ,'dayInWeek']));

Auth::loginUsingId($data->id);

答案 2 :(得分:1)

来自文档的Scren。 您只需要在LoginController中登录redirectTo属性或方法后添加。 如果你使用laravel的make:auth,P.S.it将会有效。 Screen from documentation