-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
103 lines (83 loc) · 2.93 KB
/
test.html
File metadata and controls
103 lines (83 loc) · 2.93 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
<!DOCTYPE html>
<html>
<head>
<title>GunDB Test</title>
</head>
<body>
<h1>GunDB Login/Register Test</h1>
<h3>Register</h3>
<input type="text" id="reg-user" placeholder="Username">
<input type="password" id="reg-pass" placeholder="Password">
<button onclick="register()">Register</button>
<h3>Login</h3>
<input type="text" id="login-user" placeholder="Username">
<input type="password" id="login-pass" placeholder="Password">
<button onclick="login()">Login</button>
<h3>Status</h3>
<div id="status">Waiting...</div>
<div id="current-user">Not logged in</div>
<!-- Load Gun and SEA from CDN -->
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>
<script>
// Initialize Gun
var gun = Gun({
peers: ['https://shogun-relay.scobrudot.dev/gun'],
localStorage: false,
radisk: false,
});
var user = gun.user();
window.gun = gun;
console.log(gun)
function updateStatus(msg) {
document.getElementById('status').innerText = msg;
console.log(msg);
}
function register() {
var u = document.getElementById('reg-user').value;
var p = document.getElementById('reg-pass').value;
if (!u || !p) {
updateStatus('Username and password required');
return;
}
updateStatus('Registering...');
user.create(u, p, function (ack) {
if (ack.err) {
updateStatus('Error: ' + ack.err);
} else {
updateStatus('Registered successfully! Public Key: ' + ack.pub);
}
});
}
function login() {
var u = document.getElementById('login-user').value;
var p = document.getElementById('login-pass').value;
if (!u || !p) {
updateStatus('Username and password required');
return;
}
updateStatus('Logging in...');
user.auth(u, p, function (ack) {
if (ack.err) {
updateStatus('Error: ' + ack.err);
} else {
updateStatus('Logged in successfully!');
showCurrentUser();
}
});
}
function showCurrentUser() {
if (user.is) {
document.getElementById('current-user').innerText = 'Current User Pub: ' + user.is.pub;
} else {
document.getElementById('current-user').innerText = 'Not logged in';
}
}
// Listen for auth events
gun.on('auth', function () {
// This event fires when authentication happens (login or session restore)
showCurrentUser();
});
</script>
</body>
</html>