Feat: add namecheck and tailwind css

This commit is contained in:
Grail Finder
2025-05-04 11:32:39 +03:00
parent 5dbb80121d
commit ca9b077070
10 changed files with 107 additions and 5 deletions

View File

@ -27,3 +27,24 @@ func getRoomIDFromCtx(ctx context.Context) string {
id, _ := ctx.Value(models.CtxRoomIDKey).(string)
return id
}
// cache
func getAllNames() []string {
names := []string{}
// will not scale
wholeMemStore := memcache.GetAll()
session := &models.Session{}
// filter by key size only sessions
for k, v := range wholeMemStore {
// xid is 20 in len
if len(k) != 20 {
continue
}
if err := json.Unmarshal(v, &session); err != nil {
log.Error("failed to unmarshal", "error", err)
continue
}
names = append(names, session.Username)
}
return names
}

View File

@ -7,6 +7,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"golias/models"
"golias/utils"
"html/template"
@ -21,6 +22,32 @@ func abortWithError(w http.ResponseWriter, msg string) {
tmpl.ExecuteTemplate(w, "error", msg)
}
func HandleNameCheck(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.PostFormValue("username")
if username == "" {
msg := "username not provided"
log.Error(msg)
abortWithError(w, msg)
return
}
cleanName := utils.RemoveSpacesFromStr(username)
allNames := getAllNames()
log.Info("names check", "taken_names", allNames, "trying_name", cleanName)
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
return
}
if utils.StrInSlice(cleanName, allNames) {
err := fmt.Errorf("name: %s already taken", cleanName)
log.Warn("already taken", "error", err)
tmpl.ExecuteTemplate(w, "namecheck", 2)
return
}
tmpl.ExecuteTemplate(w, "namecheck", 0)
}
func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.PostFormValue("username")
@ -46,14 +73,21 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
tmpl.ExecuteTemplate(w, "base", roundWords)
// session, ok :=r.Context().Value(models.CtxSessionKey).(*models.Session)
// if !ok{
// abortWithError(w, "failed to extract session from ctx")
// return
// }
// state := models.InitState(cleanName)
state := models.MakeTestState()
tmpl.ExecuteTemplate(w, "base", state)
}
func makeCookie(username string, remote string) (*http.Cookie, error) {
// secret
// Create a new random session token
// sessionToken := xid.New().String()
sessionToken := "token"
sessionToken := "sessionprefix_" + username
expiresAt := time.Now().Add(time.Duration(cfg.SessionLifetime) * time.Second)
// Set the token in the session map, along with the session information
session := &models.Session{