Skip to content

Commit b2d094d

Browse files
committed
feat: Add autocomplete method for seamless login
1 parent fa1abbf commit b2d094d

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,10 @@ Integrate using the `includes/passkeys.js` library:
5959
)
6060
}
6161
</script>
62+
<script>
63+
window.cbSecurity.passkeys.autocomplete(
64+
// redirectLocation ("/")
65+
// additionalParams ({})
66+
);
67+
</script>
6268
```

includes/passkeys.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,40 @@ const passkeys = {
304304
console.error("cbsecurity-passkeys - Registration failed:", registrationResponse);
305305
}
306306
},
307+
async autocomplete(redirectLocation = "/", additionalParams = {}) {
308+
if ( !(await passkeys.isSupported()) ) {
309+
return;
310+
}
311+
312+
// Make the call that returns the credentialGetJson above
313+
const credentialGetOptions = await fetch("/cbsecurity/passkeys/authentication/new?" + new URLSearchParams(additionalParams))
314+
.then(resp => resp.json())
315+
.then(json => JSON.parse(json));
316+
317+
// Call WebAuthn ceremony using webauthn-json wrapper
318+
const publicKeyCredential = await webauthnJSON.get({
319+
mediation: "conditional",
320+
...credentialGetOptions
321+
});
322+
323+
// Return encoded PublicKeyCredential to server
324+
const authenticationResponse = await fetch("/cbsecurity/passkeys/authentication", {
325+
method: "POST",
326+
headers: {
327+
"Content-Type": "application/json"
328+
},
329+
body: JSON.stringify({
330+
...additionalParams,
331+
"publicKeyCredentialJson": JSON.stringify(publicKeyCredential)
332+
})
333+
});
334+
335+
if (authenticationResponse.ok) {
336+
window.location = redirectLocation;
337+
} else {
338+
console.error("cbsecurity-passkeys - Authentication failed:", authenticationResponse);
339+
}
340+
},
307341
async login(username, redirectLocation = "/", additionalParams = {}) {
308342
if ( !username ) {
309343
username = "";

resources/assets/js/passkeys.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,40 @@ const passkeys = {
4343
console.error("cbsecurity-passkeys - Registration failed:", registrationResponse);
4444
}
4545
},
46+
async autocomplete(redirectLocation = "/", additionalParams = {}) {
47+
if ( !(await passkeys.isSupported()) ) {
48+
return;
49+
}
50+
51+
// Make the call that returns the credentialGetJson above
52+
const credentialGetOptions = await fetch("/cbsecurity/passkeys/authentication/new?" + new URLSearchParams(additionalParams))
53+
.then(resp => resp.json())
54+
.then(json => JSON.parse(json));
55+
56+
// Call WebAuthn ceremony using webauthn-json wrapper
57+
const publicKeyCredential = await webauthnJSON.get({
58+
mediation: "conditional",
59+
...credentialGetOptions
60+
});
61+
62+
// Return encoded PublicKeyCredential to server
63+
const authenticationResponse = await fetch("/cbsecurity/passkeys/authentication", {
64+
method: "POST",
65+
headers: {
66+
"Content-Type": "application/json"
67+
},
68+
body: JSON.stringify({
69+
...additionalParams,
70+
"publicKeyCredentialJson": JSON.stringify(publicKeyCredential)
71+
})
72+
});
73+
74+
if (authenticationResponse.ok) {
75+
window.location = redirectLocation;
76+
} else {
77+
console.error("cbsecurity-passkeys - Authentication failed:", authenticationResponse);
78+
}
79+
},
4680
async login(username, redirectLocation = "/", additionalParams = {}) {
4781
if ( !username ) {
4882
username = "";

0 commit comments

Comments
 (0)