mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-05-24 00:24:06 +03:00
feat: update user profile (#280)
This commit is contained in:
parent
24a0a9f5ee
commit
86e8ad41cb
9 changed files with 192 additions and 7 deletions
|
@ -100,6 +100,63 @@ func Logout() echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// LoadProfile to load user information
|
||||
func LoadProfile(db store.IStore) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
|
||||
userInfo, err := db.GetUser()
|
||||
if err != nil {
|
||||
log.Error("Cannot get user information: ", err)
|
||||
}
|
||||
|
||||
return c.Render(http.StatusOK, "profile.html", map[string]interface{}{
|
||||
"baseData": model.BaseData{Active: "profile", CurrentUser: currentUser(c)},
|
||||
"userInfo": userInfo,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateProfile to update user information
|
||||
func UpdateProfile(db store.IStore) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
data := make(map[string]interface{})
|
||||
err := json.NewDecoder(c.Request().Body).Decode(&data)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"})
|
||||
}
|
||||
|
||||
username := data["username"].(string)
|
||||
password := data["password"].(string)
|
||||
|
||||
user, err := db.GetUser()
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, err.Error()})
|
||||
}
|
||||
|
||||
if username == "" {
|
||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid username"})
|
||||
} else {
|
||||
user.Username = username
|
||||
}
|
||||
|
||||
if password != "" {
|
||||
hash, err := util.HashPassword(password)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, err.Error()})
|
||||
}
|
||||
user.PasswordHash = hash
|
||||
}
|
||||
|
||||
if err := db.SaveUser(user); err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, err.Error()})
|
||||
}
|
||||
log.Infof("Updated admin user information successfully")
|
||||
|
||||
return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated admin user information successfully"})
|
||||
}
|
||||
}
|
||||
|
||||
// WireGuardClients handler
|
||||
func WireGuardClients(db store.IStore) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue