Laravel 5.6 PasswordBroker根据特定情况动态更改令牌持续时间

问题描述 投票:1回答:2

我想要达到的目的是,当我们创建一个用户时,他会收到一封登录邮件,其中包含仅在6个小时左右有效的链接。这还不够,在大多数情况下,我们必须手动为用户设置密码。

用户应该有3天的时间来创建他的第一个密码。

但是,当用户单击忘记密码时,6个小时的限制就足够了(因为这是他有意识地做到的。)>

这是我到目前为止所拥有的!

我们在UsersController

中的存储功能如下:
public function store(StoreUser $request)
    {
        ...

        \DB::transaction(function () use ($request, $data) {

            $roles = $request->input('roles');
            $isInternal = $request->input('is_internal');
            $customers = $request->input('customers', []);

            /** @var User $user */
            $user = $this->userRepository->create($data);
            $user->assignRole($roles);

            if ($isInternal == false && !empty($customers)) {
                $user->customers()->sync($customers);
            }

            $token = app(PasswordBroker::class)->createToken($user);
            $user->notify(new AccountActivationNotification($token));
        });

        return $this->respond()->success([], "User successfully created.");
    }

我们的重置

忘记功能:
    public function reset(Request $request)
    {
        $request->validate([
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:6',
        ]);

        $credentials = $request->only('email', 'password', 'password_confirmation', 'token');

        // Here we will attempt to reset the user's password. If it is successful we
        // will update the password on an actual user model and persist it to the
        // database. Otherwise we will parse the error and return the response.
        $response = $this->passwordBroker->reset(
            $credentials,
            function ($user, $password) {
                $user->password = $password;
                $user->status = StatusesService::STATUS_ACTIVE;
                $user->email_verified_at = now();
                $user->save();
                event(new PasswordReset($user));
            }
        );

        return $response == $this->passwordBroker::PASSWORD_RESET
            ? $this->respond()->success()
            : $this->respond()->validationFailed(trans($response));
    }

    public function forgot(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
        ]);

        // We will send the password reset link to this user. Once we have attempted
        // to send the link, we will examine the response then see the message we
        // need to show to the user. Finally, we'll send out a proper response.
        $response = $this->passwordBroker->sendResetLink(
            $request->only('email')
        );
        return $response == $this->passwordBroker::RESET_LINK_SENT
            ? $this->respond()->success([], "Your password has been reset, please check your inbox.")
            : $this->respond()->validationFailed(trans($response));
    }

我们已经在config / auth.php中设置了两个不同的配置:

 'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 4320, //3 days
        ],
        'users_fpassword' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 1440, //6 hours
        ],
    ],

根据帖子开头描述的情况,我们该怎么做才能在config/auth.php中的配置之间动态更改?

我想要达到的目的是,当我们创建一个用户时,他会收到一封登录邮件,其中包含仅在6个小时左右有效的链接。这还不够,在大多数情况下,我们必须手动设置...

php laravel laravel-5.6
2个回答
0
投票

我认为您正在寻找的是如何在Laravel中动态设置配置值,您可以使用Laravel帮助器函数轻松地做到这一点。


0
投票

我认为,对您来说更好的解决方案是:

© www.soinside.com 2019 - 2024. All rights reserved.