简体   繁体   English

在 Laravel 中注册后如何进行身份验证

[英]How to authenticate after register in Laravel

In my project I am trying to validate that registration information and authenticate myself when I register.在我的项目中,我试图在注册时验证该注册信息并验证自己的身份。 But what is happening is that when you register, you send me to the Login page, this happens because when I click on the register button, you send me to a route protected by the Middleware "Auth".但是发生的情况是,当您注册时,您将我发送到登录页面,这是因为当我单击注册按钮时,您将我发送到受中间件“身份验证”保护的路由。 That is, you are not authenticating in the same registration action.也就是说,您不是在同一注册操作中进行身份验证。

protected function create(RequestFormRegister $data)
{
    $userCount = User::where(['email'=> $data['email']])->count();
    if($userCount>0){
        return redirect()->back()->with('error', '¡El correo ya existe!');
    }else{
        $user = User::create([
            'nombres' => $data['nombres'],
            'apellidos' => $data['apellidos'],
            'ni' => $data['ni'],
            'role_id' => 2,
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'password_confirmation' => Hash::make($data['password_confirmation']),
            'remember_token'=> str_random(15),
        ]);

    }

}

With the previous function the system records the data in BD.使用前面的函数,系统将数据记录在 BD 中。 But then I have to go to the login.但后来我必须去登录。 (Thing I don't want) (我不要的东西)

If I use the function that laravel brings by default如果我使用laravel默认自带的功能

 protected function create(array $data)
{
    User::create([
        'nombres' => $data['nombres'],
        'apellidos' => $data['apellidos'],
        'ni' => $data['ni'],
        'role_id' => 2,
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'password_confirmation' => Hash::make($data['password_confirmation']),
        'remember_token'=> str_random(15),
    ]);

}

I get the following error enter image description here我收到以下错误在此处输入图片说明

What would be the solution for this case.这种情况下的解决方案是什么。 I am using Laravel 5.8 and AdminLte as a template我使用 Laravel 5.8 和 AdminLte 作为模板

You can manually login the newly created user and redirect it to homepage.您可以手动登录新创建的用户并将其重定向到主页。

protected function create(RequestFormRegister $data)
{
    $userCount = User::where(['email'=> $data['email']])->count();
    if($userCount>0){
        return redirect()->back()->with('error', '¡El correo ya existe!');
    }else{
        $user = User::create([
            ...
        ]);

      // Manually logging user 
      Auth::login($user);
      return redirect()->route('homepage');

    }

}

https://laravel.com/docs/master/authentication#other-authentication-methods https://laravel.com/docs/master/authentication#other-authentication-methods

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM