feat(app): 🔒 add password change command

master
Xeronith 2023-06-05 11:29:33 +03:30
rodzic 7357551a76
commit 03f12913da
1 zmienionych plików z 29 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,29 @@
package commands
import (
. "github.com/reiver/greatape/components/constants"
. "github.com/reiver/greatape/components/contracts"
)
func ChangePassword(x IDispatcher, currentPassword string, newPassword string) (IChangePasswordResult, error) {
identity := x.Identity().(IIdentity)
if len(identity.Token()) < 10 {
return nil, ERROR_ACCOUNT_NOT_VERIFIED
}
if x.GenerateHash(currentPassword, identity.Salt()) != identity.Hash() {
return nil, ERROR_INVALID_CURRENT_PASSWORD_FOR_CHANGE_PASSWORD
}
hash := x.GenerateHash(newPassword, identity.Salt())
token := x.GenerateJwtToken()
x.Atomic(func() error {
identity.UpdateHashAtomic(x.Transaction(), hash, identity)
identity.UpdateTokenAtomic(x.Transaction(), token, identity)
x.IdentityManager().RefreshTokenCache(identity, token)
return nil
})
return x.NewChangePasswordResult(), nil
}