Home >PHP Framework >Swoole >How to use Hyperf framework for password reset

How to use Hyperf framework for password reset

PHPz
PHPzOriginal
2023-10-20 18:39:35702browse

How to use Hyperf framework for password reset

How to use the Hyperf framework to reset password

Introduction: Password reset is one of the common functions on websites or applications. When users forget their passwords or need When changing the password, the reset password function allows users to easily reset a new password. This article will introduce how to use the Hyperf framework to implement the password reset function and provide code examples.

1. Design Ideas

When designing the password reset function, the following steps are generally required:

  1. The user clicks the "Forgot Password" button to enter the password Reset page.
  2. Users enter the email address or mobile phone number used during registration.
  3. The system checks whether the email or mobile phone number submitted by the user exists. If it exists, a link to reset the password will be sent to the email or mobile phone number.
  4. The user opens the received password reset link and jumps to the password reset page.
  5. The user enters the new password and confirms the password, and the system resets the password.
  6. After the password is reset successfully, the user can log in using the new password.

2. Code implementation

  1. Create password reset controller file (ResetPasswordController.php)
<?php

namespace AppController;

use AppServiceEmailService;
use AppServiceUserService;
use HyperfHttpServerAnnotationAutoController;

/**
 * @AutoController()
 */
class ResetPasswordController
{
    /**
     * 发送重置密码链接
     */
    public function sendResetLink(UserService $userService, EmailService $emailService)
    {
        $email = request()->input('email');
        
        // 检查邮箱是否存在
        if (!$userService->checkEmailExists($email)) {
            return ['code' => 400, 'message' => '该邮箱不存在'];
        }
        
        // 发送重置密码链接
        $emailService->sendResetLinkEmail($email);
        
        return ['code' => 200, 'message' => '已发送重置密码链接,请查收邮箱'];
    }

    /**
     * 重置密码
     */
    public function resetPassword(UserService $userService)
    {
        $email = request()->input('email');
        $token = request()->input('token');
        $password = request()->input('password');
        
        // 验证重置密码链接的合法性
        if (!$userService->validateResetToken($email, $token)) {
            return ['code' => 400, 'message' => '重置密码链接已失效'];
        }
        
        // 更新用户密码
        $userService->updatePassword($email, $password);
        
        return ['code' => 200, 'message' => '密码重置成功'];
    }
}
  1. Create mail service File (EmailService.php)
<?php

namespace AppService;

class EmailService
{
    /**
     * 发送重置密码链接到用户邮箱
     */
    public function sendResetLinkEmail($email)
    {
        // 发送邮件的逻辑
    }
}
  1. Create user service file (UserService.php)
<?php

namespace AppService;

class UserService
{
    /**
     * 检查邮箱是否存在
     */
    public function checkEmailExists($email)
    {
        // 判断邮箱是否存在的逻辑
    }
    
    /**
     * 验证重置密码链接的合法性
     */
    public function validateResetToken($email, $token)
    {
        // 验证重置密码链接的合法性逻辑
    }
    
    /**
     * 更新用户密码
     */
    public function updatePassword($email, $password)
    {
        // 更新用户密码的逻辑
    }
}

3. Usage examples

  1. Route settings (routes.php)
<?php

Router::post('/reset/send', 'AppControllerResetPasswordController@sendResetLink');
Router::post('/reset/reset', 'AppControllerResetPasswordController@resetPassword');
  1. Front-end page code

Send reset password link page (send_reset_link.blade.php)

<form action="/reset/send" method="POST">
    <input type="text" name="email" placeholder="请输入注册时使用的邮箱">
    <button type="submit">发送重置密码链接</button>
</form>

Reset password page (reset_password.blade.php)

<form action="/reset/reset" method="POST">
    <input type="hidden" name="email" value="{{ $email }}">
    <input type="hidden" name="token" value="{{ $token }}">
    <input type="password" name="password" placeholder="请输入新密码">
    <input type="password" name="confirm_password" placeholder="请确认新密码">
    <button type="submit">重置密码</button>
</form>

4. Summary

By using the Hyperf framework, we can implement the password reset function simply and efficiently. The above is a simple example, and actual use may require appropriate modifications and extensions based on business needs. Hope this article is helpful for you to understand how to use Hyperf framework for password reset.

The above is the detailed content of How to use Hyperf framework for password reset. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn