@@ -2,15 +2,19 @@ package server
2
2
3
3
import (
4
4
"context"
5
+ "database/sql"
5
6
6
7
"github.com/golang/protobuf/ptypes/empty"
7
8
"github.com/izumin5210/grapi/pkg/grapiserver"
9
+ "github.com/pkg/errors"
8
10
"google.golang.org/grpc/codes"
9
11
"google.golang.org/grpc/status"
10
12
11
13
api_pb "github.com/ProgrammingLab/prolab-accounts/api"
12
14
"github.com/ProgrammingLab/prolab-accounts/app/config"
13
15
"github.com/ProgrammingLab/prolab-accounts/app/di"
16
+ "github.com/ProgrammingLab/prolab-accounts/app/util"
17
+ "github.com/ProgrammingLab/prolab-accounts/model"
14
18
)
15
19
16
20
// PasswordResetServiceServer is a composite interface of api_pb.PasswordResetServiceServer and grapiserver.Server.
@@ -20,29 +24,61 @@ type PasswordResetServiceServer interface {
20
24
}
21
25
22
26
// NewPasswordResetServiceServer creates a new PasswordResetServiceServer instance.
23
- func NewPasswordResetServiceServer (store di.StoreComponent , cfg * config.Config ) PasswordResetServiceServer {
27
+ func NewPasswordResetServiceServer (store di.StoreComponent , cli di. ClientComponent , cfg * config.Config ) PasswordResetServiceServer {
24
28
return & passwordResetServiceServerImpl {
25
- StoreComponent : store ,
26
- cfg : cfg ,
29
+ StoreComponent : store ,
30
+ ClientComponent : cli ,
31
+ cfg : cfg ,
27
32
}
28
33
}
29
34
30
35
type passwordResetServiceServerImpl struct {
31
36
di.StoreComponent
37
+ di.ClientComponent
32
38
cfg * config.Config
33
39
}
34
40
35
- func (s * passwordResetServiceServerImpl ) GetPasswordReset (ctx context.Context , req * api_pb.GetPasswordResetRequest ) (* api_pb.PasswordReset , error ) {
36
- // TODO: Not yet implemented.
37
- return nil , status .Error (codes .Unimplemented , "TODO: You should implement it!" )
41
+ func (s * passwordResetServiceServerImpl ) GetPasswordReset (ctx context.Context , req * api_pb.GetPasswordResetRequest ) (* empty.Empty , error ) {
42
+ ps := s .PasswordResetStore (ctx )
43
+ _ , err := ps .GetConfirmation (req .GetEmail (), req .GetToken ())
44
+ if err != nil {
45
+ if errors .Cause (err ) == sql .ErrNoRows {
46
+ return nil , util .ErrNotFound
47
+ }
48
+ return nil , err
49
+ }
50
+
51
+ return & empty.Empty {}, nil
38
52
}
39
53
40
- func (s * passwordResetServiceServerImpl ) CreatePasswordReset (ctx context.Context , req * api_pb.CreatePasswordResetRequest ) (* api_pb.PasswordReset , error ) {
41
- // TODO: Not yet implemented.
42
- return nil , status .Error (codes .Unimplemented , "TODO: You should implement it!" )
54
+ func (s * passwordResetServiceServerImpl ) CreatePasswordReset (ctx context.Context , req * api_pb.CreatePasswordResetRequest ) (* empty.Empty , error ) {
55
+ email := req .GetEmail ()
56
+
57
+ us := s .UserStore (ctx )
58
+ u , err := us .GetUserByEmail (email )
59
+ if err != nil {
60
+ if errors .Cause (err ) == sql .ErrNoRows {
61
+ return & empty.Empty {}, nil
62
+ }
63
+ return nil , err
64
+ }
65
+
66
+ ps := s .PasswordResetStore (ctx )
67
+ p , token , err := ps .CreateConfirmation (model .UserID (u .ID ), email )
68
+ if err != nil {
69
+ return nil , err
70
+ }
71
+
72
+ sender := s .EmailSender (ctx )
73
+ err = sender .SendPasswordReset (p , token )
74
+ if err != nil {
75
+ return nil , err
76
+ }
77
+
78
+ return & empty.Empty {}, nil
43
79
}
44
80
45
- func (s * passwordResetServiceServerImpl ) UpdatePassword (ctx context.Context , req * api_pb.UpdatePasswordRequest ) (* empty. Empty , error ) {
81
+ func (s * passwordResetServiceServerImpl ) UpdatePassword (ctx context.Context , req * api_pb.UpdatePasswordRequest ) (* api_pb. Session , error ) {
46
82
// TODO: Not yet implemented.
47
83
return nil , status .Error (codes .Unimplemented , "TODO: You should implement it!" )
48
84
}
0 commit comments