diff --git a/routes/settings.go b/routes/settings.go index 10a8579..b4084ed 100644 --- a/routes/settings.go +++ b/routes/settings.go @@ -7,6 +7,7 @@ import ( "github.com/muety/wakapi/services" "github.com/muety/wakapi/utils" "net/http" + "net/url" ) type SettingsHandler struct { @@ -28,6 +29,9 @@ func (h *SettingsHandler) GetIndex(w http.ResponseWriter, r *http.Request) { loadTemplates() } + if handleAlerts(w, r, "settings.tpl.html") { + return + } templates["settings.tpl.html"].Execute(w, nil) } @@ -87,11 +91,22 @@ func (h *SettingsHandler) PostCredentials(w http.ResponseWriter, r *http.Request HttpOnly: true, } http.SetCookie(w, cookie) - http.Redirect(w, r, fmt.Sprintf("%s/settings", h.config.BasePath), http.StatusFound) + + msg := url.QueryEscape("password was updated successfully") + http.Redirect(w, r, fmt.Sprintf("%s/settings?success=%s", h.config.BasePath, msg), http.StatusFound) } func (h *SettingsHandler) PostResetApiKey(w http.ResponseWriter, r *http.Request) { if h.config.IsDev() { loadTemplates() } + + user := r.Context().Value(models.UserKey).(*models.User) + if _, err := h.userSrvc.ResetApiKey(user); err != nil { + respondAlert(w, "internal server error", "", "settings.tpl.html", http.StatusInternalServerError) + return + } + + msg := url.QueryEscape(fmt.Sprintf("your new api key is: %s", user.ApiKey)) + http.Redirect(w, r, fmt.Sprintf("%s/settings?success=%s", h.config.BasePath, msg), http.StatusFound) } diff --git a/services/user.go b/services/user.go index 19802c8..c6a672c 100644 --- a/services/user.go +++ b/services/user.go @@ -82,6 +82,11 @@ func (srv *UserService) Update(user *models.User) (*models.User, error) { return user, nil } +func (srv *UserService) ResetApiKey(user *models.User) (*models.User, error) { + user.ApiKey = uuid.NewV4().String() + return srv.Update(user) +} + func (srv *UserService) MigrateMd5Password(user *models.User, login *models.Login) (*models.User, error) { user.Password = login.Password if err := utils.HashPassword(user, srv.Config.PasswordSalt); err != nil { diff --git a/version.txt b/version.txt index 9dbb0c0..081af9a 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.7.0 \ No newline at end of file +1.7.1 \ No newline at end of file diff --git a/views/settings.tpl.html b/views/settings.tpl.html index caea777..fea0b31 100644 --- a/views/settings.tpl.html +++ b/views/settings.tpl.html @@ -13,37 +13,57 @@ {{ template "alerts.tpl.html" . }} -
-
-
- Change Password +
+
+
+
+ Change Password +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
-
-
- - +
+
+ Reset API Key
-
- - -
-
- - -
-
- -
- + +
+
+ ⚠️ Caution: Resetting your API key requires you to update your .wakatime.cfg files on all of your computers to make the WakaTime client send heartbeats again. +
+ +
+ +
+
+