feat: check user email and phone when signing up

Signed-off-by: Kininaru <shiftregister233@outlook.com>

phone prefix error

Signed-off-by: Kininaru <shiftregister233@outlook.com>

fix i18n

Signed-off-by: Kininaru <shiftregister233@outlook.com>

fix i18n error

Signed-off-by: Kininaru <shiftregister233@outlook.com>

removed useless file

Signed-off-by: Kininaru <shiftregister233@outlook.com>

move timeout to app.conf

Signed-off-by: Kininaru <shiftregister233@outlook.com>

i18n

Signed-off-by: Kininaru <shiftregister233@outlook.com>

made verification code reusable

Signed-off-by: Kininaru <shiftregister233@outlook.com>
This commit is contained in:
Kininaru
2021-05-18 20:11:03 +08:00
parent 9bc29e25ef
commit 66d953a6c1
14 changed files with 151 additions and 37 deletions

View File

@ -36,6 +36,7 @@ type Provider struct {
RegionId string `xorm:"varchar(100)" json:"regionId"`
SignName string `xorm:"varchar(100)" json:"signName"`
TemplateCode string `xorm:"varchar(100)" json:"templateCode"`
AppId string `xorm:"varchar(100)" json:"appId"`
ProviderUrl string `xorm:"varchar(200)" json:"providerUrl"`
}

View File

@ -25,7 +25,7 @@ func SendCodeToPhone(phone, code string) string {
if provider == nil {
return "Please set an phone provider first"
}
client := go_sms_sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.RegionId, provider.TemplateCode)
client := go_sms_sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.RegionId, provider.TemplateCode, provider.AppId)
if client == nil {
return fmt.Sprintf("Unsupported provide type: %s", provider.Type)
}

View File

@ -19,6 +19,7 @@ import (
"math/rand"
"time"
"github.com/astaxie/beego"
"xorm.io/core"
)
@ -94,34 +95,54 @@ func AddToVerificationRecord(remoteAddr, recordType, dest, code string) string {
return ""
}
func CheckVerificationCode(dest, code string) string {
func getVerificationRecord(dest string) *VerificationRecord {
var record VerificationRecord
record.Receiver = dest
has, err := adapter.Engine.Desc("time").Where("is_used = 0").Get(&record)
if err != nil {
panic(err)
}
if !has {
return nil
}
return &record
}
func CheckVerificationCode(dest, code string) string {
record := getVerificationRecord(dest)
if record == nil {
return "Code has not been sent yet!"
}
timeout, err := beego.AppConfig.Int64("verificationCodeTimeout")
if err != nil {
panic(err)
}
now := time.Now().Unix()
if now-record.Time > 5*60 {
return "You should verify your code in 5 min!"
if now-record.Time > timeout*60 {
return fmt.Sprintf("You should verify your code in %d min!", timeout)
}
if record.Code != code {
return "Wrong code!"
}
return ""
}
func DisableVerificationCode(dest string) {
record := getVerificationRecord(dest)
if record == nil {
return
}
record.IsUsed = true
_, err = adapter.Engine.ID(core.PK{record.RemoteAddr, record.Type}).AllCols().Update(record)
_, err := adapter.Engine.ID(core.PK{record.RemoteAddr, record.Type}).AllCols().Update(record)
if err != nil {
panic(err)
}
return ""
}
// from Casnode/object/validateCode.go line 116