diff --git a/src/index.js b/src/index.js index a39a9762915693ada0adf431f655b3b71f0379f3..918898be4a7338e270c7640d1d4d7eb7dbac4f53 100644 --- a/src/index.js +++ b/src/index.js @@ -27,7 +27,7 @@ import "./systems/personal-space-bubble"; import registerNetworkScheams from "./network-schemas"; import registerInputMappings from "./input-mappings"; -import { promptForName } from "./utils"; +import { promptForName, getCookie, parseJwt } from "./utils"; import Config from "./config"; registerNetworkScheams(); @@ -77,10 +77,20 @@ window.App = { playerRig.setAttribute("virtual-gamepad-controls", {}); } - let username = qs.name; - if (!username) { + let username; + const jwt = getCookie("jwt"); + if (jwt) { //grab name from jwt + const data = parseJwt(jwt); + username = data.typ.name; + } + + if (qs.name) { + username = qs.name; //always override with name from querystring if available + } + else { username = promptForName(username); // promptForName is blocking } + const myNametag = document.querySelector("#player-rig .nametag"); myNametag.setAttribute("text", "value", username); diff --git a/src/utils.js b/src/utils.js index bc8a96af6c4d9f63e74016c98354f70a335024c9..b37764120a388120c89392fb6b9b97f31dd59578 100644 --- a/src/utils.js +++ b/src/utils.js @@ -166,10 +166,24 @@ export function generateName() { return name.replace(/^./, name[0].toUpperCase()); } -export function promptForName() { - var username = generateName(); +export function promptForName(username) { + if (!username) + username = generateName(); + do { username = prompt("Choose a username", username); } while (!(username && username.length)); return username; } + +export function getCookie(name) { + var value = "; " + document.cookie; + var parts = value.split("; " + name + "="); + if (parts.length == 2) return parts.pop().split(";").shift(); +} + +export function parseJwt (token) { + var base64Url = token.split('.')[1]; + var base64 = base64Url.replace('-', '+').replace('_', '/'); + return JSON.parse(window.atob(base64)); +}