Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add org login switcher #292

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const createLoginRoute: RouteCreator =
})
.then(({ headers, data: verificationFlow }) => {
// we need the csrf cookie from the verification flow
res.setHeader("set-cookie", headers["set-cookie"])
res.setHeader("set-cookie", headers["set-cookie"] || "")
// encode the verification flow id in the query parameters
const verificationParameters = new URLSearchParams({
flow: verificationFlow.id,
Expand Down Expand Up @@ -168,6 +168,7 @@ export const createLoginRoute: RouteCreator =

res.render("login", {
nodes: flow.ui.nodes,
organizations: JSON.stringify(res.locals.organizations),
webAuthnHandler: filterNodesByGroups({
nodes: flow.ui.nodes,
groups: ["webauthn"],
Expand Down
1 change: 1 addition & 0 deletions views/login.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
{{{card}}}

{{> webauthn_setup nodes=nodes webAuthnHandler=webAuthnHandler webauthnTriggerName="webauthn_login_trigger"}}
{{> organizations}}
</div>
49 changes: 49 additions & 0 deletions views/partials/organizations.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<script type="text/javascript">
function hash(string) {
const utf8 = new TextEncoder().encode(string);
return crypto.subtle.digest('SHA-256', utf8).then((hashBuffer) => {
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((bytes) => bytes.toString(16).padStart(2, '0'))
.join('');
return hashHex;
});
}

function toggleSSO(ssoEnabled, orgId) {
var passwordInput = document.querySelector('div[data-testid="node/input/password"]')
var passwordSubmit = document.querySelector('button[type="submit"][value="password"]')
var forgotPassword = document.querySelector('[data-testid="forgot-password-link"]')
if (ssoEnabled) {
passwordInput.style.display = "none";
forgotPassword.style.display = "none";
passwordSubmit.textContent = "Sign in with SSO"
passwordSubmit.onclick = function (e) {
e.preventDefault();
location.replace("/ui/login?organization=" + orgId)
}
} else {
passwordInput.style.display = "";
forgotPassword.style.display = "";
passwordSubmit.textContent = "Sign in"
}
}

var organizations = {{{organizations}}}

document.querySelector('input[name="identifier"]').addEventListener('input', function(e) {
var v = e.target.value;
if (!v.includes("@")) {
return
}
var parts = v.split("@");
var domain = parts[1];

hash(domain).then((h) => {
toggleSSO(h in organizations, organizations[h]);
}).catch((e) => {
console.log(e)
})
});

</script>
Loading