简体   繁体   English

注册用户后不自动登录

[英]Don't log in automatically after register user

I need to make the new user registration to be available only to logged in users.我需要使新用户注册仅对登录用户可用。 The thing is that when I fill the form for registration, it automatically logs in with the new account, even if I'm already logged in with another account.问题是,当我填写注册表格时,它会自动使用新帐户登录,即使我已经使用另一个帐户登录也是如此。 I'm using Laravel auth for the login system.我为登录系统使用 Laravel 身份验证。 My registerController is this:我的 registerController 是这样的:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = RouteServiceProvider::HOME;

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}
}

RegisterUsers is this: RegisterUsers是这样的:

<?php

namespace Illuminate\Foundation\Auth;

use Illuminate\Auth\Events\Registered;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

trait RegistersUsers
{
use RedirectsUsers;

/**
 * Show the application registration form.
 *
 * @return \Illuminate\View\View
 */
public function showRegistrationForm()
{
    return view('auth.register');
}

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
 */
public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

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

    if ($response = $this->registered($request, $user)) {
        return $response;
    }

    return $request->wantsJson()
                ? new JsonResponse([], 201)
                : redirect($this->redirectPath());
}

/**
 * Get the guard to be used during registration.
 *
 * @return \Illuminate\Contracts\Auth\StatefulGuard
 */
protected function guard()
{
    return Auth::guard();
}

/**
 * The user has been registered.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */
protected function registered(Request $request, $user)
{
    //
}
}

I've tried to remove "$this->guard()->login($user);"我试图删除“$this->guard()->login($user);” this part of the RegistersUsers, but it didn't change anything.. RegistersUsers 的这一部分,但它没有改变任何东西..

Edit: Someone sent me a link to this discussion-> How can I disable auto login after registration in laravel 8?编辑:有人给我发了这个讨论的链接-> 在 laravel 8 注册后如何禁用自动登录? but I'm not using Fortify (not that I know of), and the solution in that discussion is to delete this line -> "$this->guard()->login($user);", which I already did (as seen in the question).但我没有使用 Fortify(据我所知),该讨论中的解决方案是删除这一行 ->“$this->guard()->login($user);”,我已经这样做了(如问题所示)。

You can just create new users, not registering new ones but creating.您可以只创建新用户,而不是注册新用户,而是创建。 All you need to do is hashing users' passwords in your function and call it by post method您需要做的就是在您的 function 中散列用户的密码,然后通过 post 方法调用它

use Illuminate\Support\Facades\Hash;
use App\Models\User; // don't forget to include this lines at the top  

public function createUser(Request $request){
     $data = $request->all();
     $data['password'] = Hash::make($data['password']);
     User::create($data);
     return redirect()->to('/redirect-to-route-after-creating-new-user');
}

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

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