@@ -20,6 +20,11 @@ type ActivateData struct {
2020 Token string `json:"token"`
2121}
2222
23+ type ResetData struct {
24+ Email string `json:"email"`
25+ Token string `json:"token"`
26+ }
27+
2328func (app * application ) createAuthenticationTokenHandler (c echo.Context ) error {
2429
2530 var input struct {
@@ -83,7 +88,7 @@ func (app *application) createAuthenticationTokenHandler(c echo.Context) error {
8388func (app * application ) createPasswordResetTokenHandler (c echo.Context ) error {
8489
8590 var input struct {
86- Phone string `json:"phone " validate:"required,len=10 "`
91+ Email string `json:"email " validate:"required,email "`
8792 }
8893
8994 if err := c .Bind (& input ); err != nil {
@@ -94,15 +99,15 @@ func (app *application) createPasswordResetTokenHandler(c echo.Context) error {
9499 return c .JSON (http .StatusBadRequest , envelope {"error" : err .Error ()})
95100 }
96101
97- admin , err := app .store .GetAdminByEmail (c .Request ().Context (), input .Phone )
102+ admin , err := app .store .GetAdminByEmail (c .Request ().Context (), input .Email )
98103
99104 if err != nil {
100105 switch {
101106 case errors .Is (err , sql .ErrNoRows ):
102- slog .Error ("error fetching admin by phone number " , "error" , err )
103- return c .JSON (http .StatusNotFound , envelope {"error" : "invalid phone number or password " })
107+ slog .Error ("error fetching admin by email :createPasswordResetTokenHandler " , "error" , err )
108+ return c .JSON (http .StatusNotFound , envelope {"error" : "user admin not found " })
104109 default :
105- slog .Error ("error fetching admin by phone number " , "error" , err )
110+ slog .Error ("error fetching admin by email :createPasswordResetTokenHandler " , "error" , err )
106111 return c .JSON (http .StatusInternalServerError , envelope {"error" : "internal server error" })
107112 }
108113 }
@@ -115,20 +120,53 @@ func (app *application) createPasswordResetTokenHandler(c echo.Context) error {
115120
116121 token , err := app .store .NewToken (admin .ID , expiry , db .ScopePasswordReset )
117122 if err != nil {
118- slog .Error ("error generating new token" , "error" , err )
123+ slog .Error ("error generating token :createPasswordResetTokenHandler " , "error" , err )
119124 return c .JSON (http .StatusInternalServerError , envelope {"error" : "internal server error" })
120125 }
121126
122- msg := fmt .Sprintf ("Your password reset token is: %s" , token .Plaintext )
123-
124127 app .background (func () {
125128
126- // send mail here
129+ dt := ResetData {
130+ Email : admin .Email ,
131+ Token : token .Plaintext ,
132+ }
133+
134+ jsonData , err := json .Marshal (dt )
135+ if err != nil {
136+ slog .Error ("Error marshaling JSON" , "Error" , err )
137+ }
138+
139+ client := & http.Client {
140+ Timeout : 10 * time .Second ,
141+ }
142+
143+ req , err := http .NewRequest ("POST" , fmt .Sprintf ("%s/rent-resetpwd" , app .config .mailer_url ), bytes .NewBuffer (jsonData ))
144+ if err != nil {
145+ slog .Error ("Error creating request" , "Error" , err )
146+ }
147+
148+ req .Header .Set ("Content-Type" , "application/json" )
149+
150+ resp , err := client .Do (req )
151+ if err != nil {
152+ slog .Error ("Error sending request" , "Error" , err )
153+ }
154+ defer resp .Body .Close ()
155+
156+ respBody , err := io .ReadAll (resp .Body )
157+ if err != nil {
158+ slog .Error ("Error reading response" , "Error" , err )
159+ }
160+
161+ if resp .StatusCode != http .StatusOK {
162+ slog .Error (fmt .Sprintf ("Request failed with status code %d: %s" , resp .StatusCode , string (respBody )))
163+ }
164+
165+ slog .Info (fmt .Sprintf ("Email sent successfully to %s\n " , admin .Email ))
127166
128- _ = msg
129167 })
130168
131- return c .JSON (http .StatusAccepted , nil )
169+ return c .JSON (http .StatusOK , nil )
132170}
133171
134172func (app * application ) resendActivationTokenHandler (c echo.Context ) error {
0 commit comments