fix login
This commit is contained in:
parent
3d1c1d7538
commit
cd94018a17
|
@ -54,3 +54,14 @@ func GetValueByKey(key string) ([]byte, error) {
|
|||
ctx := context.Background()
|
||||
return c.Get(ctx, key).Bytes()
|
||||
}
|
||||
|
||||
func GetJSON(key string, result interface{}) bool {
|
||||
v, err := GetValueByKey(key)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if err = json.Unmarshal(v, result); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package cache
|
||||
|
||||
const (
|
||||
keyPrefix = "usermngmt_"
|
||||
)
|
||||
|
||||
const (
|
||||
KeyLoginFailedTimes = keyPrefix + "login_failed_time"
|
||||
)
|
|
@ -1,5 +1,12 @@
|
|||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
MaximumLoginFailedTime = 5
|
||||
LoginFailedBlockedDuration = time.Hour
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
EmailIsUnique bool
|
||||
PhoneNumberIsUnique bool
|
||||
|
|
|
@ -29,4 +29,6 @@ const (
|
|||
// Incorrect
|
||||
|
||||
ErrorIncorrectPassword = "mật khẩu không chính xác"
|
||||
ErrorInvalidLogin = "thông tin đăng nhập không đúng"
|
||||
ErrorExceedMaximumLoginFail = "bạn đã đăng nhập sai thông tin quá số lần cho phép"
|
||||
)
|
||||
|
|
|
@ -579,15 +579,21 @@ func ChangeAllUsersStatus(roleID, status string) error {
|
|||
func LoginWithEmailAndPassword(email, password string) (result model.User, err error) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
numOfLoginFail int
|
||||
)
|
||||
k := cache.KeyLoginFailedTimes + email
|
||||
// process block if reach maximum of login failed
|
||||
if ok := cache.GetJSON(k, &numOfLoginFail); ok && numOfLoginFail >= config.MaximumLoginFailedTime {
|
||||
return model.User{}, errors.New(internal.ErrorExceedMaximumLoginFail)
|
||||
}
|
||||
|
||||
// Validate email, password
|
||||
if email == "" {
|
||||
err = errors.New(internal.ErrorInvalidEmail)
|
||||
err = errors.New(internal.ErrorInvalidLogin)
|
||||
return
|
||||
}
|
||||
if password == "" {
|
||||
err = errors.New(internal.ErrorInvalidPassword)
|
||||
err = errors.New(internal.ErrorInvalidLogin)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -597,13 +603,14 @@ func LoginWithEmailAndPassword(email, password string) (result model.User, err e
|
|||
"deleted": false,
|
||||
})
|
||||
if user.ID.IsZero() {
|
||||
err = errors.New(internal.ErrorNotFoundUser)
|
||||
err = errors.New(internal.ErrorInvalidLogin)
|
||||
return
|
||||
}
|
||||
|
||||
// Check Password
|
||||
if !internal.CheckPasswordHash(password, user.HashedPassword) {
|
||||
err = errors.New(internal.ErrorIncorrectPassword)
|
||||
err = errors.New(internal.ErrorInvalidLogin)
|
||||
cache.SetKeyValue(k, numOfLoginFail+1, config.LoginFailedBlockedDuration)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue