3
3
package main
4
4
5
5
import (
6
- "log"
7
6
"net/http"
8
7
9
8
"github.com/geosoft1/token"
@@ -22,12 +21,8 @@ func reset(w http.ResponseWriter, r *http.Request) {
22
21
http .Error (w , http .StatusText (http .StatusNotAcceptable ), http .StatusNotAcceptable )
23
22
return
24
23
}
25
- // TODO reset password to random, uncomment the following 4 lines
24
+ // generate a random password
26
25
user .Password = token .GetToken (token_len )
27
- if err := sqlUpdateUser (& user ); err != nil {
28
- http .Error (w , http .StatusText (http .StatusNotAcceptable ), http .StatusNotAcceptable )
29
- return
30
- }
31
26
// https://stackoverflow.com/a/24431749
32
27
mail := gomail .NewMessage ()
33
28
mail .SetAddressHeader ("From" , config .SMTP .User , config .SMTP .Name )
@@ -37,7 +32,13 @@ func reset(w http.ResponseWriter, r *http.Request) {
37
32
dialer := gomail .NewPlainDialer (config .SMTP .Server , config .SMTP .Port , config .SMTP .User , config .SMTP .Password )
38
33
//dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
39
34
if err := dialer .DialAndSend (mail ); err != nil {
40
- log .Println (err )
35
+ http .Error (w , http .StatusText (http .StatusServiceUnavailable ), http .StatusServiceUnavailable )
36
+ return
37
+ }
38
+ // reset the password to random only if the mail was successfully sent
39
+ if err := sqlUpdateUser (& user ); err != nil {
40
+ http .Error (w , http .StatusText (http .StatusNotAcceptable ), http .StatusNotAcceptable )
41
+ return
41
42
}
42
43
w .Header ().Set ("Content-Type" , "text/html; charset=utf-8" )
43
44
w .Write ([]byte ("A mail with instructions was send, read and <a href=\" /\" >sign in</a>" ))
0 commit comments