0

0

密码重置功能:使用 OTP 重置密码

碧海醫心

碧海醫心

发布时间:2024-10-07 13:34:02

|

1531人浏览过

|

来源于dev.to

转载

密码重置功能:使用 otp 重置密码

后端

2. 重置密码

转向下一个 api。

put 到 /api/reset-password, req -> otp, email, 新密码, res -> nocontent

// controllers/passwordreset.go
func resetpassword(c *fiber.ctx) error {
    type input struct {
        otp         string `json:"otp"`
        email       string `json:"email"`
        newpassword string `json:"new_password"`
    }

    var input input

    err := c.bodyparser(&input)
    if err != nil {
        return c.status(fiber.statusbadrequest).json(fiber.map{
            "error": "invalid data",
        })
    }

    // no input field should be empty
    if input.otp == "" || input.email == "" || input.newpassword == "" {
        return c.status(fiber.statusbadrequest).json(fiber.map{
            "error": "invalid data",
        })
    }

    // todo: check redis for otp and update password

    return c.sendstatus(fiber.statusnocontent)
}

为其添加路线

// routes/routes.go
api.put("/reset-password", controllers.resetpassword)

现在我需要两个函数:

  1. 验证otp -> 输入 = otp、电子邮件;输出 = 错误(如果有)
  2. updatepassword -> 输入 = 电子邮件、密码;输出 = 错误(如果有)
// utils/passwordreset.go
func verifyotp(otp string, email string, c context.context) (error, bool) {
    key := otpkeyprefix + email

    // get the value for the key
    value, err := config.redisclient.get(c, key).result()
    if err != nil {
        // the following states that the key was not found
        if err == redis.nil {
            return errors.new("otp expired / incorrect email"), false
        }

        // for other errors
        return err, true
    }

    // compare received otp's hash with value in redis
    err = bcrypt.comparehashandpassword([]byte(value), []byte(otp))
    if err != nil {
        return errors.new("incorrect otp"), false
    }

    // delete redis key to prevent abuse of otp
    err = config.redisclient.del(c, key).err()
    if err != nil {
        return err, true
    }

    return nil, false
}

func updatepassword(email string, password string, c context.context) error {
    users := config.db.collection("users")

    // hash the password
    hashedpassword, _ := bcrypt.generatefrompassword([]byte(password), 10)

    // update the password
    update := bson.m{
        "$set": bson.m{
            "password": hashedpassword,
        },
    }
    _, err := users.updatebyid(c, email, update)
    if err != nil {
        return err
    }

    return nil
}

现在我需要将它们放在控制器中。我使用verifyotp函数中的bool来表示错误是内部错误还是由于输入引起的。

NT80 购物系统
NT80 购物系统

功能说明:1 会员可申请开店功能2 购买在线扣除金额3 冲值卡自动生成4 支持2级分类5 数据库压缩和备份6 会员分5个级别7 商品带讨论8 自带融合论坛,可关闭打开9 密码找回功能10 新闻``滚动新闻``帮助中心11 后台设置前台会员的上传权限12 可关闭/打开商店13 会员自助发布商品功能14 用户问题咨询管理

下载
// controllers/passwordreset.go
func resetpassword(c *fiber.ctx) error {
    type input struct {
        otp         string `json:"otp"`
        email       string `json:"email"`
        newpassword string `json:"new_password"`
    }

    var input input

    err := c.bodyparser(&input)
    if err != nil {
        return c.status(fiber.statusbadrequest).json(fiber.map{
            "error": "invalid data",
        })
    }

    // no input field should be empty
    if input.otp == "" || input.email == "" || input.newpassword == "" {
        return c.status(fiber.statusbadrequest).json(fiber.map{
            "error": "invalid data",
        })
    }

    // check redis for otp
    err, isinternalerr := utils.verifyotp(input.otp, input.email, c.context())
    if err != nil {
        var code int
        if isinternalerr {
            code = fiber.statusinternalservererror
        } else {
            code = fiber.statusunauthorized
        }

        return c.status(code).json(fiber.map{
            "error": err.error(),
        })
    }

    err = utils.updatepassword(input.email, input.newpassword, c.context())
    if err != nil {
        return c.status(fiber.statusinternalservererror).json(fiber.map{
            "error": err.error(),
        })
    }

    return c.sendstatus(fiber.statusnocontent)
}

api现已构建完成,可以使用以下curl命令进行测试

curl --location --request PUT 'localhost:3000/api/reset-password' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email": "yashjaiswal.cse@gmail.com",
    "new_password": "tester123",
    "otp": "DM4RDNF07B"
}'

下一部分,我将从前端开始

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
curl_exec
curl_exec

curl_exec函数是PHP cURL函数列表中的一种,它的功能是执行一个cURL会话。给大家总结了一下php curl_exec函数的一些用法实例,这个函数应该在初始化一个cURL会话并且全部的选项都被设置后被调用。他的返回值成功时返回TRUE, 或者在失败时返回FALSE。

441

2023.06.14

linux常见下载安装工具
linux常见下载安装工具

linux常见下载安装工具有APT、YUM、DNF、Snapcraft、Flatpak、AppImage、Wget、Curl等。想了解更多linux常见下载安装工具相关内容,可以阅读本专题下面的文章。

178

2023.10.30

curl_exec
curl_exec

curl_exec函数是PHP cURL函数列表中的一种,它的功能是执行一个cURL会话。给大家总结了一下php curl_exec函数的一些用法实例,这个函数应该在初始化一个cURL会话并且全部的选项都被设置后被调用。他的返回值成功时返回TRUE, 或者在失败时返回FALSE。

441

2023.06.14

linux常见下载安装工具
linux常见下载安装工具

linux常见下载安装工具有APT、YUM、DNF、Snapcraft、Flatpak、AppImage、Wget、Curl等。想了解更多linux常见下载安装工具相关内容,可以阅读本专题下面的文章。

178

2023.10.30

C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

9

2026.01.30

c++ 字符串格式化
c++ 字符串格式化

本专题整合了c++字符串格式化用法、输出技巧、实践等等内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.30

java 字符串格式化
java 字符串格式化

本专题整合了java如何进行字符串格式化相关教程、使用解析、方法详解等等内容。阅读专题下面的文章了解更多详细教程。

8

2026.01.30

python 字符串格式化
python 字符串格式化

本专题整合了python字符串格式化教程、实践、方法、进阶等等相关内容,阅读专题下面的文章了解更多详细操作。

3

2026.01.30

java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

20

2026.01.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.4万人学习

Redis+MySQL数据库面试教程
Redis+MySQL数据库面试教程

共72课时 | 6.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号