Add saving user, add nonpersistant register login handling

This commit is contained in:
2026-03-17 21:40:48 +01:00
parent e496cb0017
commit d3fc2a65d9
9 changed files with 160 additions and 25 deletions
+11 -7
View File
@@ -11,7 +11,7 @@ import (
"github.com/coder/websocket/wsjson"
)
func ServeConnection(responseWriter http.ResponseWriter, request *http.Request) {
func ServeWsConnection(responseWriter http.ResponseWriter, request *http.Request) {
connection, err := websocket.Accept(responseWriter, request, nil)
if err != nil {
log.Printf("websocket accept error: %v", err)
@@ -33,7 +33,7 @@ func ServeConnection(responseWriter http.ResponseWriter, request *http.Request)
if len(clientMessage) > 0 {
if client.IsAuthenticated {
handleAuthenticatedMessage()
handleAuthenticatedMessage(connection, &client, &clientMessage)
} else {
if !handleUnauthenticatedMessage(connection, &client, &clientMessage) {
closeConnection(connection)
@@ -61,7 +61,7 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
token, ok := (*message)["token"].(string)
if !ok {
var errmsg = map[string]any{
"type": WSServerResponse(BadMessage),
"type": WsServerResponse(BadMessage),
"message": "token required",
}
sendMessageCloseIfTimeout(conn, &errmsg)
@@ -71,7 +71,7 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
clientId, err := GetClientIdFromToken(token)
if err != nil {
var errmsg = map[string]any{
"type": WSServerResponse(InvalidCredentials),
"type": WsServerResponse(InvalidCredentials),
"message": "bad token",
}
sendMessageCloseIfTimeout(conn, &errmsg)
@@ -81,7 +81,7 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
client, err = GetClientFromId(clientId)
if err != nil {
var errmsg = map[string]any{
"type": WSServerResponse(InvalidCredentials),
"type": WsServerResponse(InvalidCredentials),
"message": "bad token",
}
sendMessageCloseIfTimeout(conn, &errmsg)
@@ -92,8 +92,12 @@ func handleUnauthenticatedMessage(conn *websocket.Conn, client *Client, message
return true
}
func handleAuthenticatedMessage() {
func handleAuthenticatedMessage(conn *websocket.Conn, client *Client, message *map[string]any) {
for _, sendTo := range clients {
if sendTo.IsAuthenticated && sendTo.Id != client.Id {
sendMessageCloseIfTimeout(conn, message)
}
}
}
func closeConnection(conn *websocket.Conn) {