forked from kinde-starter-kits/javascript-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
168 lines (146 loc) · 5.31 KB
/
index.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kinde - JS Starter kit</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1, user-scalable=no"
/>
<style>
[hidden] {
display: none;
}
</style>
<link rel="stylesheet" href="./styles/index.css">
<body>
<header>
<nav class="nav container">
<h1 class="text-display-3">KindeAuth</h1>
<div class="js-logged-out-view">
<button id="login" class="btn btn-ghost sign-in-btn">
Sign in
</button>
<button id="register" class="btn btn-dark">
Sign up
</button>
</div>
<div class="js-logged-in-view" hidden>
<div class="profile-blob">
<div class="js-user-avatar avatar"></div>
<div>
<p class="js-user-name text-heading-2"></p>
<button id="logout" class="text-subtle">
Sign out
</button>
</div>
</div>
</div>
</nav>
</header>
<main>
<div class="container">
<div class="js-logged-out-view">
<div class="card hero">
<p class="text-display-1 hero-title">
Let's start authenticating <br /> with KindeAuth
</p>
<p class="text-body-1 hero-tagline">Configure your app</p>
<a
href="https://kinde.com/docs/sdks/javascript-sdk"
target="_blank"
rel="noreferrer"
class="btn btn-light btn-big"
>
Go to docs
</a>
</div>
</div>
<div class="js-logged-in-view" hidden>
<div class="card start-hero">
<p class="text-body-2 start-hero-intro">Woohoo!</p>
<p class="text-display-2">
Your authentication is all sorted.
<br />
Build the important stuff.
</p>
</div>
<section class="next-steps-section">
<h2 class="text-heading-1">Next steps for you</h2>
</section>
</div>
</div>
</main>
<footer class="footer">
<div class="container">
<strong class="text-heading-2">KindeAuth</strong>
<p class="footer-tagline text-body-3">
Visit our
<a class="link" href="https://kinde.com/docs">
help center
</a>
</p>
<small class="text-subtle">
© 2022 KindeAuth, Inc. All rights reserved
</small>
</div>
</footer>
<script src="https://unpkg.com/@kinde-oss/kinde-auth-pkce-js"></script>
<script>
'use strict';
(async function() {
const loggedInViews = document.getElementsByClassName('js-logged-in-view');
const loggedOutViews = document.getElementsByClassName('js-logged-out-view');
const switchViews = (a, b) => {
[...a].forEach(v => v.removeAttribute('hidden'));
[...b].forEach(v => v.setAttribute("hidden", true));
}
const renderLoggedInView = (user) => {
const namePlaceholder = document.querySelector('.js-user-name');
const avatarPlaceholder = document.querySelector('.js-user-avatar');
namePlaceholder.textContent = `${user.given_name} ${user?.family_name || ''}`;
avatarPlaceholder.textContent = `${user.given_name[0]}${user?.family_name?.[0] || user.given_name[1]}`;
switchViews(loggedInViews, loggedOutViews);
}
const renderLoggedOutView = () => {
const loggedInViews = document.getElementsByClassName('js-logged-in-view');
const loggedOutViews = document.getElementsByClassName('js-logged-out-view');
switchViews(loggedOutViews, loggedInViews);
}
const render = async (user) => {
if (user) {
renderLoggedInView(user);
} else {
renderLoggedOutView();
}
}
const kinde = await createKindeClient({
client_id: '<your_kinde_client_id>',
domain: 'https://<your_kinde_subdomain.kinde.com',
redirect_uri: 'http://localhost:3000',
on_redirect_callback: (user, appState) => {
console.log({user, appState});
if (user) {
renderLoggedInView(user);
} else {
renderLoggedOutView();
}
}
});
const addKindeEvent = id => {
document.getElementById(id).addEventListener('click', async () => {
await kinde[id]();
});
}
['login', 'register', 'logout'].forEach(addKindeEvent)
// Handle redirect
window.addEventListener('load', async () => {
const user = await kinde.getUser();
await render();
});
})();
</script>
</body>
</html>