mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-11-04 11:22:10 +08:00 
			
		
		
		
	add reset pwd
This commit is contained in:
		
							parent
							
								
									def252652a
								
							
						
					
					
						commit
						d5ba2b4cf7
					
				@ -21,6 +21,7 @@ func main() {
 | 
			
		||||
		authRouterGroup.POST("/verify", register.Verify)
 | 
			
		||||
		authRouterGroup.POST("/password", register.SetPassword)
 | 
			
		||||
		authRouterGroup.POST("/login", register.Login)
 | 
			
		||||
		authRouterGroup.POST("/reset_password", register.ResetPassword)
 | 
			
		||||
	}
 | 
			
		||||
	log.NewPrivateLog("demo")
 | 
			
		||||
	ginPort := flag.Int("port", 42233, "get ginServerPort from cmd,default 42233 as port")
 | 
			
		||||
 | 
			
		||||
@ -32,7 +32,7 @@ func GetMessagesStatistics(c *gin.Context) {
 | 
			
		||||
	client := pb.NewUserClient(etcdConn)
 | 
			
		||||
	respPb, err := client.GetMessageStatistics(context.Background(), &reqPb)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		openIMHttp.RespHttp200(c, constant.ErrServer, resp)
 | 
			
		||||
		openIMHttp.RespHttp200(c, err, resp)
 | 
			
		||||
		log.NewError("0", utils.GetSelfFuncName(), err.Error())
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
@ -78,7 +78,7 @@ func GetUserStatistics(c *gin.Context) {
 | 
			
		||||
	respPb, err := client.GetUserStatistics(context.Background(), &reqPb)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.NewError("0", utils.GetSelfFuncName(), err.Error())
 | 
			
		||||
		openIMHttp.RespHttp200(c, constant.ErrServer, nil)
 | 
			
		||||
		openIMHttp.RespHttp200(c, err, nil)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// utils.CopyStructFields(&resp, respPb)
 | 
			
		||||
@ -132,7 +132,7 @@ func GetGroupStatistics(c *gin.Context) {
 | 
			
		||||
	client := pb.NewUserClient(etcdConn)
 | 
			
		||||
	respPb, err := client.GetGroupStatistics(context.Background(), &reqPb)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		openIMHttp.RespHttp200(c, constant.ErrServer, nil)
 | 
			
		||||
		openIMHttp.RespHttp200(c, err, nil)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// utils.CopyStructFields(&resp, respPb)
 | 
			
		||||
@ -179,7 +179,7 @@ func GetActiveUser(c *gin.Context) {
 | 
			
		||||
	client := pb.NewUserClient(etcdConn)
 | 
			
		||||
	respPb, err := client.GetActiveUser(context.Background(), &reqPb)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		openIMHttp.RespHttp200(c, constant.ErrServer, nil)
 | 
			
		||||
		openIMHttp.RespHttp200(c, err, nil)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	utils.CopyStructFields(&resp.ActiveUserList, respPb.Users)
 | 
			
		||||
@ -204,7 +204,7 @@ func GetActiveGroup(c *gin.Context) {
 | 
			
		||||
	respPb, err := client.GetActiveGroup(context.Background(), &reqPb)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.NewError("0", "BindJSON failed ", err.Error())
 | 
			
		||||
		openIMHttp.RespHttp200(c, constant.ErrServer, nil)
 | 
			
		||||
		openIMHttp.RespHttp200(c, err, nil)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, group := range respPb.Groups {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										56
									
								
								internal/demo/register/reset_password.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								internal/demo/register/reset_password.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,56 @@
 | 
			
		||||
package register
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"Open_IM/pkg/common/config"
 | 
			
		||||
	"Open_IM/pkg/common/constant"
 | 
			
		||||
	"Open_IM/pkg/common/db"
 | 
			
		||||
	"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
 | 
			
		||||
	"Open_IM/pkg/common/log"
 | 
			
		||||
	"Open_IM/pkg/utils"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type resetPasswordRequest struct {
 | 
			
		||||
	VerificationCode string `json:"verificationCode"`
 | 
			
		||||
	Email            string `json:"email"`
 | 
			
		||||
	PhoneNumber      string `json:"phoneNumber"`
 | 
			
		||||
	NewPassword string `json:"newPassword"`
 | 
			
		||||
	OperationID string `json:"operationID"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ResetPassword(c *gin.Context) {
 | 
			
		||||
	var (
 | 
			
		||||
		req resetPasswordRequest
 | 
			
		||||
	)
 | 
			
		||||
	if err := c.BindJSON(&req); err != nil {
 | 
			
		||||
		c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	var account string
 | 
			
		||||
	if req.Email != "" {
 | 
			
		||||
		account = req.Email
 | 
			
		||||
	} else {
 | 
			
		||||
		account = req.PhoneNumber
 | 
			
		||||
	}
 | 
			
		||||
	if req.VerificationCode != config.Config.Demo.SuperCode {
 | 
			
		||||
		accountKey := account + "_" + constant.VerificationCodeForResetSuffix
 | 
			
		||||
		v, err := db.DB.GetAccountCode(accountKey)
 | 
			
		||||
		if err != nil || v != req.VerificationCode {
 | 
			
		||||
			log.NewError(req.OperationID, "password Verification code error", account, req.VerificationCode, v)
 | 
			
		||||
			c.JSON(http.StatusOK, gin.H{"errCode": constant.CodeInvalidOrExpired, "errMsg": "Verification code error!"})
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	user, err := im_mysql_model.GetRegister(account)
 | 
			
		||||
	if err != nil || user.Account == "" {
 | 
			
		||||
		log.NewError(req.OperationID, utils.GetSelfFuncName(), "get register error", err.Error())
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{"errCode": constant.NotRegistered, "errMsg": "user not register!"})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if err := im_mysql_model.ResetPassword(account, req.NewPassword); err != nil {
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{"errCode": constant.ResetPasswordFailed, "errMsg": "reset password failed: "+err.Error()})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "reset password success"})
 | 
			
		||||
}
 | 
			
		||||
@ -21,6 +21,7 @@ type paramsVerificationCode struct {
 | 
			
		||||
	Email       string `json:"email"`
 | 
			
		||||
	PhoneNumber string `json:"phoneNumber"`
 | 
			
		||||
	OperationID string `json:"operationID" binding:"required"`
 | 
			
		||||
	UsedFor     int `json:"usedFor" binding:"required"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SendVerificationCode(c *gin.Context) {
 | 
			
		||||
@ -36,6 +37,9 @@ func SendVerificationCode(c *gin.Context) {
 | 
			
		||||
	} else {
 | 
			
		||||
		account = params.PhoneNumber
 | 
			
		||||
	}
 | 
			
		||||
	var accountKey string
 | 
			
		||||
	switch params.UsedFor {
 | 
			
		||||
	case constant.VerificationCodeForRegister:
 | 
			
		||||
		_, err := im_mysql_model.GetRegister(account)
 | 
			
		||||
		if err == nil {
 | 
			
		||||
			log.NewError(params.OperationID, "The phone number has been registered", params)
 | 
			
		||||
@ -48,13 +52,17 @@ func SendVerificationCode(c *gin.Context) {
 | 
			
		||||
			c.JSON(http.StatusOK, gin.H{"errCode": constant.RepeatSendCode, "errMsg": ""})
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	log.InfoByKv("begin sendSms", account)
 | 
			
		||||
		accountKey = account + "_" + constant.VerificationCodeForRegisterSuffix
 | 
			
		||||
 | 
			
		||||
	case constant.VerificationCodeForReset:
 | 
			
		||||
		accountKey = account + "_" + constant.VerificationCodeForResetSuffix
 | 
			
		||||
	}
 | 
			
		||||
	log.NewInfo(params.OperationID, params.UsedFor,"begin store redis", accountKey)
 | 
			
		||||
	rand.Seed(time.Now().UnixNano())
 | 
			
		||||
	code := 100000 + rand.Intn(900000)
 | 
			
		||||
	log.NewInfo(params.OperationID, "begin store redis", account)
 | 
			
		||||
	err = db.DB.SetAccountCode(account, code, config.Config.Demo.CodeTTL)
 | 
			
		||||
	err := db.DB.SetAccountCode(accountKey, code, config.Config.Demo.CodeTTL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.NewError(params.OperationID, "set redis error", account, "err", err.Error())
 | 
			
		||||
		log.NewError(params.OperationID, "set redis error", accountKey, "err", err.Error())
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{"errCode": constant.SmsSendCodeErr, "errMsg": "Enter the superCode directly in the verification code box, SuperCode can be configured in config.xml"})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,6 @@ import (
 | 
			
		||||
	"Open_IM/pkg/utils"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/garyburd/redigo/redis"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
@ -29,6 +28,7 @@ type ParamsSetPassword struct {
 | 
			
		||||
func SetPassword(c *gin.Context) {
 | 
			
		||||
	params := ParamsSetPassword{}
 | 
			
		||||
	if err := c.BindJSON(¶ms); err != nil {
 | 
			
		||||
		log.NewError(params.OperationID, utils.GetSelfFuncName(), "bind json failed", err.Error())
 | 
			
		||||
		c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
@ -39,7 +39,8 @@ func SetPassword(c *gin.Context) {
 | 
			
		||||
		account = params.PhoneNumber
 | 
			
		||||
	}
 | 
			
		||||
	if params.VerificationCode != config.Config.Demo.SuperCode {
 | 
			
		||||
		v, err := redis.String(db.DB.Exec("GET", account))
 | 
			
		||||
		accountKey := account + "_" + constant.VerificationCodeForRegisterSuffix
 | 
			
		||||
		v, err := db.DB.GetAccountCode(accountKey)
 | 
			
		||||
		if err != nil || v != params.VerificationCode {
 | 
			
		||||
			log.NewError(params.OperationID, "password Verification code error", account, params.VerificationCode)
 | 
			
		||||
			data := make(map[string]interface{})
 | 
			
		||||
@ -65,7 +66,7 @@ func SetPassword(c *gin.Context) {
 | 
			
		||||
	err = json.Unmarshal(bMsg, &openIMRegisterResp)
 | 
			
		||||
	if err != nil || openIMRegisterResp.ErrCode != 0 {
 | 
			
		||||
		log.NewError(params.OperationID, "request openIM register error", account, "err", "")
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": ""})
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": "register failed: "+openIMRegisterResp.ErrMsg})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	log.Info(params.OperationID, "begin store mysql", account, params.Password)
 | 
			
		||||
 | 
			
		||||
@ -15,6 +15,7 @@ type paramsCertification struct {
 | 
			
		||||
	PhoneNumber      string `json:"phoneNumber"`
 | 
			
		||||
	VerificationCode string `json:"verificationCode"`
 | 
			
		||||
	OperationID      string `json:"operationID" binding:"required"`
 | 
			
		||||
	UsedFor          int `json:"usedFor" binding:"required"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Verify(c *gin.Context) {
 | 
			
		||||
 | 
			
		||||
@ -124,6 +124,12 @@ const (
 | 
			
		||||
 | 
			
		||||
	//Minio
 | 
			
		||||
	MinioDurationTimes = 3600
 | 
			
		||||
 | 
			
		||||
	// verificationCode used for
 | 
			
		||||
	VerificationCodeForRegister = 1
 | 
			
		||||
	VerificationCodeForReset = 2
 | 
			
		||||
	VerificationCodeForRegisterSuffix = "_forRegister"
 | 
			
		||||
	VerificationCodeForResetSuffix = "_forReset"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var ContentType2PushContent = map[int64]string{
 | 
			
		||||
 | 
			
		||||
@ -80,6 +80,7 @@ const (
 | 
			
		||||
	SmsSendCodeErr       = 10008
 | 
			
		||||
	CodeInvalidOrExpired = 10009
 | 
			
		||||
	RegisterFailed       = 10010
 | 
			
		||||
	ResetPasswordFailed  = 10011
 | 
			
		||||
	DatabaseError        = 10002
 | 
			
		||||
	ServerError          = 10004
 | 
			
		||||
	HttpError            = 10005
 | 
			
		||||
 | 
			
		||||
@ -14,6 +14,7 @@ func GetRegister(account string) (*db.Register, error) {
 | 
			
		||||
	return &r, dbConn.Table("registers").Where("account = ?",
 | 
			
		||||
		account).Take(&r).Error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SetPassword(account, password, ex string) error {
 | 
			
		||||
	r := db.Register{
 | 
			
		||||
		Account:  account,
 | 
			
		||||
@ -25,5 +26,16 @@ func SetPassword(account, password, ex string) error {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return dbConn.Table("registers").Create(&r).Error
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ResetPassword(account, password string) error {
 | 
			
		||||
	r := db.Register{
 | 
			
		||||
		Password:password,
 | 
			
		||||
	}
 | 
			
		||||
	dbConn, err := db.DB.MysqlDB.DefaultGormDB()
 | 
			
		||||
	dbConn.LogMode(true)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return dbConn.Table("registers").Where("account = ?", account).Update(&r).Error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,11 +3,13 @@ package db
 | 
			
		||||
import (
 | 
			
		||||
	"Open_IM/pkg/common/constant"
 | 
			
		||||
	log2 "Open_IM/pkg/common/log"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/garyburd/redigo/redis"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	registerAccountTempCode       = "REGISTER_ACCOUNT_TEMP_CODE"
 | 
			
		||||
	AccountTempCode               = "ACCOUNT_TEMP_CODE"
 | 
			
		||||
	resetPwdTempCode              = "RESET_PWD_TEMP_CODE"
 | 
			
		||||
	userIncrSeq                   = "REDIS_USER_INCR_SEQ:" // user incr seq
 | 
			
		||||
	appleDeviceToken              = "DEVICE_TOKEN"
 | 
			
		||||
	userMinSeq                    = "REDIS_USER_MIN_SEQ:"
 | 
			
		||||
@ -35,19 +37,21 @@ func (d *DataBases) Exec(cmd string, key interface{}, args ...interface{}) (inte
 | 
			
		||||
	return con.Do(cmd, params...)
 | 
			
		||||
}
 | 
			
		||||
func (d *DataBases) JudgeAccountEXISTS(account string) (bool, error) {
 | 
			
		||||
	key := registerAccountTempCode + account
 | 
			
		||||
	key := AccountTempCode + account
 | 
			
		||||
	return redis.Bool(d.Exec("EXISTS", key))
 | 
			
		||||
}
 | 
			
		||||
func (d *DataBases) SetAccountCode(account string, code, ttl int) (err error) {
 | 
			
		||||
	key := registerAccountTempCode + account
 | 
			
		||||
	_, err = d.Exec("Set", key, code, ttl)
 | 
			
		||||
	key := AccountTempCode + account
 | 
			
		||||
	_, err = d.Exec("SET", key, code)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
func (d *DataBases) GetAccountCode(account string) (string, error) {
 | 
			
		||||
	key := userIncrSeq + account
 | 
			
		||||
	key := AccountTempCode + account
 | 
			
		||||
	fmt.Println(key)
 | 
			
		||||
	return redis.String(d.Exec("GET", key))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//Perform seq auto-increment operation of user messages
 | 
			
		||||
func (d *DataBases) IncrUserSeq(uid string) (uint64, error) {
 | 
			
		||||
	key := userIncrSeq + uid
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user