2021-06-01 10:51:35 +02:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
// @Summary Change Password
|
|
|
|
// @Description Change Password of current login user
|
2021-06-30 15:47:24 +02:00
|
|
|
// @Tags auth
|
2021-06-01 10:51:35 +02:00
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolean "if password was saved (e.g. `true`)"
|
|
|
|
// @Failure 400 {object} web.HTTPError
|
|
|
|
// @Failure 401 {object} web.HTTPError
|
|
|
|
// @Failure 500 {object} web.HTTPError
|
|
|
|
// @Router /api/v1/my/auth/password [post]
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param body body string false "new password"
|
2021-07-19 17:59:08 +02:00
|
|
|
func apiMyPassword(r *gin.Engine, ws *web.Service) {
|
|
|
|
r.POST("/api/v1/my/auth/password", MiddlewareLogin(ws), func(c *gin.Context) {
|
|
|
|
d, ok := GetCurrentUser(c, ws)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var password string
|
|
|
|
if err := c.BindJSON(&password); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, web.HTTPError{
|
2021-07-22 18:16:05 +02:00
|
|
|
Message: web.ErrAPIInvalidRequestFormat.Error(),
|
2021-07-19 17:59:08 +02:00
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := d.SetPassword(password); err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, web.HTTPError{
|
2021-07-22 18:16:05 +02:00
|
|
|
Message: ErrAPICreatePassword.Error(),
|
2021-07-19 17:59:08 +02:00
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-06-01 10:51:35 +02:00
|
|
|
|
2021-07-19 17:59:08 +02:00
|
|
|
if err := ws.DB.Save(&d).Error; err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, web.HTTPError{
|
2021-07-22 18:16:05 +02:00
|
|
|
Message: web.ErrAPIInternalDatabase.Error(),
|
2021-07-19 17:59:08 +02:00
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-06-01 10:51:35 +02:00
|
|
|
|
2021-07-19 17:59:08 +02:00
|
|
|
c.JSON(http.StatusOK, true)
|
2021-06-01 10:51:35 +02:00
|
|
|
})
|
|
|
|
}
|