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

是否有从注册中删除帐户枚举的内置方法?

如何解决是否有从注册中删除帐户枚举的内置方法?

我使用 Jetstream 和 Inertia 创建了一个站点。目前,应用程序将返回“电子邮件已被占用”。如果用户尝试使用现有电子邮件注册,则消息。尽管进行了时间分析,但我还是希望将用户帐户的存在保密。有没有办法保持电子邮件的唯一约束,但如果有人使用现有电子邮件注册,则显示相同的外向行为?理想情况下,我不想创建第二个用户,而是向现有用户发送电子邮件,建议他们重置密码或忽略电子邮件

解决方法

我同意 Unflux 关于不更改这一点的意见,但如果您需要,您可以修改位于 CreateNewUser.phpapp\Actions\Fortify\CreateNewUser.php 并更改验证消息或修改流程。

负责创建新用户的 create() 方法如下所示:

public function create(array $input)
    {
        //define custom messages
        $customValidationMessages = {
            'email.unique' => 'New Message',}


        Validator::make($input,[
            'name' => ['required','string','max:255'],'email' => ['required','email','max:255','unique:users'],//email validation rules here
            'password' => $this->passwordRules(),],$customValidationMessages)->validate(); //add the variable containing the custom message(s) here

        
        return User::create([
            'name' => $input['name'],'email' => $input['email'],'password' => Hash::make($input['password']),'api_token' => Str::random(60),]);
    }

如果您需要向用户发送电子邮件或进一步自定义,我建议您考虑实施“验证后挂钩”,您可以在此处阅读:https://laravel.com/docs/8.x/validation#after-validation-hook

,

这对我有用:

  1. app/Exceptions/ExistingUserException.php 中创建新的验证异常
namespace App\Exceptions;

use Illuminate\Validation\ValidationException;

class ExistingUserException extends ValidationException
{

}
  1. app/Actions/Fortify/CreateNewUser.php 中将验证分为 2 个步骤,如果表单其他方面良好,则抛出扩展的 ValidationException
        Validator::make($input,'password' => $this->passwordRules(),'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required','accepted'] : '',])->validate();

        $validator =  Validator::make($input,[
            'email' => ['unique:users'],['email.unique'=>'']);

        if ($validator->fails())
        {
            throw new ExistingUserException($validator);
        }
  1. app/Http/Middleware/CatchExistingUser.php 中创建一个新的中间件
<?php

namespace App\Http\Middleware;

use App\Exceptions\ExistingUserException;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;

class CatchExistingUser
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request,Closure $next,$redirectToRoute = null)
    {
        $response = $next($request);

        if ($response->exception && $response->exception instanceof ExistingUserException)
        {
            return $request->expectsJson()
                    ? abort(403,'Your email address is not verified.')
                    : Redirect::guest(URL::route($redirectToRoute ?: 'verification.notice'));
        }

        return $response;
    }
}
  1. 通过 config/fortify.php 将中间件注入所有强化路由
'middleware' => [CatchExistingUser::class,'web'],
  1. 通过覆盖 routes/web.php 中的路由,从验证页面中删除 auth 中间件
use Illuminate\Http\Request;
use Laravel\Fortify\Contracts\VerifyEmailViewResponse;
...
Route::get('/email/verify',function (Request $request) {
    $user = $request->user();
    if ($user && $user->hasVerifiedEmail())
    {
        return redirect()->intended(config('fortify.home'));
    }
    return app(VerifyEmailViewResponse::class);
})
->name('verification.notice');

自定义异常并不理想,但它似乎比测试存储在 ValidatorException 中的验证器更干净,然后在出现多个错误时删除一条消息。我认为这是允许验证其他字段同时不泄露电子邮件唯一性所必需的。

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?