add reset pwd

pull/148/head
wangchuxiao 3 years ago
parent a2cd7c8007
commit 4d28ccebad

@ -21,6 +21,7 @@ func main() {
authRouterGroup.POST("/verify", register.Verify) authRouterGroup.POST("/verify", register.Verify)
authRouterGroup.POST("/password", register.SetPassword) authRouterGroup.POST("/password", register.SetPassword)
authRouterGroup.POST("/login", register.Login) authRouterGroup.POST("/login", register.Login)
authRouterGroup.POST("/reset_password", register.ResetPassword)
} }
log.NewPrivateLog("demo") log.NewPrivateLog("demo")
ginPort := flag.Int("port", 42233, "get ginServerPort from cmd,default 42233 as port") 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) client := pb.NewUserClient(etcdConn)
respPb, err := client.GetMessageStatistics(context.Background(), &reqPb) respPb, err := client.GetMessageStatistics(context.Background(), &reqPb)
if err != nil { if err != nil {
openIMHttp.RespHttp200(c, constant.ErrServer, resp) openIMHttp.RespHttp200(c, err, resp)
log.NewError("0", utils.GetSelfFuncName(), err.Error()) log.NewError("0", utils.GetSelfFuncName(), err.Error())
return return
} }
@ -78,7 +78,7 @@ func GetUserStatistics(c *gin.Context) {
respPb, err := client.GetUserStatistics(context.Background(), &reqPb) respPb, err := client.GetUserStatistics(context.Background(), &reqPb)
if err != nil { if err != nil {
log.NewError("0", utils.GetSelfFuncName(), err.Error()) log.NewError("0", utils.GetSelfFuncName(), err.Error())
openIMHttp.RespHttp200(c, constant.ErrServer, nil) openIMHttp.RespHttp200(c, err, nil)
return return
} }
// utils.CopyStructFields(&resp, respPb) // utils.CopyStructFields(&resp, respPb)
@ -132,7 +132,7 @@ func GetGroupStatistics(c *gin.Context) {
client := pb.NewUserClient(etcdConn) client := pb.NewUserClient(etcdConn)
respPb, err := client.GetGroupStatistics(context.Background(), &reqPb) respPb, err := client.GetGroupStatistics(context.Background(), &reqPb)
if err != nil { if err != nil {
openIMHttp.RespHttp200(c, constant.ErrServer, nil) openIMHttp.RespHttp200(c, err, nil)
return return
} }
// utils.CopyStructFields(&resp, respPb) // utils.CopyStructFields(&resp, respPb)
@ -179,7 +179,7 @@ func GetActiveUser(c *gin.Context) {
client := pb.NewUserClient(etcdConn) client := pb.NewUserClient(etcdConn)
respPb, err := client.GetActiveUser(context.Background(), &reqPb) respPb, err := client.GetActiveUser(context.Background(), &reqPb)
if err != nil { if err != nil {
openIMHttp.RespHttp200(c, constant.ErrServer, nil) openIMHttp.RespHttp200(c, err, nil)
return return
} }
utils.CopyStructFields(&resp.ActiveUserList, respPb.Users) utils.CopyStructFields(&resp.ActiveUserList, respPb.Users)
@ -204,7 +204,7 @@ func GetActiveGroup(c *gin.Context) {
respPb, err := client.GetActiveGroup(context.Background(), &reqPb) respPb, err := client.GetActiveGroup(context.Background(), &reqPb)
if err != nil { if err != nil {
log.NewError("0", "BindJSON failed ", err.Error()) log.NewError("0", "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrServer, nil) openIMHttp.RespHttp200(c, err, nil)
return return
} }
for _, group := range respPb.Groups { 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"` Email string `json:"email"`
PhoneNumber string `json:"phoneNumber"` PhoneNumber string `json:"phoneNumber"`
OperationID string `json:"operationID" binding:"required"` OperationID string `json:"operationID" binding:"required"`
UsedFor int `json:"usedFor" binding:"required"`
} }
func SendVerificationCode(c *gin.Context) { func SendVerificationCode(c *gin.Context) {
@ -36,25 +37,32 @@ func SendVerificationCode(c *gin.Context) {
} else { } else {
account = params.PhoneNumber account = params.PhoneNumber
} }
_, err := im_mysql_model.GetRegister(account) var accountKey string
if err == nil { switch params.UsedFor {
log.NewError(params.OperationID, "The phone number has been registered", params) case constant.VerificationCodeForRegister:
c.JSON(http.StatusOK, gin.H{"errCode": constant.HasRegistered, "errMsg": ""}) _, err := im_mysql_model.GetRegister(account)
return if err == nil {
} log.NewError(params.OperationID, "The phone number has been registered", params)
ok, err := db.DB.JudgeAccountEXISTS(account) c.JSON(http.StatusOK, gin.H{"errCode": constant.HasRegistered, "errMsg": ""})
if ok || err != nil { return
log.NewError(params.OperationID, "The phone number has been registered", params) }
c.JSON(http.StatusOK, gin.H{"errCode": constant.RepeatSendCode, "errMsg": ""}) ok, err := db.DB.JudgeAccountEXISTS(account)
return 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
} }
log.InfoByKv("begin sendSms", account) log.NewInfo(params.OperationID, params.UsedFor,"begin store redis", accountKey)
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
code := 100000 + rand.Intn(900000) code := 100000 + rand.Intn(900000)
log.NewInfo(params.OperationID, "begin store redis", account) err := db.DB.SetAccountCode(accountKey, code, config.Config.Demo.CodeTTL)
err = db.DB.SetAccountCode(account, code, config.Config.Demo.CodeTTL)
if err != nil { 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"}) 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 return
} }

@ -11,7 +11,6 @@ import (
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
) )
@ -29,6 +28,7 @@ type ParamsSetPassword struct {
func SetPassword(c *gin.Context) { func SetPassword(c *gin.Context) {
params := ParamsSetPassword{} params := ParamsSetPassword{}
if err := c.BindJSON(&params); err != nil { if err := c.BindJSON(&params); 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()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return return
} }
@ -39,7 +39,8 @@ func SetPassword(c *gin.Context) {
account = params.PhoneNumber account = params.PhoneNumber
} }
if params.VerificationCode != config.Config.Demo.SuperCode { 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 { if err != nil || v != params.VerificationCode {
log.NewError(params.OperationID, "password Verification code error", account, params.VerificationCode) log.NewError(params.OperationID, "password Verification code error", account, params.VerificationCode)
data := make(map[string]interface{}) data := make(map[string]interface{})
@ -65,7 +66,7 @@ func SetPassword(c *gin.Context) {
err = json.Unmarshal(bMsg, &openIMRegisterResp) err = json.Unmarshal(bMsg, &openIMRegisterResp)
if err != nil || openIMRegisterResp.ErrCode != 0 { if err != nil || openIMRegisterResp.ErrCode != 0 {
log.NewError(params.OperationID, "request openIM register error", account, "err", "") 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 return
} }
log.Info(params.OperationID, "begin store mysql", account, params.Password) log.Info(params.OperationID, "begin store mysql", account, params.Password)

@ -15,6 +15,7 @@ type paramsCertification struct {
PhoneNumber string `json:"phoneNumber"` PhoneNumber string `json:"phoneNumber"`
VerificationCode string `json:"verificationCode"` VerificationCode string `json:"verificationCode"`
OperationID string `json:"operationID" binding:"required"` OperationID string `json:"operationID" binding:"required"`
UsedFor int `json:"usedFor" binding:"required"`
} }
func Verify(c *gin.Context) { func Verify(c *gin.Context) {

@ -124,6 +124,12 @@ const (
//Minio //Minio
MinioDurationTimes = 3600 MinioDurationTimes = 3600
// verificationCode used for
VerificationCodeForRegister = 1
VerificationCodeForReset = 2
VerificationCodeForRegisterSuffix = "_forRegister"
VerificationCodeForResetSuffix = "_forReset"
) )
var ContentType2PushContent = map[int64]string{ var ContentType2PushContent = map[int64]string{

@ -80,6 +80,7 @@ const (
SmsSendCodeErr = 10008 SmsSendCodeErr = 10008
CodeInvalidOrExpired = 10009 CodeInvalidOrExpired = 10009
RegisterFailed = 10010 RegisterFailed = 10010
ResetPasswordFailed = 10011
DatabaseError = 10002 DatabaseError = 10002
ServerError = 10004 ServerError = 10004
HttpError = 10005 HttpError = 10005

@ -14,6 +14,7 @@ func GetRegister(account string) (*db.Register, error) {
return &r, dbConn.Table("registers").Where("account = ?", return &r, dbConn.Table("registers").Where("account = ?",
account).Take(&r).Error account).Take(&r).Error
} }
func SetPassword(account, password, ex string) error { func SetPassword(account, password, ex string) error {
r := db.Register{ r := db.Register{
Account: account, Account: account,
@ -25,5 +26,16 @@ func SetPassword(account, password, ex string) error {
return err return err
} }
return dbConn.Table("registers").Create(&r).Error 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 ( import (
"Open_IM/pkg/common/constant" "Open_IM/pkg/common/constant"
log2 "Open_IM/pkg/common/log" log2 "Open_IM/pkg/common/log"
"fmt"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
) )
const ( const (
registerAccountTempCode = "REGISTER_ACCOUNT_TEMP_CODE" AccountTempCode = "ACCOUNT_TEMP_CODE"
resetPwdTempCode = "RESET_PWD_TEMP_CODE"
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
appleDeviceToken = "DEVICE_TOKEN" appleDeviceToken = "DEVICE_TOKEN"
userMinSeq = "REDIS_USER_MIN_SEQ:" 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...) return con.Do(cmd, params...)
} }
func (d *DataBases) JudgeAccountEXISTS(account string) (bool, error) { func (d *DataBases) JudgeAccountEXISTS(account string) (bool, error) {
key := registerAccountTempCode + account key := AccountTempCode + account
return redis.Bool(d.Exec("EXISTS", key)) return redis.Bool(d.Exec("EXISTS", key))
} }
func (d *DataBases) SetAccountCode(account string, code, ttl int) (err error) { func (d *DataBases) SetAccountCode(account string, code, ttl int) (err error) {
key := registerAccountTempCode + account key := AccountTempCode + account
_, err = d.Exec("Set", key, code, ttl) _, err = d.Exec("SET", key, code)
return err return err
} }
func (d *DataBases) GetAccountCode(account string) (string, error) { func (d *DataBases) GetAccountCode(account string) (string, error) {
key := userIncrSeq + account key := AccountTempCode + account
fmt.Println(key)
return redis.String(d.Exec("GET", key)) return redis.String(d.Exec("GET", key))
} }
//Perform seq auto-increment operation of user messages //Perform seq auto-increment operation of user messages
func (d *DataBases) IncrUserSeq(uid string) (uint64, error) { func (d *DataBases) IncrUserSeq(uid string) (uint64, error) {
key := userIncrSeq + uid key := userIncrSeq + uid

Loading…
Cancel
Save