-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify_user.go
79 lines (62 loc) · 1.72 KB
/
verify_user.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package account
import (
"context"
"fmt"
"time"
"github.com/polyscone/tofu/app"
"github.com/polyscone/tofu/errsx"
)
type VerifyUserBehaviour byte
const (
VerifyUserOnly VerifyUserBehaviour = iota
VerifyUserActivate
)
func (s *Service) VerifyUser(ctx context.Context, email, password, passwordCheck string, behaviour VerifyUserBehaviour) error {
var input struct {
email Email
password Password
passwordCheck Password
}
{
var err error
var errs errsx.Map
input.passwordCheck, _ = NewPassword(passwordCheck)
if input.email, err = NewEmail(email); err != nil {
errs.Set("email", err)
}
if input.password, err = NewPassword(password); err != nil {
errs.Set("password", err)
} else if !input.password.Equal(input.passwordCheck) {
errs.Set("password", "passwords do not match")
}
if errs != nil {
return fmt.Errorf("%w: %w", app.ErrMalformedInput, errs)
}
}
log, err := s.repo.FindSignInAttemptLogByEmail(ctx, email)
if err != nil {
return fmt.Errorf("find sign in attempt log by email: %w", err)
}
user, err := s.repo.FindUserByEmail(ctx, input.email.String())
if err != nil {
return fmt.Errorf("find user by email: %w", err)
}
if err := user.Verify(input.password, s.hasher); err != nil {
return err
}
if isInvited := !user.InvitedAt.IsZero(); isInvited || behaviour == VerifyUserActivate {
if err := user.Activate(); err != nil {
return err
}
}
log.Attempts = 0
log.LastAttemptAt = time.Time{}
if err := s.repo.SaveSignInAttemptLog(ctx, log); err != nil {
return fmt.Errorf("save sign in attempt log: %w", err)
}
if err := s.repo.SaveUser(ctx, user); err != nil {
return fmt.Errorf("save user: %w", err)
}
s.broker.Flush(&user.Events)
return nil
}