Skip to content

Commit

Permalink
chore: simplify js setup (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl authored Feb 8, 2024
1 parent af3395e commit 3b8d045
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 60 deletions.
12 changes: 0 additions & 12 deletions src/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,6 @@ export const createLoginRoute: RouteCreator =

res.render("login", {
nodes: flow.ui.nodes,
webAuthnHandler: filterNodesByGroups({
nodes: flow.ui.nodes,
groups: ["webauthn", "passkey"],
attributes: ["button"],
withoutDefaultAttributes: true,
withoutDefaultGroup: true,
})
.filter(({ attributes }) => isUiNodeInputAttributes(attributes))
.map(({ attributes }) => {
return (attributes as UiNodeInputAttributes).onclick
})
.filter((c) => c !== undefined),
card: UserAuthCard(
{
flow,
Expand Down
14 changes: 1 addition & 13 deletions src/routes/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,9 @@ export const createRegistrationRoute: RouteCreator =
flow.oauth2_login_request.challenge,
)
}
const webAuthnHandler = filterNodesByGroups({
nodes: flow.ui.nodes,
groups: ["webauthn", "passkey"],
attributes: ["button"],
withoutDefaultAttributes: true,
withoutDefaultGroup: true,
})
.filter(({ attributes }) => isUiNodeInputAttributes(attributes))
.map(({ attributes }) => {
return (attributes as UiNodeInputAttributes).onclick
})
.filter((onClickAction) => !!onClickAction)

res.render("registration", {
nodes: flow.ui.nodes,
webAuthnHandler: webAuthnHandler,
card: UserAuthCard(
{
flow,
Expand Down
12 changes: 0 additions & 12 deletions src/routes/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,6 @@ export const createSettingsRoute: RouteCreator =
nodes: flow.ui.nodes,
nav: settingsScreen.Nav,
settingsScreen: settingsScreen.Body,
webAuthnHandler: filterNodesByGroups({
nodes: flow.ui.nodes,
groups: ["webauthn", "passkey"],
attributes: ["button"],
withoutDefaultAttributes: true,
withoutDefaultGroup: true,
})
.filter(({ attributes }) => isUiNodeInputAttributes(attributes))
.map(({ attributes }) => {
return (attributes as UiNodeInputAttributes).onclick
})
.filter((c) => c !== undefined),
})
})
.catch(redirectOnSoftError(res, next, initFlowUrl))
Expand Down
1 change: 0 additions & 1 deletion views/login.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<div id="login">
{{{card}}}

{{> webauthn_setup nodes=nodes webAuthnHandler=webAuthnHandler webauthnTriggerName="webauthn_login_trigger"}}
{{> js_setup nodes=nodes}}
{{#if extraPartial }}
{{> (extraPartial) }}
Expand Down
14 changes: 13 additions & 1 deletion views/partials/js_setup.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{{! This inserts the webauthn.js script tag}}
{{#each (onlyNodes nodes "webauthn" "text/javascript")}}
<script
src="{{attributes.src}}"
type="{{attributes.type}}"
integrity="{{attributes.integrity}}"
referrerpolicy="{{attributes.referrerpolicy}}"
crossorigin="{{attributes.crossorigin}}"
{{#if attributes.async}}async{{/if}}
data-testid="node/script/{{attributes.id}}"
></script>
{{/each}}
{{! This partial sets up the onclick and onload event handlers for each passkey node.}}
<script type="text/javascript">
// register onclick and onload event handlers for each passkey node.
window.addEventListener("load", () => {
{{#each (onlyNodes nodes "passkey" "")}}
{{#each (onlyNodes nodes "passkey,webauthn" "")}}
{{#if attributes.onclick}}
// onclick for {{attributes.name}}
document.getElementsByName("{{attributes.name}}").forEach((el) => {
Expand Down
19 changes: 0 additions & 19 deletions views/partials/webauthn_setup.hbs

This file was deleted.

1 change: 0 additions & 1 deletion views/registration.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

{{{card}}}

{{> webauthn_setup nodes=nodes webAuthnHandler=webAuthnHandler webauthnTriggerName="webauthn_register_trigger"}}
{{> js_setup nodes=nodes}}
{{#if extraPartial }}
{{> (extraPartial) }}
Expand Down
1 change: 0 additions & 1 deletion views/settings.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
{{{settingsScreen}}}
</div>

{{> webauthn_setup nodes=nodes webAuthnHandler=webAuthnHandler webauthnTriggerName="webauthn_register_trigger"}}
{{> js_setup nodes=nodes}}
</div>

0 comments on commit 3b8d045

Please sign in to comment.