initial refactoring to use a pure html/js login screen

This commit is contained in:
2025-05-25 22:05:35 -06:00
parent 457d6cee96
commit 2ba48354ac
6 changed files with 264 additions and 70 deletions
+16 -8
View File
@@ -2,9 +2,15 @@ import gleam/bit_array
import gleam/crypto
import gleam/dynamic/decode
import gleam/json
import gleam/option.{type Option}
pub type PlayerSession {
PlayerSession(id: Int, token_hash: String, username: String, socket_id: Int)
PlayerSession(
id: Int,
token_hash: String,
username: String,
socket_id: Option(Int),
)
}
pub fn encode_player_session(player_session: PlayerSession) -> json.Json {
@@ -12,7 +18,10 @@ pub fn encode_player_session(player_session: PlayerSession) -> json.Json {
#("id", json.int(player_session.id)),
#("token_hash", json.string(player_session.token_hash)),
#("username", json.string(player_session.username)),
#("socket_id", json.int(player_session.socket_id)),
#("socket_id", case player_session.socket_id {
option.None -> json.null()
option.Some(value) -> json.int(value)
}),
])
}
@@ -20,14 +29,13 @@ pub fn player_session_decoder() -> decode.Decoder(PlayerSession) {
use id <- decode.field("id", decode.int)
use token_hash <- decode.field("token_hash", decode.string)
use username <- decode.field("username", decode.string)
use socket_id <- decode.field("socket_id", decode.int)
use socket_id <- decode.field("socket_id", decode.optional(decode.int))
decode.success(PlayerSession(id:, token_hash:, username:, socket_id:))
}
pub fn hash_token(token: String) -> String {
let token =
crypto.new_hasher(crypto.Sha512)
|> crypto.hash_chunk(bit_array.from_string(token))
|> crypto.digest
|> bit_array.base64_encode(True)
crypto.new_hasher(crypto.Sha512)
|> crypto.hash_chunk(bit_array.from_string(token))
|> crypto.digest
|> bit_array.base64_encode(True)
}