mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-14 22:15:59 +08:00
add reset pwd
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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,25 +37,32 @@ func SendVerificationCode(c *gin.Context) {
|
||||
} else {
|
||||
account = params.PhoneNumber
|
||||
}
|
||||
_, err := im_mysql_model.GetRegister(account)
|
||||
if err == nil {
|
||||
log.NewError(params.OperationID, "The phone number has been registered", params)
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.HasRegistered, "errMsg": ""})
|
||||
return
|
||||
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)
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.HasRegistered, "errMsg": ""})
|
||||
return
|
||||
}
|
||||
ok, err := db.DB.JudgeAccountEXISTS(account)
|
||||
if ok || err != nil {
|
||||
log.NewError(params.OperationID, "The phone number has been registered", params)
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RepeatSendCode, "errMsg": ""})
|
||||
return
|
||||
}
|
||||
accountKey = account + "_" + constant.VerificationCodeForRegisterSuffix
|
||||
|
||||
case constant.VerificationCodeForReset:
|
||||
accountKey = account + "_" + constant.VerificationCodeForResetSuffix
|
||||
}
|
||||
ok, err := db.DB.JudgeAccountEXISTS(account)
|
||||
if ok || err != nil {
|
||||
log.NewError(params.OperationID, "The phone number has been registered", params)
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RepeatSendCode, "errMsg": ""})
|
||||
return
|
||||
}
|
||||
log.InfoByKv("begin sendSms", account)
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user