-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_in_recovery_code.go
57 lines (45 loc) · 1.21 KB
/
sign_in_recovery_code.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
package account
import (
"context"
"fmt"
"github.com/polyscone/tofu/app"
"github.com/polyscone/tofu/errsx"
)
func (s *Service) signInWithRecoveryCode(ctx context.Context, userID, recoveryCode string) error {
var input struct {
userID UserID
recoveryCode RecoveryCode
}
{
var err error
var errs errsx.Map
if input.userID, err = s.repo.ParseUserID(userID); err != nil {
errs.Set("user id", err)
}
if input.recoveryCode, err = NewRecoveryCode(recoveryCode); err != nil {
errs.Set("recovery code", err)
}
if errs != nil {
return fmt.Errorf("%w: %w", app.ErrMalformedInput, errs)
}
}
user, err := s.repo.FindUserByID(ctx, input.userID.String())
if err != nil {
return fmt.Errorf("find user by id: %w", err)
}
if err := user.SignInWithRecoveryCode(s.system, input.recoveryCode); err != nil {
return err
}
if err := s.repo.SaveUser(ctx, user); err != nil {
return fmt.Errorf("save user: %w", err)
}
s.broker.Flush(&user.Events)
return nil
}
func (s *Service) SignInWithRecoveryCode(ctx context.Context, userID, recoveryCode string) error {
err := s.signInWithRecoveryCode(ctx, userID, recoveryCode)
if err != nil {
return fmt.Errorf("%w: %w", ErrAuth, err)
}
return nil
}