微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在laravel 8中注册后如何禁用自动登录?

如何解决在laravel 8中注册后如何禁用自动登录?

在结合使用laravel 8和fortify时,我没有

App \ Http \ Controllers \ Auth \ RegisterController

预先感谢

解决方法

首先您必须最好在app\Http\Controllers\Auth中创建一个名为RegisteredUserController的控制器,在这个控制器中您必须覆盖RegisteredUserController类的方法store .

store 方法复制到您的新控制器并删除行 $this->guard->login($user);

它应该是这样的:

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Fortify\Contracts\RegisterResponse;

class RegisteredUserController
    extends \Laravel\Fortify\Http\Controllers\RegisteredUserController
{

    public function store(Request $request,CreatesNewUsers $creator): RegisterResponse {
        event(new Registered($user = $creator->create($request->all())));
        return app(RegisterResponse::class);
    }

}

最后更改指向新控制器的默认 /register 路径。

Route::post('/register','Auth\RegisteredUserController@store');
,

转到您的config / fortify.php

'features' => [
    Features::registration(),Features::resetPasswords(),//Features::emailVerification(),=>uncomment this
    Features::updateProfileInformation(),Features::updatePasswords(),Features::twoFactorAuthentication([
        'confirmPassword' => true,]),],
,

仅当您从 CreateNewUser 类返回用户时,Fortify 才会自动登录用户。不是返回创建的用户,而是抛出异常和闪现消息。 Fortify 将尝试将您重定向到主页,并将您返回到登录页面,因为用户未通过身份验证向您显示 Flash 消息。下面是对文件 App\Actions\Fortify\CreateNewUser 中的进程的一瞥。

public function create(array $input)
    {
        Validator::make($input,[
            'name' => ['required','string','max:255'],'email' => [
                'required','email','max:255',Rule::unique(User::class),'password' => $this->passwordRules(),])->validate();

        $user = User::create([
            'name' => $input['name'],'email' => $input['email'],'password' => Hash::make($input['password'])
        ]);

        event(new Registered($user));
        flash('Registration successful! Awaiting approval from admin.')->success()->important();
        throw new \Illuminate\Auth\AuthenticationException\AuthenticationException();
    }

我认为可能有其他解决方案可以连接到任何 Fortify 事件中,以便更优雅地做到这一点。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。