Merge pull request 'fix login' (#17) from fix-login into master
Reviewed-on: #17
This commit is contained in:
commit
fca744f17a
|
@ -54,3 +54,14 @@ func GetValueByKey(key string) ([]byte, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
return c.Get(ctx, key).Bytes()
|
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
|
package config
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
const (
|
||||||
|
MaximumLoginFailedTime = 5
|
||||||
|
LoginFailedBlockedDuration = time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
EmailIsUnique bool
|
EmailIsUnique bool
|
||||||
PhoneNumberIsUnique bool
|
PhoneNumberIsUnique bool
|
||||||
|
|
|
@ -28,5 +28,7 @@ const (
|
||||||
|
|
||||||
// Incorrect
|
// Incorrect
|
||||||
|
|
||||||
ErrorIncorrectPassword = "mật khẩu không chính xác"
|
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"
|
||||||
)
|
)
|
||||||
|
|
|
@ -578,16 +578,22 @@ func ChangeAllUsersStatus(roleID, status string) error {
|
||||||
// LoginWithEmailAndPassword ...
|
// LoginWithEmailAndPassword ...
|
||||||
func LoginWithEmailAndPassword(email, password string) (result model.User, err error) {
|
func LoginWithEmailAndPassword(email, password string) (result model.User, err error) {
|
||||||
var (
|
var (
|
||||||
ctx = context.Background()
|
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
|
// Validate email, password
|
||||||
if email == "" {
|
if email == "" {
|
||||||
err = errors.New(internal.ErrorInvalidEmail)
|
err = errors.New(internal.ErrorInvalidLogin)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if password == "" {
|
if password == "" {
|
||||||
err = errors.New(internal.ErrorInvalidPassword)
|
err = errors.New(internal.ErrorInvalidLogin)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -597,13 +603,14 @@ func LoginWithEmailAndPassword(email, password string) (result model.User, err e
|
||||||
"deleted": false,
|
"deleted": false,
|
||||||
})
|
})
|
||||||
if user.ID.IsZero() {
|
if user.ID.IsZero() {
|
||||||
err = errors.New(internal.ErrorNotFoundUser)
|
err = errors.New(internal.ErrorInvalidLogin)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check Password
|
// Check Password
|
||||||
if !internal.CheckPasswordHash(password, user.HashedPassword) {
|
if !internal.CheckPasswordHash(password, user.HashedPassword) {
|
||||||
err = errors.New(internal.ErrorIncorrectPassword)
|
err = errors.New(internal.ErrorInvalidLogin)
|
||||||
|
cache.SetKeyValue(k, numOfLoginFail+1, config.LoginFailedBlockedDuration)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue