-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
293 lines (263 loc) · 14.2 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<html>
<head>
<link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" type="text/css">
<script>
function loadFirebase(config) {
firebase.initializeApp(config);
localStorage.setItem('validConfig', 'true');
$('.setup-step').hide();
$('#loaded-part').show();
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
user.getIdToken().then(function(token) {
const userData = {
uid: user.uid,
displayName: user.displayName,
email: user.email,
phoneNumber: user.phoneNumber
};
$('#user').text(JSON.stringify(userData, null, 2));
$('#result').text(token)
$('#jwtio').show();
$('#jwtio').attr('href', `https://jwt.io/#debugger-io?token=${token}`);
})
}
});
}
</script>
<style>
.header {
margin-top: 6rem;
text-align: center;
}
.page-section {
border-top: 1px solid #eee;
padding: 4rem 0;
margin-bottom: 0;
}
.page-section h3 {
text-transform: uppercase;
font-size: 1.4rem;
letter-spacing: .2rem;
font-weight: 600;
}
.form {
margin-top: 1rem;
}
.icon {
height: 1.2rem;
fill: #222;
}
a .icon {
fill: #1EAEDB;
}
/* for pretty printing code/jwt */
.prettyprint {
background: #fff;
font-family: Menlo, 'Bitstream Vera Sans Mono', 'DejaVu Sans Mono', Monaco, Consolas, monospace;
font-size: 1.2rem;
padding: 2.5rem 3rem;
-webkit-font-smoothing: antialiased;
}
.jwt {
white-space: pre-wrap; /* CSS3 */
white-space: -moz-pre-wrap; /* Firefox */
white-space: -pre-wrap; /* Opera <7 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* IE */
width: 100%;
margin: 1rem 0;
display: block;
}
.user-json {
overflow: scroll;
}
.code-example-body {
white-space: pre;
word-wrap: break-word;
}
.github-fork {
position: absolute;
top: 0;
right: 0;
}
</style>
</head>
<body>
<a class="github-fork" target="_blank" href="https://github.com/ddikman/firebase-jwt"><img loading="lazy" width="149" height="149" src="https://github.blog/wp-content/uploads/2008/12/forkme_right_darkblue_121621.png?resize=149%2C149" class="attachment-full size-full" alt="Fork me on GitHub" data-recalc-dims="1"></a>
<div class="container">
<section class="header">
<h2 class="title">Firebase JWT</h2>
<p>
Tiny tool to generate JWT tokens.
</p>
</section>
<div class="page-section setup-step">
<h3>Before getting started</h3>
<p>You can check the code, this tool never sends anything to any backend aside from google. It saves some inputs to make it easier to use in local storage but it's not doing anything with these. But always use extreme caution, download, check the code and run locally if you're unsure!</p>
</div>
<div class="page-section setup-step">
<h3>Setup project</h3>
<p>
You'll find these settings by opening <a href="http://console.firebase.google.com/">http://console.firebase.google.com/</a> and navigating to <code>Project overview / Project settings</code>.
</p>
<img src="project-settings.png" style="margin: 3rem; width: 50%;" />
<form class="form">
<div class="row">
<div class="six columns">
<label for="project-id">Project id</label>
<input placeholder="project-id" class="remember-input u-full-width" type="text" id="project-id" />
</div>
<div class="six columns">
<label for="api-key">API key</label>
<input placeholder="api-key" class="remember-input u-full-width" type="password" id="api-key" />
</div>
</div>
<p><button id="load" class="button-primary">Connect project</button></p>
</form>
</div>
<div id="loaded-part" style="display: none;">
<div class="page-section">
<h3>Google login</h3>
<p>As you expect, just that one button.</p>
<p><button id="google-signin">Login with google</button></p>
</div>
<div class="page-section">
<h3>Email login</h3>
<div class="row">
<div class="six columns">
<label for="email">Email</label>
<input placeholder="[email protected]" class="remember-input u-full-width" type="email" id="email" />
</div>
<div class="six columns">
<label for="password">Password</label>
<input type="password" class="u-full-width" id="password" />
</div>
<button id="email-signin">Login with email</button>
</div>
</div>
<div class="page-section">
<h3>Phone number login</h3>
<p>This one is a bit tricky, first you need to enter your phone number, then a captcha and then you'll get a code sent to your phone to validate with.</p>
<div id="phone-step-1">
<p>The user's phone number in E.164 format (e.g. +16505550101).</p>
<input class="remember-input" type="text" id="phonenumber"/>
<button id="phone-getcode">Phone code</button>
<div id="recaptcha-container"></div>
</div>
<div id="phone-step-2" style="display: none">
<label for="phonecode">Verification code</label>
<input type="number" id="phonecode"/>
<button id="phone-signin">Login with phone number</button>
</div>
</div>
<div class="page-section">
<h3>Current login</h3>
<pre><code class="user-json" id="user"></code></pre>
<code id="result" class="jwt"></code>
<a target="_blank" id="jwtio" style="display: none;" href="">View token contents in JWT.io</a>
</div>
<div class="page-section">
<h3>Change project</h3>
<p>Pressing this will clear your local session and refresh the page allowing you to select a different project.</p>
<button id="switch-project">Logout firebase project</button>
</div>
</div>
<div class="page-section">
<h3>Source</h3>
<p>
This tool is set together by <a target="_blank" href="https://twitter.com/almundgrey">David <svg class="icon" viewBox="0 0 512 512"><g><path d="M512,100c0,1,0,2,0,3c-0.286,0.144-0.678,0.213-0.843,0.444c-12.988,18.134-28.416,33.907-45.899,47.727c-1.117,0.883-2.077,2.606-2.191,4.012c-0.803,9.932-1.033,19.92-2.083,29.822c-1.466,13.833-2.476,27.829-5.44,41.37c-14.968,68.364-47.628,126.547-101.357,172.138c-38.719,32.854-83.248,53.526-133.219,62.461c-8.297,1.483-16.643,2.691-24.966,4.027c-21.667,0-43.333,0-65,0c-6.005-0.986-12-2.043-18.017-2.947c-38.1-5.724-73.019-20.017-106.071-39.338c-1.137-0.664-2.119-1.592-3.174-2.396c0.036-0.494,0.072-0.988,0.108-1.482c55.033,4.465,105.085-8.256,149.475-42.204c-45.885-6.43-78.15-29.625-94.769-73.517c5.615,0,10.368,0.022,15.12-0.006c4.832-0.029,9.677,0.091,14.492-0.223c4.535-0.295,9.04-1.07,13.558-1.634c-59.936-17.467-83.246-74.124-78.912-104.888c13.575,6.847,27.734,11.166,43.121,11.612c-20.952-16.59-35.308-37.341-40.662-63.445c-5.325-25.967-2.189-50.979,11.493-75.753c57.54,65.903,128.3,104.011,215.511,109.404c0-14.21-1.604-27.562,0.309-40.389c5.197-34.848,24.9-60.248,55.821-76.352C318.775,56.04,330.755,53.726,342,50c8.667,0,17.333,0,26,0c0.753,0.291,1.497,0.808,2.261,0.839c10.134,0.412,19.371,4.007,28.571,7.834c10.89,4.53,20.224,11.34,28.906,19.186c3.154,2.85,6.411,4.546,11.24,3.473c17.612-3.914,33.97-11.042,50.23-18.523c3.07-1.413,6.197-2.702,10.545-4.59c-8.316,23.453-22.797,40.565-40.623,55.441C477.73,112.015,494.891,106.107,512,100z"></path></g></svg></a> using <a target="_blank" href="https://firebase.google.com/docs/reference/js">Firebase web SDK</a> and the <a target="_blank" href="http://getskeleton.com/">Skeleton</a> css framework.
</p>
</div>
</div>
<!-- Load all of firebase -->
<script src="https://www.gstatic.com/firebasejs/8.3.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.3.2/firebase-auth.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js"></script>
<script>
let config = {};
$(document).ready(function(){
function rememberInput(elementId) {
const selector = `#${elementId}`;
$(selector).val(localStorage.getItem(elementId));
$(selector).on('blur', function() {
localStorage.setItem(elementId, $(selector).val());
})
}
$('.remember-input').each(function(_, element) {
rememberInput(element.id);
})
function buildConfig() {
const id = $('#project-id').val();
config = {
projectId: id,
authDomain: `${id}.firebaseapp.com`,
apiKey: $('#api-key').val()
}
}
for (let configElement of ['#project-id', '#api-key']) {
$('#project-id').on('change', function() {
buildConfig();
})
}
$('#load').on('click', function(){
try {
loadFirebase(config);
$('#result').text('Done!');
} catch(e) {
$('#result').text(e);
}
});
function handleAuthError(error) {
if (error.code === "auth/unauthorized-domain") {
const authDomain = firebase.auth().app.options.authDomain;
const projectName = authDomain.replace('.firebaseapp.com', '');
$('#result').html(`<p>${error.message}</p><p><a target="_blank" href="https://console.firebase.google.com/u/0/project/${projectName}/authentication/providers">Open</a></p>`);
return;
}
$('#result').text(error.message);
}
$('#google-signin').on('click', function() {
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function() {}, handleAuthError);
})
$('#email-signin').on('click', function() {
var cred = firebase.auth().signInWithEmailAndPassword(
$('#email').val(),
$('#password').val()
).then(function(){}, handleAuthError);
})
$('#email-signin').on('click', function() {
var cred = firebase.auth().signInWithEmailAndPassword(
$('#email').val(),
$('#password').val()
).then(function(){}, handleAuthError);
})
$('#phone-getcode').on('click', function(){
const recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container');
firebase.auth().signInWithPhoneNumber(
$('#phonenumber').val(),
recaptchaVerifier
).then(function(result) {
$('#phone-signin').unbind('click');
$('#phone-step-1').hide();
$('#phone-step-2').show();
$('#phone-signin').on('click', function() {
result.confirm($('#phonecode').val())
});
});
})
$('#switch-project').on('click', function() {
localStorage.removeItem('validConfig');
location.reload();
})
buildConfig();
if (localStorage.getItem('validConfig')) {
loadFirebase(config);
}
})
</script>
</body>
</html>