-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
130 lines (115 loc) · 4.87 KB
/
Copy pathauth.js
File metadata and controls
130 lines (115 loc) · 4.87 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
(function () {
'use strict';
const msgEl = document.getElementById('message');
const next = new URLSearchParams(location.search).get('next') || 'account.html';
const googleLoginBtn = document.getElementById('googleLoginBtn');
const authCard = document.getElementById('authCard');
const modeSignin = document.getElementById('modeSignin');
const modeSignup = document.getElementById('modeSignup');
const form = document.getElementById('oneAuthForm');
const emailInput = document.getElementById('authEmail');
const passwordInput = document.getElementById('authPassword');
const togglePasswordBtn = document.getElementById('togglePassword');
const submitBtn = document.getElementById('oneAuthSubmit');
const GOOGLE_REDIRECT_URL = 'https://hyphsworld.com/account.html';
let mode = 'signin';
function show(text, type) {
if (!msgEl) return;
msgEl.textContent = text;
msgEl.className = 'message ' + (type || '');
}
function redirectToNext(delay) {
setTimeout(() => { location.href = next; }, delay || 400);
}
async function refreshPoints() {
try {
if (window.HWPoints && typeof window.HWPoints.refresh === 'function') {
await window.HWPoints.refresh();
if (typeof window.HWPoints.render === 'function') window.HWPoints.render();
}
} catch (error) {}
}
function setMode(nextMode) {
mode = nextMode === 'signup' ? 'signup' : 'signin';
if (authCard) {
authCard.classList.toggle('mode-signup', mode === 'signup');
authCard.classList.toggle('mode-signin', mode === 'signin');
}
if (modeSignin) {
modeSignin.classList.toggle('is-active', mode === 'signin');
modeSignin.setAttribute('aria-selected', mode === 'signin' ? 'true' : 'false');
}
if (modeSignup) {
modeSignup.classList.toggle('is-active', mode === 'signup');
modeSignup.setAttribute('aria-selected', mode === 'signup' ? 'true' : 'false');
}
if (submitBtn) submitBtn.textContent = mode === 'signup' ? 'Create ID' : 'Login';
if (passwordInput) passwordInput.setAttribute('autocomplete', mode === 'signup' ? 'new-password' : 'current-password');
show(mode === 'signup' ? 'Create one HYPHSWORLD ID. Use it everywhere.' : 'Login with your existing HYPHSWORLD ID.', '');
}
function togglePassword() {
if (!passwordInput || !togglePasswordBtn) return;
const showing = passwordInput.type === 'text';
passwordInput.type = showing ? 'password' : 'text';
togglePasswordBtn.textContent = showing ? 'See' : 'Hide';
togglePasswordBtn.setAttribute('aria-pressed', showing ? 'false' : 'true');
}
async function submitAuth(event) {
event.preventDefault();
const email = emailInput ? emailInput.value.trim() : '';
const password = passwordInput ? passwordInput.value : '';
if (!email || !password) return show('Email and password required.', 'error');
if (submitBtn) submitBtn.disabled = true;
try {
if (mode === 'signup') {
await HWAuth.signUpWithEmail(email, password);
await refreshPoints();
show('ID created. Opening your account…', 'success');
} else {
await HWAuth.signInWithEmail(email, password);
await refreshPoints();
show('Logged in. Opening your account…', 'success');
}
redirectToNext(450);
} catch (error) {
const text = String(error && error.message || 'Auth failed.');
if (mode === 'signin' && /invalid|credential|not found|email/i.test(text)) {
show('Login failed. New here? Tap Create ID once.', 'error');
} else if (mode === 'signup' && /already|registered|exists/i.test(text)) {
setMode('signin');
show('That email already has an ID. Login instead.', 'warn');
} else {
show(text, 'error');
}
} finally {
if (submitBtn) submitBtn.disabled = false;
}
}
async function googleLogin() {
try {
if (googleLoginBtn) googleLoginBtn.disabled = true;
show('Opening Google ID tunnel…', 'success');
await HWAuth.signInWithGoogle({ redirectTo: GOOGLE_REDIRECT_URL });
} catch (error) {
if (googleLoginBtn) googleLoginBtn.disabled = false;
show(error.message || 'Google login failed.', 'error');
}
}
async function boot() {
setMode('signin');
try {
const session = await HWAuth.getSession();
if (session) {
await refreshPoints();
show('Already logged in. Opening account management…', 'success');
redirectToNext(350);
}
} catch (error) {}
if (modeSignin) modeSignin.addEventListener('click', () => setMode('signin'));
if (modeSignup) modeSignup.addEventListener('click', () => setMode('signup'));
if (togglePasswordBtn) togglePasswordBtn.addEventListener('click', togglePassword);
if (googleLoginBtn) googleLoginBtn.addEventListener('click', googleLogin);
if (form) form.addEventListener('submit', submitAuth);
}
boot();
})();