From 03f12913da76119bd889f7b55e4329e63499785f Mon Sep 17 00:00:00 2001 From: Xeronith Date: Mon, 5 Jun 2023 11:29:33 +0330 Subject: [PATCH] feat(app): :lock: add password change command --- app/commands/change_password.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 app/commands/change_password.go diff --git a/app/commands/change_password.go b/app/commands/change_password.go new file mode 100644 index 0000000..8b62070 --- /dev/null +++ b/app/commands/change_password.go @@ -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 +}