-
Notifications
You must be signed in to change notification settings - Fork 0
/
resetpassword.go
58 lines (45 loc) · 1.44 KB
/
resetpassword.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package commands
import (
"fmt"
"github.com/Goldwin/ies-pik-cms/pkg/auth/dto"
"github.com/Goldwin/ies-pik-cms/pkg/auth/entities"
. "github.com/Goldwin/ies-pik-cms/pkg/common/commands"
)
const (
ResetPasswordErrorInvalidInput CommandErrorCode = 20401
ResetPasswordErrorFailedToVerifyAccount CommandErrorCode = 20402
)
type ResetPasswordCommand struct {
Input dto.PasswordResetInput
}
func (cmd ResetPasswordCommand) Execute(ctx CommandContext) CommandExecutionResult[dto.PasswordResult] {
token, err := ctx.PasswordRepository().GetResetCode(entities.EmailAddress(cmd.Input.Email))
if err != nil {
return CommandExecutionResult[dto.PasswordResult]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: SavePasswordErrorInvalidInput,
Message: fmt.Sprintf("Failed to fetch Reset Token: %s", err.Error()),
},
}
}
if token != cmd.Input.Code {
return CommandExecutionResult[dto.PasswordResult]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: SavePasswordErrorInvalidInput,
Message: fmt.Sprintf("Reset Token Mismatched"),
},
}
}
savePasswordCmd := SavePasswordCommand{
Email: entities.EmailAddress(cmd.Input.Email),
Password: []byte(cmd.Input.Password),
}
result := savePasswordCmd.Execute(ctx)
if result.Status != ExecutionStatusSuccess {
return result
}
err = ctx.PasswordRepository().DeleteResetToken(entities.EmailAddress(cmd.Input.Email))
return result
}