Repository navigation
Expand file tree
/
Copy pathauth.js
More file actions
141 lines (130 loc) · 6.16 KB
/
Copy pathauth.js
File metadata and controls
141 lines (130 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// ============================================================================
// WELCOME SCREEN: SIGN UP / LOG IN BEFORE ENTERING THE MULTIVERSE
// ============================================================================
// With the Node server, accounts live on the server (scrypt-hashed passwords,
// session tokens). On static hosts without a server (solo mode), the account
// is kept only in this browser, and the screen says so.
const Auth = (() => {
const TOKEN_KEY = 'github-multiverse-session';
const LOCAL_ACCOUNTS_KEY = 'github-multiverse-local-accounts';
const hasServer = typeof io === 'function';
const screen = document.getElementById('auth-screen');
const form = document.getElementById('auth-form');
const tabs = document.querySelectorAll('#auth-tabs button');
const usernameInput = document.getElementById('auth-username');
const passwordInput = document.getElementById('auth-password');
const confirmRow = document.getElementById('auth-confirm-row');
const confirmInput = document.getElementById('auth-confirm');
const submitButton = document.getElementById('auth-submit');
const errorBox = document.getElementById('auth-error');
const note = document.getElementById('auth-note');
let mode = 'signup';
let resolveLogin;
function storage(action, key, value) {
try {
if (action === 'get') return localStorage.getItem(key);
if (action === 'set') localStorage.setItem(key, value);
if (action === 'remove') localStorage.removeItem(key);
} catch (error) {
return null;
}
return null;
}
function setMode(nextMode) {
mode = nextMode;
tabs.forEach((tab) => tab.classList.toggle('active', tab.dataset.mode === mode));
confirmRow.style.display = mode === 'signup' ? 'block' : 'none';
confirmInput.required = mode === 'signup';
passwordInput.autocomplete = mode === 'signup' ? 'new-password' : 'current-password';
submitButton.textContent = mode === 'signup' ? 'Create account & enter' : 'Log in';
errorBox.textContent = '';
}
async function hashLocally(password, salt) {
const data = new TextEncoder().encode(`${salt}:${password}`);
const digest = await crypto.subtle.digest('SHA-256', data);
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
}
// Solo mode: accounts saved in this browser only
async function localAuth(username, password) {
let local = {};
try { local = JSON.parse(storage('get', LOCAL_ACCOUNTS_KEY) || '{}'); } catch (error) { local = {}; }
const key = username.toLowerCase();
if (mode === 'signup') {
if (local[key]) throw new Error('That username already exists in this browser. Log in instead?');
const salt = Math.random().toString(36).slice(2);
local[key] = { username, salt, hash: await hashLocally(password, salt) };
storage('set', LOCAL_ACCOUNTS_KEY, JSON.stringify(local));
} else if (!local[key] || local[key].hash !== await hashLocally(password, local[key].salt)) {
throw new Error('Wrong username or password.');
}
return { token: `local:${key}`, username: local[key].username };
}
async function serverAuth(username, password) {
const response = await fetch(mode === 'signup' ? '/api/signup' : '/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
const body = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(body.error || 'Something went wrong. Try again.');
return body;
}
function enter(session) {
storage('set', TOKEN_KEY, JSON.stringify(session));
screen.classList.add('leaving');
setTimeout(() => { screen.style.display = 'none'; }, 600);
resolveLogin(session);
}
async function onSubmit(event) {
event.preventDefault();
errorBox.textContent = '';
const username = usernameInput.value.trim();
const password = passwordInput.value;
if (!/^[A-Za-z0-9_]{3,16}$/.test(username)) { errorBox.textContent = 'Usernames have 3-16 letters, numbers or _.'; return; }
if (password.length < 8) { errorBox.textContent = 'Passwords need at least 8 characters.'; return; }
if (mode === 'signup' && password !== confirmInput.value) { errorBox.textContent = 'The passwords do not match.'; return; }
submitButton.disabled = true;
try {
enter(hasServer ? await serverAuth(username, password) : await localAuth(username, password));
} catch (error) {
errorBox.textContent = error.message;
} finally {
submitButton.disabled = false;
}
}
// Resolves with { token, username } once the developer is signed in
function signIn() {
return new Promise(async (resolve) => {
resolveLogin = resolve;
tabs.forEach((tab) => tab.addEventListener('click', () => setMode(tab.dataset.mode)));
form.addEventListener('submit', onSubmit);
note.textContent = hasServer
? 'Your password is hashed on the server and never stored in plain text.'
: 'Solo mode: no multiplayer server found, so this account is saved only in this browser.';
let saved = null;
try { saved = JSON.parse(storage('get', TOKEN_KEY)); } catch (error) { saved = null; }
if (saved && saved.token) {
// Already signed in: skip straight into the world
const stillValid = hasServer
? await fetch('/api/session', { headers: { 'x-session-token': saved.token } }).then((response) => response.ok).catch(() => false)
: saved.token.startsWith('local:');
if (stillValid) { enter(saved); return; }
storage('remove', TOKEN_KEY);
setMode('login');
errorBox.textContent = 'Your session expired. Please log in again.';
} else {
setMode('signup');
}
screen.style.display = 'flex';
usernameInput.focus();
});
}
async function signOut() {
let saved = null;
try { saved = JSON.parse(storage('get', TOKEN_KEY)); } catch (error) { saved = null; }
if (saved && hasServer) await fetch('/api/logout', { method: 'POST', headers: { 'x-session-token': saved.token } }).catch(() => {});
storage('remove', TOKEN_KEY);
location.reload();
}
return { signIn, signOut, hasServer };
})();