-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathindex.html
More file actions
62 lines (55 loc) · 1.95 KB
/
index.html
File metadata and controls
62 lines (55 loc) · 1.95 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>JWT authentication with Ably</title>
<link rel="stylesheet" href="css/style.css" />
<link
rel="icon"
href="https://static.ably.dev/motif-red.svg?jwt-authentication-nodejs"
type="image/svg+xml"
/>
<script
src="https://cdn.ably.io/lib/ably.min-2.js"
type="text/javascript"
></script>
<script type="text/javascript">
/*
Set up a Realtime client that authenticates
with the local web server auth endpoint
and outputs the JWT to the console.log
*/
function login(e) {
e.preventDefault();
const username = document.querySelector("#username").value;
const password = document.querySelector("#password").value;
if (!`${username}`.trim() || !`${password}`.trim()) {
return alert("Error! Require username and password");
}
// Send user details to the auth server
const authUrl = `/auth?clientId=${username}:${password}`;
const realtime = new Ably.Realtime({ authUrl });
console.log("Fetching JWT token from auth server", username, password);
realtime.connection.once("connected", () => {
const { tokenDetails } = realtime.auth;
console.log("Client connected to Ably using JWT", tokenDetails);
alert("Client successfully connected to Ably using JWT auth");
});
}
</script>
</head>
<body>
<section class="container">
<div class="login">
<h1>Ably JWT auth example</h1>
<form>
<label for="username">Username</label>
<input type="text" placeholder="Enter Username" id="username" />
<label for="password">Password</label>
<input type="password" placeholder="Password" id="password" />
<button type="submit" onclick="login(event)">Login</button>
</form>
</div>
</section>
</body>
</html>