Repository navigation
Expand file tree
/
Copy pathauth.js
More file actions
89 lines (82 loc) · 3.66 KB
/
Copy pathauth.js
File metadata and controls
89 lines (82 loc) · 3.66 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
// TechStore – Authentication
function getUser() {
const u = localStorage.getItem('ts_user');
return u ? JSON.parse(u) : null;
}
function setUser(user) {
localStorage.setItem('ts_user', JSON.stringify(user));
}
function logout() {
localStorage.removeItem('ts_user');
updateNav();
showToast('Signed out successfully');
setTimeout(() => { window.location.href = getRootPath() + 'index.html'; }, 600);
}
function openAuth(mode = 'login') {
document.getElementById('authModal').classList.remove('hidden');
renderAuthForm(mode);
}
function renderAuthForm(mode) {
document.getElementById('authTitle').textContent = mode === 'login' ? 'Sign In' : 'Create Account';
const f = document.getElementById('authForm');
if (mode === 'login') {
f.innerHTML = `
<div class="form-group">
<label>Email</label>
<input id="authEmail" type="email" placeholder="you@example.com" value="demo@techstore.in">
</div>
<div class="form-group">
<label>Password</label>
<input id="authPass" type="password" placeholder="Password" value="demo123">
</div>
<p id="authErr" class="error-text"></p>
<div class="btn-row">
<button class="btn btn-secondary" onclick="document.getElementById('authModal').classList.add('hidden')">Cancel</button>
<button class="btn btn-primary" onclick="doLogin()">Sign In</button>
</div>
<p class="auth-switch">No account? <a href="#" onclick="renderAuthForm('register');return false">Register</a></p>
<p class="auth-hint">Admin demo: admin@techstore.in / admin123</p>`;
} else {
f.innerHTML = `
<div class="form-group"><label>Full Name</label><input id="regName" placeholder="Your Name"></div>
<div class="form-row">
<div class="form-group"><label>Email</label><input id="regEmail" type="email" placeholder="you@example.com"></div>
<div class="form-group"><label>Phone</label><input id="regPhone" placeholder="+91 98765 43210"></div>
</div>
<div class="form-group"><label>Password</label><input id="regPass" type="password" placeholder="Create password"></div>
<p id="authErr" class="error-text"></p>
<div class="btn-row">
<button class="btn btn-secondary" onclick="renderAuthForm('login')">Back</button>
<button class="btn btn-primary" onclick="doRegister()">Create Account</button>
</div>`;
}
}
function doLogin() {
const e = document.getElementById('authEmail').value.trim();
const p = document.getElementById('authPass').value;
const err = document.getElementById('authErr');
if (!e || !p) { err.textContent = 'Please fill in all fields'; return; }
if (e === 'admin@techstore.in' && p === 'admin123') {
setUser({ name: 'Admin User', email: e, admin: true });
} else if (e && p.length >= 4) {
setUser({ name: e.split('@')[0].replace(/[._]/g, ' '), email: e, admin: false });
} else {
err.textContent = 'Invalid credentials. Try demo@techstore.in / demo123';
return;
}
document.getElementById('authModal').classList.add('hidden');
updateNav();
showToast('Welcome back!');
}
function doRegister() {
const n = document.getElementById('regName').value.trim();
const e = document.getElementById('regEmail').value.trim();
const p = document.getElementById('regPass').value;
const err = document.getElementById('authErr');
if (!n || !e || !p) { err.textContent = 'Please fill in all fields'; return; }
if (p.length < 4) { err.textContent = 'Password must be at least 4 characters'; return; }
setUser({ name: n, email: e, admin: false });
document.getElementById('authModal').classList.add('hidden');
updateNav();
showToast('Account created! Welcome, ' + n.split(' ')[0] + '!');
}