Add Regex matching

This commit is contained in:
Marco Realacci 2022-11-29 16:07:42 +01:00
parent 758be58b31
commit df5b3fdda8
3 changed files with 29 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package api
import (
"net/http"
"regexp"
"github.com/julienschmidt/httprouter"
"github.com/notherealmarco/WASAPhoto/service/api/authorization"
@ -22,6 +23,18 @@ func (rt *_router) UpdateUsername(w http.ResponseWriter, r *http.Request, ps htt
return
}
stat, err := regexp.Match(`^[a-zA-Z0-9_]{3,16}$`, []byte(req.Name))
if err != nil {
helpers.SendInternalError(err, "Error while matching username", w, rt.baseLogger)
return
}
if !stat { //todo: sta regex non me piace
helpers.SendBadRequest(w, "Username must be between 3 and 16 characters long and can only contain letters, numbers and underscores", rt.baseLogger)
return
}
status, err := rt.db.UpdateUsername(uid, req.Name)
if status == database.ERR_EXISTS {