简体   繁体   中英

How to stop auto login after registration in Laravel 8 breeze

Hi I am fairly new to Laravel framework. 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.

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. So add CreateUserRequest above app/http/request

<?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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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