简体   繁体   English

在 Laravel 8 微风中注册后如何停止自动登录

[英]How to stop auto login after registration in Laravel 8 breeze

Hi I am fairly new to Laravel framework.嗨,我对 Laravel 框架还很陌生。 I am trying to create a separate controller replicating the registeredusercontroller.php in laravel breeze to have a function for admin user to be able to add new users. I am trying to create a separate controller replicating the registeredusercontroller.php in laravel breeze to have a function for admin user to be able to add new users. But I am having trouble with auto login and as soon as I add a new user it automatically logs in to the new user.但是我在自动登录时遇到了问题,一旦我添加了一个新用户,它就会自动登录到新用户。 How can I stop this from happening.我怎样才能阻止这种情况发生。 I saw posts stating to remove我看到帖子说要删除

$this->guard()->login($user);

but when I see the app/Auth/registeredusercontroller.php I don't see that line.但是当我看到 app/Auth/registeredusercontroller.php 时,我看不到那条线。

public function store(Request $request)
{
    $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|confirmed|min:8',
    ]);

    Auth::login($user = User::create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => Hash::make($request->password),
    ]));
    $user->attachRole('user');
    event(new Registered($user));

    return redirect(RouteServiceProvider::HOME);
}

how can I stop the auto login after registration.注册后如何停止自动登录。

Any help is greatly appreciated.任何帮助是极大的赞赏。

You can do this proper way like using custom request handler.您可以像使用自定义请求处理程序一样正确地执行此操作。 The parameter you need is name, email, password.您需要的参数是名称,email,密码。 So add CreateUserRequest above app/http/request所以在 app/http/request 上面添加 CreateUserRequest

<?php

namespace App\Http\Requests;

use Illuminate\Http\Request;

class CreateUserRequest extends Request
{

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|string|max:255',
    'email' => 'required|string|email|max:255|unique:users',
    'password' => 'required|string|confirmed|min:8',
        ]; 
    }
}

And into your controller just do this;并进入您的 controller 只需执行此操作;

public function store(CreateUserRequest $request) // add your custom request as a parameter

$user = User::create($request) 

These codes makes your code structure clear and clean.这些代码使您的代码结构清晰明了。

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

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