@kayjs/auth 0.0.0-stage → 1.104.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +78 -2
- package/llms/admin-and-organizations.md +70 -0
- package/llms/admin-pages.md +30 -0
- package/llms/grouped-methods.md +106 -0
- package/llms/hardening-and-testing.md +108 -0
- package/llms/oauth-passkeys-two-factor.md +111 -0
- package/llms/oauth-providers.md +28 -0
- package/llms/oauth-server-and-tokens.md +76 -0
- package/llms/organization-pages.md +20 -0
- package/llms/passwords-and-mails.md +129 -0
- package/llms/sessions-and-guards.md +51 -0
- package/llms/setup.md +122 -0
- package/llms/signed-in-components.md +59 -0
- package/llms.txt +28 -0
- package/package.json +85 -4
- package/src/access.ts +31 -0
- package/src/account.ts +198 -0
- package/src/add.ts +70 -0
- package/src/address.ts +51 -0
- package/src/admin.ts +361 -0
- package/src/auditWording.ts +153 -0
- package/src/auth.ts +219 -0
- package/src/authorize/clients.ts +160 -0
- package/src/authorize/grants.ts +44 -0
- package/src/authorize/metadata.ts +72 -0
- package/src/authorize/register.ts +38 -0
- package/src/authorize/reply.ts +9 -0
- package/src/authorize/request.ts +253 -0
- package/src/authorize/resource.ts +34 -0
- package/src/authorize/revoke.ts +36 -0
- package/src/authorize/routes.ts +87 -0
- package/src/authorize/token.ts +213 -0
- package/src/authorize/uris.ts +76 -0
- package/src/bearer.ts +208 -0
- package/src/bearerHeader.ts +11 -0
- package/src/breached.ts +47 -0
- package/src/browser.ts +59 -0
- package/src/captcha.ts +196 -0
- package/src/cbor.ts +129 -0
- package/src/components/deletion-banner.kay +20 -0
- package/src/components/impersonation-banner.kay +21 -0
- package/src/components/signed-in.kay +8 -0
- package/src/components/signed-out.kay +6 -0
- package/src/components/user-menu.kay +21 -0
- package/src/context.ts +5 -0
- package/src/core.ts +308 -0
- package/src/createUserCommand.ts +217 -0
- package/src/decide.ts +45 -0
- package/src/deletion.ts +80 -0
- package/src/devTool.ts +104 -0
- package/src/device.ts +37 -0
- package/src/email.ts +594 -0
- package/src/erase.ts +237 -0
- package/src/events.ts +297 -0
- package/src/export.ts +101 -0
- package/src/failure.ts +63 -0
- package/src/forwardAuth.ts +55 -0
- package/src/forwardRoutes.ts +68 -0
- package/src/gate.ts +104 -0
- package/src/guard.ts +22 -0
- package/src/guards.ts +72 -0
- package/src/guesses.ts +99 -0
- package/src/index.ts +46 -0
- package/src/keys.ts +51 -0
- package/src/limits.ts +182 -0
- package/src/load.ts +18 -0
- package/src/locale.ts +15 -0
- package/src/log.ts +4 -0
- package/src/mail.ts +225 -0
- package/src/memoryStore.ts +599 -0
- package/src/messages/fr.ts +357 -0
- package/src/methods/account.ts +80 -0
- package/src/methods/admin.ts +63 -0
- package/src/methods/audit.ts +15 -0
- package/src/methods/email.ts +39 -0
- package/src/methods/magicLink.ts +32 -0
- package/src/methods/oauth.ts +35 -0
- package/src/methods/oauthServer.ts +31 -0
- package/src/methods/organizations.ts +96 -0
- package/src/methods/passkeys.ts +61 -0
- package/src/methods/password.ts +53 -0
- package/src/methods/sessions.ts +18 -0
- package/src/methods/tokens.ts +24 -0
- package/src/methods/twoFactor.ts +62 -0
- package/src/oauth/client.ts +324 -0
- package/src/oauth/flow.ts +350 -0
- package/src/oauth/index.ts +5 -0
- package/src/oauth/pkce.ts +20 -0
- package/src/oauth/providers.ts +490 -0
- package/src/oauth/refresher.ts +177 -0
- package/src/oauth/timeout.ts +2 -0
- package/src/oauth/types.ts +78 -0
- package/src/oauthTest.ts +112 -0
- package/src/organizations.ts +675 -0
- package/src/organizationsCore.ts +60 -0
- package/src/own.ts +313 -0
- package/src/pages/components/captcha.kay +16 -0
- package/src/pages/components/frame.kay +11 -0
- package/src/pages/components/message.kay +8 -0
- package/src/pages/components/panel.kay +12 -0
- package/src/pages/components/passkey-add.kay +48 -0
- package/src/pages/components/passkey-sign-in.kay +34 -0
- package/src/pages/components/passkey-two-factor.kay +33 -0
- package/src/pages/components/proof.kay +15 -0
- package/src/pages/components/provider-buttons.kay +25 -0
- package/src/pages/components/recovery-codes.kay +13 -0
- package/src/pages/components/separator.kay +4 -0
- package/src/pages/english.ts +82 -0
- package/src/pages/forms.ts +185 -0
- package/src/pages/index.ts +361 -0
- package/src/pages/organizationPage.ts +12 -0
- package/src/pages/routes/account/_guard.ts +15 -0
- package/src/pages/routes/account/_layout.kay +37 -0
- package/src/pages/routes/account/clients.kay +33 -0
- package/src/pages/routes/account/connections.kay +57 -0
- package/src/pages/routes/account/data.kay +106 -0
- package/src/pages/routes/account/index.kay +109 -0
- package/src/pages/routes/account/organizations/[org].kay +188 -0
- package/src/pages/routes/account/organizations/_guard.ts +8 -0
- package/src/pages/routes/account/organizations/index.kay +86 -0
- package/src/pages/routes/account/security.kay +164 -0
- package/src/pages/routes/account/sessions.kay +51 -0
- package/src/pages/routes/account/tokens.kay +84 -0
- package/src/pages/routes/admin/_layout.kay +43 -0
- package/src/pages/routes/admin/audit.kay +43 -0
- package/src/pages/routes/admin/index.kay +60 -0
- package/src/pages/routes/admin/users/[id].kay +181 -0
- package/src/pages/routes/confirm-email.kay +39 -0
- package/src/pages/routes/invitation.kay +104 -0
- package/src/pages/routes/magic-link.kay +42 -0
- package/src/pages/routes/oauth/authorize.kay +80 -0
- package/src/pages/routes/register.kay +83 -0
- package/src/pages/routes/reset-password.kay +73 -0
- package/src/pages/routes/sign-in.kay +128 -0
- package/src/pages/routes/two-factor.kay +61 -0
- package/src/pages/routes/verify-email.kay +83 -0
- package/src/passkeys.ts +373 -0
- package/src/password.ts +316 -0
- package/src/payloads.ts +22 -0
- package/src/pg.ts +705 -0
- package/src/profile.ts +43 -0
- package/src/proof.ts +110 -0
- package/src/prune.ts +123 -0
- package/src/rates.ts +82 -0
- package/src/request.ts +712 -0
- package/src/requiredTwoFactor.ts +71 -0
- package/src/requirement.ts +23 -0
- package/src/roles.ts +18 -0
- package/src/routes.ts +202 -0
- package/src/rules-check.ts +37 -0
- package/src/rules.ts +108 -0
- package/src/securityNotice.ts +45 -0
- package/src/sessions.ts +184 -0
- package/src/setup.ts +417 -0
- package/src/signInOptions.ts +9 -0
- package/src/state.ts +34 -0
- package/src/store.ts +410 -0
- package/src/storeHelpers.ts +23 -0
- package/src/test.ts +46 -0
- package/src/testCookies.ts +9 -0
- package/src/testHelpers.ts +58 -0
- package/src/testing.ts +113 -0
- package/src/text.ts +6 -0
- package/src/totp.ts +120 -0
- package/src/twoFactor.ts +200 -0
- package/src/types.ts +23 -0
- package/src/userData.ts +47 -0
- package/src/viewer-layout/_layout.kay +11 -0
- package/src/viewer.ts +116 -0
- package/src/wayIn.ts +40 -0
- package/src/webauthn.ts +375 -0
- package/src/wsAuth.ts +53 -0
- package/types/access.d.ts +12 -0
- package/types/access.d.ts.map +1 -0
- package/types/account.d.ts +32 -0
- package/types/account.d.ts.map +1 -0
- package/types/add.d.ts +29 -0
- package/types/add.d.ts.map +1 -0
- package/types/address.d.ts +2 -0
- package/types/address.d.ts.map +1 -0
- package/types/admin.d.ts +84 -0
- package/types/admin.d.ts.map +1 -0
- package/types/auditWording.d.ts +9 -0
- package/types/auditWording.d.ts.map +1 -0
- package/types/auth.d.ts +3475 -0
- package/types/auth.d.ts.map +1 -0
- package/types/authorize/clients.d.ts +15 -0
- package/types/authorize/clients.d.ts.map +1 -0
- package/types/authorize/grants.d.ts +15 -0
- package/types/authorize/grants.d.ts.map +1 -0
- package/types/authorize/metadata.d.ts +20 -0
- package/types/authorize/metadata.d.ts.map +1 -0
- package/types/authorize/register.d.ts +4 -0
- package/types/authorize/register.d.ts.map +1 -0
- package/types/authorize/reply.d.ts +6 -0
- package/types/authorize/reply.d.ts.map +1 -0
- package/types/authorize/request.d.ts +48 -0
- package/types/authorize/request.d.ts.map +1 -0
- package/types/authorize/resource.d.ts +4 -0
- package/types/authorize/resource.d.ts.map +1 -0
- package/types/authorize/revoke.d.ts +4 -0
- package/types/authorize/revoke.d.ts.map +1 -0
- package/types/authorize/routes.d.ts +5 -0
- package/types/authorize/routes.d.ts.map +1 -0
- package/types/authorize/token.d.ts +7 -0
- package/types/authorize/token.d.ts.map +1 -0
- package/types/authorize/uris.d.ts +5 -0
- package/types/authorize/uris.d.ts.map +1 -0
- package/types/bearer.d.ts +45 -0
- package/types/bearer.d.ts.map +1 -0
- package/types/bearerHeader.d.ts +3 -0
- package/types/bearerHeader.d.ts.map +1 -0
- package/types/breached.d.ts +5 -0
- package/types/breached.d.ts.map +1 -0
- package/types/browser.d.ts +11 -0
- package/types/browser.d.ts.map +1 -0
- package/types/captcha.d.ts +42 -0
- package/types/captcha.d.ts.map +1 -0
- package/types/cbor.d.ts +4 -0
- package/types/cbor.d.ts.map +1 -0
- package/types/context.d.ts +3 -0
- package/types/context.d.ts.map +1 -0
- package/types/core.d.ts +180 -0
- package/types/core.d.ts.map +1 -0
- package/types/createUserCommand.d.ts +11 -0
- package/types/createUserCommand.d.ts.map +1 -0
- package/types/decide.d.ts +12 -0
- package/types/decide.d.ts.map +1 -0
- package/types/deletion.d.ts +17 -0
- package/types/deletion.d.ts.map +1 -0
- package/types/devTool.d.ts +3 -0
- package/types/devTool.d.ts.map +1 -0
- package/types/device.d.ts +7 -0
- package/types/device.d.ts.map +1 -0
- package/types/email.d.ts +73 -0
- package/types/email.d.ts.map +1 -0
- package/types/erase.d.ts +31 -0
- package/types/erase.d.ts.map +1 -0
- package/types/events.d.ts +139 -0
- package/types/events.d.ts.map +1 -0
- package/types/export.d.ts +54 -0
- package/types/export.d.ts.map +1 -0
- package/types/failure.d.ts +19 -0
- package/types/failure.d.ts.map +1 -0
- package/types/forwardAuth.d.ts +12 -0
- package/types/forwardAuth.d.ts.map +1 -0
- package/types/forwardRoutes.d.ts +11 -0
- package/types/forwardRoutes.d.ts.map +1 -0
- package/types/gate.d.ts +31 -0
- package/types/gate.d.ts.map +1 -0
- package/types/guard.d.ts +10 -0
- package/types/guard.d.ts.map +1 -0
- package/types/guards.d.ts +37 -0
- package/types/guards.d.ts.map +1 -0
- package/types/guesses.d.ts +18 -0
- package/types/guesses.d.ts.map +1 -0
- package/types/index.d.ts +47 -0
- package/types/index.d.ts.map +1 -0
- package/types/keys.d.ts +11 -0
- package/types/keys.d.ts.map +1 -0
- package/types/limits.d.ts +133 -0
- package/types/limits.d.ts.map +1 -0
- package/types/load.d.ts +18 -0
- package/types/load.d.ts.map +1 -0
- package/types/locale.d.ts +4 -0
- package/types/locale.d.ts.map +1 -0
- package/types/log.d.ts +2 -0
- package/types/log.d.ts.map +1 -0
- package/types/mail.d.ts +51 -0
- package/types/mail.d.ts.map +1 -0
- package/types/memoryStore.d.ts +3 -0
- package/types/memoryStore.d.ts.map +1 -0
- package/types/messages/fr.d.ts +409 -0
- package/types/messages/fr.d.ts.map +1 -0
- package/types/methods/account.d.ts +24 -0
- package/types/methods/account.d.ts.map +1 -0
- package/types/methods/admin.d.ts +30 -0
- package/types/methods/admin.d.ts.map +1 -0
- package/types/methods/audit.d.ts +11 -0
- package/types/methods/audit.d.ts.map +1 -0
- package/types/methods/email.d.ts +14 -0
- package/types/methods/email.d.ts.map +1 -0
- package/types/methods/magicLink.d.ts +12 -0
- package/types/methods/magicLink.d.ts.map +1 -0
- package/types/methods/oauth.d.ts +16 -0
- package/types/methods/oauth.d.ts.map +1 -0
- package/types/methods/oauthServer.d.ts +13 -0
- package/types/methods/oauthServer.d.ts.map +1 -0
- package/types/methods/organizations.d.ts +30 -0
- package/types/methods/organizations.d.ts.map +1 -0
- package/types/methods/passkeys.d.ts +19 -0
- package/types/methods/passkeys.d.ts.map +1 -0
- package/types/methods/password.d.ts +17 -0
- package/types/methods/password.d.ts.map +1 -0
- package/types/methods/sessions.d.ts +9 -0
- package/types/methods/sessions.d.ts.map +1 -0
- package/types/methods/tokens.d.ts +11 -0
- package/types/methods/tokens.d.ts.map +1 -0
- package/types/methods/twoFactor.d.ts +20 -0
- package/types/methods/twoFactor.d.ts.map +1 -0
- package/types/oauth/client.d.ts +39 -0
- package/types/oauth/client.d.ts.map +1 -0
- package/types/oauth/flow.d.ts +38 -0
- package/types/oauth/flow.d.ts.map +1 -0
- package/types/oauth/index.d.ts +6 -0
- package/types/oauth/index.d.ts.map +1 -0
- package/types/oauth/pkce.d.ts +10 -0
- package/types/oauth/pkce.d.ts.map +1 -0
- package/types/oauth/providers.d.ts +100 -0
- package/types/oauth/providers.d.ts.map +1 -0
- package/types/oauth/refresher.d.ts +55 -0
- package/types/oauth/refresher.d.ts.map +1 -0
- package/types/oauth/timeout.d.ts +2 -0
- package/types/oauth/timeout.d.ts.map +1 -0
- package/types/oauth/types.d.ts +75 -0
- package/types/oauth/types.d.ts.map +1 -0
- package/types/oauthTest.d.ts +23 -0
- package/types/oauthTest.d.ts.map +1 -0
- package/types/organizations.d.ts +111 -0
- package/types/organizations.d.ts.map +1 -0
- package/types/organizationsCore.d.ts +29 -0
- package/types/organizationsCore.d.ts.map +1 -0
- package/types/own.d.ts +55 -0
- package/types/own.d.ts.map +1 -0
- package/types/pages/english.d.ts +80 -0
- package/types/pages/english.d.ts.map +1 -0
- package/types/pages/forms.d.ts +204 -0
- package/types/pages/forms.d.ts.map +1 -0
- package/types/pages/index.d.ts +166 -0
- package/types/pages/index.d.ts.map +1 -0
- package/types/pages/organizationPage.d.ts +7 -0
- package/types/pages/organizationPage.d.ts.map +1 -0
- package/types/pages/routes/account/_guard.d.ts +3 -0
- package/types/pages/routes/account/_guard.d.ts.map +1 -0
- package/types/pages/routes/account/organizations/_guard.d.ts +3 -0
- package/types/pages/routes/account/organizations/_guard.d.ts.map +1 -0
- package/types/passkeys.d.ts +61 -0
- package/types/passkeys.d.ts.map +1 -0
- package/types/password.d.ts +49 -0
- package/types/password.d.ts.map +1 -0
- package/types/payloads.d.ts +4 -0
- package/types/payloads.d.ts.map +1 -0
- package/types/pg.d.ts +303 -0
- package/types/pg.d.ts.map +1 -0
- package/types/profile.d.ts +11 -0
- package/types/profile.d.ts.map +1 -0
- package/types/proof.d.ts +33 -0
- package/types/proof.d.ts.map +1 -0
- package/types/prune.d.ts +5 -0
- package/types/prune.d.ts.map +1 -0
- package/types/rates.d.ts +15 -0
- package/types/rates.d.ts.map +1 -0
- package/types/request.d.ts +119 -0
- package/types/request.d.ts.map +1 -0
- package/types/requiredTwoFactor.d.ts +16 -0
- package/types/requiredTwoFactor.d.ts.map +1 -0
- package/types/requirement.d.ts +4 -0
- package/types/requirement.d.ts.map +1 -0
- package/types/roles.d.ts +9 -0
- package/types/roles.d.ts.map +1 -0
- package/types/routes.d.ts +31 -0
- package/types/routes.d.ts.map +1 -0
- package/types/rules-check.d.ts +3 -0
- package/types/rules-check.d.ts.map +1 -0
- package/types/rules.d.ts +38 -0
- package/types/rules.d.ts.map +1 -0
- package/types/securityNotice.d.ts +7 -0
- package/types/securityNotice.d.ts.map +1 -0
- package/types/sessions.d.ts +32 -0
- package/types/sessions.d.ts.map +1 -0
- package/types/setup.d.ts +18 -0
- package/types/setup.d.ts.map +1 -0
- package/types/signInOptions.d.ts +6 -0
- package/types/signInOptions.d.ts.map +1 -0
- package/types/state.d.ts +25 -0
- package/types/state.d.ts.map +1 -0
- package/types/store.d.ts +295 -0
- package/types/store.d.ts.map +1 -0
- package/types/storeHelpers.d.ts +4 -0
- package/types/storeHelpers.d.ts.map +1 -0
- package/types/test.d.ts +21 -0
- package/types/test.d.ts.map +1 -0
- package/types/testCookies.d.ts +3 -0
- package/types/testCookies.d.ts.map +1 -0
- package/types/testHelpers.d.ts +34 -0
- package/types/testHelpers.d.ts.map +1 -0
- package/types/testing.d.ts +12 -0
- package/types/testing.d.ts.map +1 -0
- package/types/text.d.ts +2 -0
- package/types/text.d.ts.map +1 -0
- package/types/totp.d.ts +12 -0
- package/types/totp.d.ts.map +1 -0
- package/types/twoFactor.d.ts +41 -0
- package/types/twoFactor.d.ts.map +1 -0
- package/types/types.d.ts +17 -0
- package/types/types.d.ts.map +1 -0
- package/types/userData.d.ts +9 -0
- package/types/userData.d.ts.map +1 -0
- package/types/viewer.d.ts +23 -0
- package/types/viewer.d.ts.map +1 -0
- package/types/wayIn.d.ts +12 -0
- package/types/wayIn.d.ts.map +1 -0
- package/types/webauthn.d.ts +111 -0
- package/types/webauthn.d.ts.map +1 -0
- package/types/wsAuth.d.ts +14 -0
- package/types/wsAuth.d.ts.map +1 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Auth } from '../../../auth'
|
|
2
|
+
import { guard } from 'kay'
|
|
3
|
+
|
|
4
|
+
// The account pages are the signed-in user's own, through their session rather than a bearer token: anyone else signs in first and comes back.
|
|
5
|
+
export default guard<{ auth: Auth }>(async (ctx) => {
|
|
6
|
+
const { auth } = ctx.locals
|
|
7
|
+
|
|
8
|
+
if (await auth.via() === 'session')
|
|
9
|
+
return
|
|
10
|
+
|
|
11
|
+
if (ctx.request.method === 'GET')
|
|
12
|
+
ctx.redirect(auth.signInUrl())
|
|
13
|
+
|
|
14
|
+
ctx.deny(401)
|
|
15
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
--- server
|
|
2
|
+
import { localize } from 'kay/i18n'
|
|
3
|
+
import { wording } from 'kay/words'
|
|
4
|
+
import TabNav from '@kayjs/ui/tab-nav.kay'
|
|
5
|
+
import DeletionBanner from '../../../components/deletion-banner.kay'
|
|
6
|
+
import ImpersonationBanner from '../../../components/impersonation-banner.kay'
|
|
7
|
+
import UserMenu from '../../../components/user-menu.kay'
|
|
8
|
+
import Frame from '../../components/frame.kay'
|
|
9
|
+
|
|
10
|
+
const features = ctx.locals.auth.features()
|
|
11
|
+
const base = localize(`${features.pages?.prefix ?? features.prefix}/account`)
|
|
12
|
+
// An organization's page keeps the Organizations tab on.
|
|
13
|
+
const current = ctx.url.pathname.startsWith(`${base}/organizations/`) ? `${base}/organizations` : ctx.url.pathname
|
|
14
|
+
const links = [
|
|
15
|
+
{ href: base, label: wording('auth.account.nav.profile', 'Profile') },
|
|
16
|
+
{ href: `${base}/security`, label: wording('auth.account.nav.security', 'Security') },
|
|
17
|
+
{ href: `${base}/sessions`, label: wording('auth.account.nav.sessions', 'Sessions') },
|
|
18
|
+
...features.providers.length > 0 ? [{ href: `${base}/connections`, label: wording('auth.account.nav.connections', 'Connections') }] : [],
|
|
19
|
+
{ href: `${base}/tokens`, label: wording('auth.account.nav.tokens', 'Tokens') },
|
|
20
|
+
...features.oauthServer ? [{ href: `${base}/clients`, label: wording('auth.account.nav.clients', 'Apps') }] : [],
|
|
21
|
+
...features.organizations ? [{ href: `${base}/organizations`, label: wording('auth.account.nav.organizations', 'Organizations') }] : [],
|
|
22
|
+
{ href: `${base}/data`, label: wording('auth.account.nav.data', 'Your data') },
|
|
23
|
+
]
|
|
24
|
+
// The tab on screen names the page, so a tab switch or a page swap announces where the user landed.
|
|
25
|
+
const here = links.find(link => link.href.replace(/\/$/, '') === current.replace(/\/$/, ''))
|
|
26
|
+
const title = here ? `${here.label} - ${wording('auth.account.title', 'Your account')}` : wording('auth.account.title', 'Your account')
|
|
27
|
+
---
|
|
28
|
+
head
|
|
29
|
+
title {title}
|
|
30
|
+
Frame(size="max-w-3xl" gap="gap-6" landmark={features.pages?.main !== false})
|
|
31
|
+
ImpersonationBanner
|
|
32
|
+
DeletionBanner
|
|
33
|
+
.flex.flex-wrap.items-center.justify-between.gap-4
|
|
34
|
+
h1.kui-heading-section {wording('auth.account.title', 'Your account')}
|
|
35
|
+
UserMenu(account={false})
|
|
36
|
+
TabNav(links={links} current={current} label={wording('auth.account.nav.label', 'Account')})
|
|
37
|
+
slot
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
--- server
|
|
2
|
+
import { action } from 'kay'
|
|
3
|
+
import { formatDate } from 'kay/i18n'
|
|
4
|
+
import { wording } from 'kay/words'
|
|
5
|
+
import { schemas, scopeLabel } from '../../forms'
|
|
6
|
+
import Button from '@kayjs/ui/button.kay'
|
|
7
|
+
import CardRow from '@kayjs/ui/card-row.kay'
|
|
8
|
+
import EmptyState from '@kayjs/ui/empty-state.kay'
|
|
9
|
+
import PageSection from '@kayjs/ui/page-section.kay'
|
|
10
|
+
|
|
11
|
+
export const revoke = action(schemas.oauthClient, async ({ id }, ctx) => {
|
|
12
|
+
return (await ctx.locals.auth.oauthServer.revokeGrant(id)).ok ? 'revoked' : ctx.fail(404, { error: 'not-found' })
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const clients = await ctx.locals.auth.oauthServer.clients()
|
|
16
|
+
const date = (value: Date) => formatDate(value, { dateStyle: 'medium' })
|
|
17
|
+
---
|
|
18
|
+
PageSection(title={wording('auth.account.clients.title', 'Connected apps')})
|
|
19
|
+
.flex.flex-col.gap-2
|
|
20
|
+
p {wording('auth.account.clients.intro', 'These apps signed in to your account. Revoking one ends its access at once.')}
|
|
21
|
+
for client of clients key={client.id}
|
|
22
|
+
CardRow(data-client={client.id})
|
|
23
|
+
@right
|
|
24
|
+
form(action={revoke})
|
|
25
|
+
input(type="hidden" name="id" value={client.id})
|
|
26
|
+
Button(type="submit" tone="danger" label={wording('auth.account.clients.revoke', 'Revoke')} aria-label={`${wording('auth.account.clients.revoke', 'Revoke')}: ${client.name}`})
|
|
27
|
+
.flex.flex-col
|
|
28
|
+
strong {client.name}
|
|
29
|
+
span.text-muted {client.scopes === null ? wording('auth.account.clients.allScopes', 'Everything') : client.scopes.length === 0 ? wording('auth.consent.noScopes', 'Identify you, with no access to the features that need a permission') : client.scopes.map(scopeLabel).join(', ')}
|
|
30
|
+
span.text-muted
|
|
31
|
+
| {wording('auth.account.clients.approved', 'Approved {date}', { date: date(client.createdAt) })} · {client.lastUsedAt ? wording('auth.account.clients.lastUsed', 'Last used {date}', { date: date(client.lastUsedAt) }) : wording('auth.account.clients.neverUsed', 'Never used')}
|
|
32
|
+
else
|
|
33
|
+
EmptyState(title={wording('auth.account.clients.none', 'No app is connected.')})
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
--- server
|
|
2
|
+
import { action } from 'kay'
|
|
3
|
+
import { localize } from 'kay/i18n'
|
|
4
|
+
import { wording } from 'kay/words'
|
|
5
|
+
import Message from '../../components/message.kay'
|
|
6
|
+
import Proof from '../../components/proof.kay'
|
|
7
|
+
import { failure, oauthFailure, proofOf, schemas } from '../../forms'
|
|
8
|
+
import Badge from '@kayjs/ui/badge.kay'
|
|
9
|
+
import Button from '@kayjs/ui/button.kay'
|
|
10
|
+
import CardRow from '@kayjs/ui/card-row.kay'
|
|
11
|
+
import PageSection from '@kayjs/ui/page-section.kay'
|
|
12
|
+
|
|
13
|
+
// Leaves for the provider once the proof holds, and comes back here.
|
|
14
|
+
export const connect = action(schemas.unlink, async (input, ctx) => {
|
|
15
|
+
const auth = ctx.locals.auth
|
|
16
|
+
const features = auth.features()
|
|
17
|
+
const started = await auth.oauth.link(input.provider, proofOf(input), { redirect: localize(`${features.pages?.prefix ?? features.prefix}/account/connections`) })
|
|
18
|
+
|
|
19
|
+
return started.ok ? ctx.redirect(started.url) : ctx.fail(400, { error: started.error.code, provider: input.provider })
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
export const disconnect = action(schemas.unlink, async (input, ctx) => {
|
|
23
|
+
const result = await ctx.locals.auth.oauth.unlink(input.provider, proofOf(input))
|
|
24
|
+
|
|
25
|
+
return result.ok ? 'disconnected' : ctx.fail(400, { error: result.error.code, provider: input.provider })
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const auth = ctx.locals.auth
|
|
29
|
+
const user = await auth.requireUser()
|
|
30
|
+
const features = auth.features()
|
|
31
|
+
const linked = new Set(await auth.oauth.identities())
|
|
32
|
+
const refused = connect.failed()?.data ?? disconnect.failed()?.data
|
|
33
|
+
---
|
|
34
|
+
PageSection(title={wording('auth.account.connections.title', 'Connected accounts')})
|
|
35
|
+
.flex.flex-col.gap-2
|
|
36
|
+
p {wording('auth.account.connections.intro', 'Sign in with these accounts as well as your own.')}
|
|
37
|
+
for provider of features.providers key={provider.name}
|
|
38
|
+
CardRow(data-provider={provider.name})
|
|
39
|
+
.flex.flex-col.gap-2
|
|
40
|
+
span.flex.items-center.gap-2
|
|
41
|
+
strong {provider.label}
|
|
42
|
+
if linked.has(provider.name)
|
|
43
|
+
Badge(label={wording('auth.account.connections.connected', 'Connected')} tone="success")
|
|
44
|
+
if refused?.provider === provider.name
|
|
45
|
+
Message(text={oauthFailure(refused.error) ?? failure(refused.error)})
|
|
46
|
+
if linked.has(provider.name)
|
|
47
|
+
form.flex.flex-wrap.items-end.gap-2(action={disconnect})
|
|
48
|
+
input(type="hidden" name="provider" value={provider.name})
|
|
49
|
+
Proof(user={user})
|
|
50
|
+
Button(type="submit" tone="danger" label={wording('auth.account.connections.disconnect', 'Disconnect')})
|
|
51
|
+
else
|
|
52
|
+
form.flex.flex-wrap.items-end.gap-2(action={connect})
|
|
53
|
+
input(type="hidden" name="provider" value={provider.name})
|
|
54
|
+
Proof(user={user} secondFactor)
|
|
55
|
+
Button(type="submit" label={wording('auth.account.connections.connect', 'Connect')})
|
|
56
|
+
else
|
|
57
|
+
p {wording('auth.account.connections.none', 'No provider is set up.')}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
--- server
|
|
2
|
+
import { action } from 'kay'
|
|
3
|
+
import { formatDate, localize } from 'kay/i18n'
|
|
4
|
+
import { durationWords } from 'kay/library'
|
|
5
|
+
import { wording } from 'kay/words'
|
|
6
|
+
import Message from '../../components/message.kay'
|
|
7
|
+
import Proof from '../../components/proof.kay'
|
|
8
|
+
import { failure, proofOf, schemas } from '../../forms'
|
|
9
|
+
import Button from '@kayjs/ui/button.kay'
|
|
10
|
+
import PageSection from '@kayjs/ui/page-section.kay'
|
|
11
|
+
|
|
12
|
+
// Re-proves the user, so the export link below shows for the next ten minutes.
|
|
13
|
+
export const prove = action(schemas.proof, async (input, ctx) => {
|
|
14
|
+
const result = await ctx.locals.auth.reprove(proofOf(input))
|
|
15
|
+
|
|
16
|
+
return !result.ok ? ctx.fail(400, { error: result.error.code }) : undefined
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
export const remove = action(schemas.proof, async (input, ctx) => {
|
|
20
|
+
const result = await ctx.locals.auth.account.delete(proofOf(input))
|
|
21
|
+
|
|
22
|
+
if (!result.ok)
|
|
23
|
+
return ctx.fail(400, { error: result.error.code })
|
|
24
|
+
|
|
25
|
+
const { pages, prefix } = ctx.locals.auth.features()
|
|
26
|
+
|
|
27
|
+
// A deletion left pending keeps the user signed in, on the page that offers to cancel it.
|
|
28
|
+
return ctx.redirect(localize(result.deleteAt === undefined ? pages?.home ?? '/' : `${pages?.prefix ?? prefix}/account/data`))
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
export const cancelPending = action(async (ctx) => {
|
|
32
|
+
const { pages, prefix } = ctx.locals.auth.features()
|
|
33
|
+
|
|
34
|
+
await ctx.locals.auth.account.cancelDeletion()
|
|
35
|
+
|
|
36
|
+
return ctx.redirect(localize(`${pages?.prefix ?? prefix}/account/data`))
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
// An account with neither a password nor a second factor proves it is still the user by a recent sign-in alone, so it signs in again and comes back here.
|
|
40
|
+
export const signInAgain = action(async (ctx) => {
|
|
41
|
+
const { signInPage, pages, prefix } = ctx.locals.auth.features()
|
|
42
|
+
const signIn = localize(signInPage)
|
|
43
|
+
|
|
44
|
+
await ctx.locals.auth.signOut()
|
|
45
|
+
|
|
46
|
+
return ctx.redirect(`${signIn}${signIn.includes('?') ? '&' : '?'}redirect=${encodeURIComponent(localize(`${pages?.prefix ?? prefix}/account/data`))}`)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
const auth = ctx.locals.auth
|
|
50
|
+
const user = await auth.requireUser()
|
|
51
|
+
const features = auth.features()
|
|
52
|
+
const refused = remove.failed()?.data?.error
|
|
53
|
+
const notProved = prove.failed()?.data?.error
|
|
54
|
+
const proved = await auth.provedLately()
|
|
55
|
+
// Neither proof form can pass: an admin acting as the user proves nothing, and an account with nothing to prove it by has only its sign-in, now old.
|
|
56
|
+
const impersonated = await auth.impersonator() !== null
|
|
57
|
+
const impact = await auth.account.deletionImpact()
|
|
58
|
+
const stale = !proved && !user.hasPassword && !user.twoFactor
|
|
59
|
+
const pending = await auth.account.deletion()
|
|
60
|
+
const deleteIntro = features.deleteAfter === null
|
|
61
|
+
? wording('auth.account.data.deleteIntro', 'Your account and everything tied to it are deleted for good. This cannot be undone.')
|
|
62
|
+
: wording('auth.account.data.deleteGraceIntro', 'Your account and everything tied to it are deleted for good {after} from now. Until then you can cancel, and signing in is enough to reach the button.', { after: durationWords(`${features.deleteAfter}s`, ctx.locale) })
|
|
63
|
+
---
|
|
64
|
+
if impersonated
|
|
65
|
+
p {failure('impersonating')}
|
|
66
|
+
else
|
|
67
|
+
PageSection(title={wording('auth.account.data.export', 'Download your data')})
|
|
68
|
+
.flex.flex-col.items-start.gap-3
|
|
69
|
+
p {wording('auth.account.data.exportIntro', 'Everything we keep about your account, as a JSON file.')}
|
|
70
|
+
if proved
|
|
71
|
+
Button(href={`${features.prefix}/export`} label={wording('auth.account.data.export', 'Download your data')} download data-no-prefetch)
|
|
72
|
+
else if stale
|
|
73
|
+
form.flex.flex-col.items-start.gap-3(action={signInAgain})
|
|
74
|
+
p {wording('auth.proof.stale', 'For your safety, sign in again before doing this.')}
|
|
75
|
+
Button(type="submit" label={wording('auth.proof.signInAgain', 'Sign in again')})
|
|
76
|
+
else
|
|
77
|
+
form.flex.flex-col.gap-3(action={prove})
|
|
78
|
+
Proof(user={user})
|
|
79
|
+
if notProved
|
|
80
|
+
Message(text={failure(notProved)})
|
|
81
|
+
Button(type="submit" label={wording('auth.account.data.confirmExport', 'Confirm to download my data')})
|
|
82
|
+
PageSection(title={wording('auth.account.data.delete', 'Delete your account')})
|
|
83
|
+
if pending
|
|
84
|
+
form.flex.flex-col.items-start.gap-3(action={cancelPending})
|
|
85
|
+
p {wording('auth.account.data.deletePending', 'Your account will be deleted on {date}.', { date: formatDate(pending.deleteAt, { dateStyle: 'long' }) })}
|
|
86
|
+
Button(type="submit" label={wording('auth.deletion.cancel', 'Cancel the deletion')})
|
|
87
|
+
else if stale
|
|
88
|
+
form.flex.flex-col.items-start.gap-3(action={signInAgain})
|
|
89
|
+
p {deleteIntro}
|
|
90
|
+
if impact.deleted.length > 0
|
|
91
|
+
p {wording('auth.account.data.deleteOrganizations', 'These organizations are deleted with it, as you are their only member: {names}.', { names: impact.deleted.join(', ') })}
|
|
92
|
+
if impact.blocked.length > 0
|
|
93
|
+
p {wording('auth.account.data.deleteBlocked', 'You are the only owner of {names}: make another member an owner first.', { names: impact.blocked.join(', ') })}
|
|
94
|
+
p {wording('auth.proof.stale', 'For your safety, sign in again before doing this.')}
|
|
95
|
+
Button(type="submit" label={wording('auth.proof.signInAgain', 'Sign in again')})
|
|
96
|
+
else
|
|
97
|
+
form.flex.flex-col.gap-3(action={remove})
|
|
98
|
+
p {deleteIntro}
|
|
99
|
+
if impact.deleted.length > 0
|
|
100
|
+
p {wording('auth.account.data.deleteOrganizations', 'These organizations are deleted with it, as you are their only member: {names}.', { names: impact.deleted.join(', ') })}
|
|
101
|
+
if impact.blocked.length > 0
|
|
102
|
+
p {wording('auth.account.data.deleteBlocked', 'You are the only owner of {names}: make another member an owner first.', { names: impact.blocked.join(', ') })}
|
|
103
|
+
Proof(user={user})
|
|
104
|
+
if refused
|
|
105
|
+
Message(text={failure(refused)})
|
|
106
|
+
Button(type="submit" tone="danger" label={wording('auth.account.data.confirmDelete', 'Delete my account')})
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
--- server
|
|
2
|
+
import { action } from 'kay'
|
|
3
|
+
import { wording } from 'kay/words'
|
|
4
|
+
import Message from '../../components/message.kay'
|
|
5
|
+
import Proof from '../../components/proof.kay'
|
|
6
|
+
import { failure, issue, profileIssue, proofOf, schemas } from '../../forms'
|
|
7
|
+
import Badge from '@kayjs/ui/badge.kay'
|
|
8
|
+
import Button from '@kayjs/ui/button.kay'
|
|
9
|
+
import FormField from '@kayjs/ui/form-field.kay'
|
|
10
|
+
import PageSection from '@kayjs/ui/page-section.kay'
|
|
11
|
+
|
|
12
|
+
export const resend = action(async (ctx) => {
|
|
13
|
+
const result = await ctx.locals.auth.email.sendVerification()
|
|
14
|
+
|
|
15
|
+
return result.ok ? 'sent' : ctx.fail(400, { error: result.error.code })
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
export const changeEmail = action(schemas.changeEmail, async (input, ctx) => {
|
|
19
|
+
const result = await ctx.locals.auth.email.change(proofOf(input), input.email)
|
|
20
|
+
|
|
21
|
+
if (!result.ok)
|
|
22
|
+
return ctx.fail(400, 'errors' in result ? { email: result.errors.email } : { error: result.error.code })
|
|
23
|
+
|
|
24
|
+
return input.email
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
export const changeUsername = action(schemas.changeUsername, async ({ username }, ctx) => {
|
|
28
|
+
const result = await ctx.locals.auth.account.changeUsername(username)
|
|
29
|
+
|
|
30
|
+
if (!result.ok)
|
|
31
|
+
return ctx.fail(400, 'errors' in result ? { username: result.errors.username } : { error: result.error.code })
|
|
32
|
+
|
|
33
|
+
return 'saved'
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
export const saveProfile = action(schemas.profile, async (input, ctx) => {
|
|
37
|
+
const result = await ctx.locals.auth.account.saveProfile(input)
|
|
38
|
+
|
|
39
|
+
if (!result.ok)
|
|
40
|
+
return ctx.fail(400, 'errors' in result ? { errors: result.errors } : { error: 'form' as const })
|
|
41
|
+
|
|
42
|
+
return 'saved'
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
const auth = ctx.locals.auth
|
|
46
|
+
const user = await auth.requireUser()
|
|
47
|
+
const features = auth.features()
|
|
48
|
+
const resent = resend.succeeded() !== undefined
|
|
49
|
+
const resendError = resend.failed()?.data?.error
|
|
50
|
+
const emailSent = changeEmail.succeeded()?.data
|
|
51
|
+
const emailRefused = changeEmail.failed()?.data
|
|
52
|
+
const usernameSaved = changeUsername.succeeded() !== undefined
|
|
53
|
+
const usernameRefused = changeUsername.failed()?.data
|
|
54
|
+
const profile = await auth.account.profile(ctx.locale)
|
|
55
|
+
const profileSaved = saveProfile.succeeded() !== undefined
|
|
56
|
+
const profileRefused = saveProfile.failed()?.data
|
|
57
|
+
const profileProblems = profileRefused && 'errors' in profileRefused ? profileRefused.errors : {}
|
|
58
|
+
const problemOf = (name: string) => Object.hasOwn(profileProblems, name) ? profileProblems[name] : undefined
|
|
59
|
+
---
|
|
60
|
+
PageSection(title={wording('auth.account.profile.title', 'Profile')})
|
|
61
|
+
.flex.flex-col.gap-3
|
|
62
|
+
.flex.flex-wrap.items-center.gap-2
|
|
63
|
+
if user.email
|
|
64
|
+
span#kauth-email {user.email}
|
|
65
|
+
if user.emailVerified
|
|
66
|
+
Badge(label={wording('auth.account.profile.verified', 'Verified')} tone="success")
|
|
67
|
+
else
|
|
68
|
+
Badge(label={wording('auth.account.profile.unverified', 'Not verified')} tone="warning")
|
|
69
|
+
else
|
|
70
|
+
span {wording('auth.account.profile.noEmail', 'No address yet')}
|
|
71
|
+
if user.email && !user.emailVerified && features.email
|
|
72
|
+
form(action={resend})
|
|
73
|
+
if resent
|
|
74
|
+
Message(tone="success" text={wording('auth.account.profile.resent', 'A verification link is on its way.')})
|
|
75
|
+
if resendError
|
|
76
|
+
Message(text={failure(resendError)})
|
|
77
|
+
Button(type="submit" label={wording('auth.account.profile.resend', 'Mail me a verification link')})
|
|
78
|
+
form.flex.flex-col.gap-3(action={changeUsername})
|
|
79
|
+
FormField(label={wording('auth.account.profile.username', 'Username')} issue={usernameRefused && 'username' in usernameRefused ? issue('username', usernameRefused.username, features.rules) : undefined})
|
|
80
|
+
input(name="username" value={user.username ?? ''} autocomplete="username" required)
|
|
81
|
+
if usernameSaved
|
|
82
|
+
Message(tone="success" text={wording('auth.account.profile.usernameSaved', 'Your username is saved.')})
|
|
83
|
+
if usernameRefused && 'error' in usernameRefused
|
|
84
|
+
Message(text={failure(usernameRefused.error)})
|
|
85
|
+
Button(type="submit" label={wording('auth.account.profile.saveUsername', 'Save the username')})
|
|
86
|
+
if profile
|
|
87
|
+
form.flex.flex-col.gap-3(action={saveProfile})
|
|
88
|
+
for field of profile key={field.name}
|
|
89
|
+
FormField(label={field.label} issue={problemOf(field.name) && profileIssue(problemOf(field.name)!, field.maxLength)} required={field.required})
|
|
90
|
+
if field.type === 'textarea'
|
|
91
|
+
textarea(name={field.name} rows="4" maxlength={field.maxLength} required={field.required}) {field.value}
|
|
92
|
+
else
|
|
93
|
+
input(name={field.name} type={field.type ?? 'text'} value={field.value} autocomplete={field.autocomplete} maxlength={field.maxLength} required={field.required})
|
|
94
|
+
if profileSaved
|
|
95
|
+
Message(tone="success" text={wording('auth.account.profile.profileSaved', 'Your profile is saved.')})
|
|
96
|
+
if profileRefused && 'error' in profileRefused
|
|
97
|
+
Message(text={failure(profileRefused.error)})
|
|
98
|
+
Button(type="submit" label={wording('auth.account.profile.saveProfile', 'Save the profile')})
|
|
99
|
+
if features.email
|
|
100
|
+
PageSection(title={wording('auth.account.profile.changeEmail', 'Change your address')})
|
|
101
|
+
form.flex.flex-col.gap-3(action={changeEmail})
|
|
102
|
+
FormField(label={wording('auth.account.profile.newEmail', 'New address')} issue={emailRefused && 'email' in emailRefused ? issue('email', emailRefused.email) : undefined} required)
|
|
103
|
+
input(name="email" type="email" autocomplete="email" required)
|
|
104
|
+
Proof(user={user} secondFactor)
|
|
105
|
+
if emailSent
|
|
106
|
+
Message(tone="success" text={wording('auth.account.profile.changeSent', 'Open the link mailed to {email} to confirm it.', { email: emailSent })})
|
|
107
|
+
if emailRefused && 'error' in emailRefused
|
|
108
|
+
Message(text={failure(emailRefused.error)})
|
|
109
|
+
Button(type="submit" label={wording('auth.account.profile.sendChange', 'Mail a confirmation link')})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
--- server
|
|
2
|
+
import { action } from 'kay'
|
|
3
|
+
import { formatDate } from 'kay/i18n'
|
|
4
|
+
import { wording } from 'kay/words'
|
|
5
|
+
import Message from '../../../components/message.kay'
|
|
6
|
+
import { failure, schemas } from '../../../forms'
|
|
7
|
+
import { organizationPage, organizationsPage } from '../../../organizationPage'
|
|
8
|
+
import Badge from '@kayjs/ui/badge.kay'
|
|
9
|
+
import Button from '@kayjs/ui/button.kay'
|
|
10
|
+
import CardRow from '@kayjs/ui/card-row.kay'
|
|
11
|
+
import EmptyState from '@kayjs/ui/empty-state.kay'
|
|
12
|
+
import FormField from '@kayjs/ui/form-field.kay'
|
|
13
|
+
import PageSection from '@kayjs/ui/page-section.kay'
|
|
14
|
+
|
|
15
|
+
// Every action answers about the organization the route names, never one the input names, and the methods behind them check who may.
|
|
16
|
+
export const rename = action(schemas.organization, async ({ name, slug }, ctx) => {
|
|
17
|
+
const auth = ctx.locals.auth
|
|
18
|
+
const result = await auth.organizations.update(String(ctx.params.org), { name: name.trim(), slug: slug?.trim() || null })
|
|
19
|
+
|
|
20
|
+
if (!result.ok)
|
|
21
|
+
return ctx.fail(400, { error: result.error.code })
|
|
22
|
+
|
|
23
|
+
const { organization } = result
|
|
24
|
+
|
|
25
|
+
return ctx.redirect(organizationPage(auth.features(), organization))
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
export const setRoles = action(schemas.memberRoles, async ({ userId, roles }, ctx) => {
|
|
29
|
+
const result = await ctx.locals.auth.organizations.setMemberRoles(String(ctx.params.org), userId, (roles ?? []) as never[])
|
|
30
|
+
|
|
31
|
+
return result.ok ? 'done' : ctx.fail(400, { error: result.error.code })
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
export const removeMember = action(schemas.member, async ({ userId }, ctx) => {
|
|
35
|
+
const result = await ctx.locals.auth.organizations.removeMember(String(ctx.params.org), userId)
|
|
36
|
+
|
|
37
|
+
return result.ok ? 'done' : ctx.fail(400, { error: result.error.code })
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
export const invite = action(schemas.invite, async ({ email, roles }, ctx) => {
|
|
41
|
+
const result = await ctx.locals.auth.organizations.invite(String(ctx.params.org), email.trim(), roles?.length ? roles as never[] : undefined)
|
|
42
|
+
|
|
43
|
+
return result.ok ? result.invitation.email : ctx.fail(400, { error: result.error.code })
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
export const revoke = action(schemas.id, async ({ id }, ctx) => {
|
|
47
|
+
const result = await ctx.locals.auth.organizations.revokeInvitation(String(ctx.params.org), id)
|
|
48
|
+
|
|
49
|
+
return result.ok ? 'done' : ctx.fail(400, { error: result.error.code })
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
export const leave = action(schemas.adminDelete, async ({ confirm }, ctx) => {
|
|
53
|
+
if (confirm !== true)
|
|
54
|
+
return ctx.fail(400, { error: 'form' })
|
|
55
|
+
|
|
56
|
+
const auth = ctx.locals.auth
|
|
57
|
+
const result = await auth.organizations.leave(String(ctx.params.org))
|
|
58
|
+
|
|
59
|
+
return result.ok ? ctx.redirect(organizationsPage(auth.features())) : ctx.fail(400, { error: result.error.code })
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
export const remove = action(schemas.adminDelete, async ({ confirm }, ctx) => {
|
|
63
|
+
if (confirm !== true)
|
|
64
|
+
return ctx.fail(400, { error: 'form' })
|
|
65
|
+
|
|
66
|
+
const auth = ctx.locals.auth
|
|
67
|
+
const result = await auth.organizations.delete(String(ctx.params.org))
|
|
68
|
+
|
|
69
|
+
return result.ok ? ctx.redirect(organizationsPage(auth.features())) : ctx.fail(400, { error: result.error.code })
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
const auth = ctx.locals.auth
|
|
73
|
+
const features = auth.features()
|
|
74
|
+
const settings = features.organizations!
|
|
75
|
+
const found = await auth.organizations.get(String(ctx.params.org))
|
|
76
|
+
|
|
77
|
+
if (found === null)
|
|
78
|
+
ctx.notFound()
|
|
79
|
+
|
|
80
|
+
const { organization } = found
|
|
81
|
+
const me = await auth.user()
|
|
82
|
+
// The roles the viewer holds, through the hierarchy.
|
|
83
|
+
const held = new Set(found.roles.flatMap(role => settings.grants[role] ?? []))
|
|
84
|
+
|
|
85
|
+
const manages = held.has(settings.manage)
|
|
86
|
+
const owns = held.has(settings.owner)
|
|
87
|
+
const members = await auth.organizations.members(organization.id)
|
|
88
|
+
const people = members.ok ? members.members : []
|
|
89
|
+
const open = manages ? await auth.organizations.invitations(organization.id) : null
|
|
90
|
+
const invitations = open?.ok ? open.invitations : []
|
|
91
|
+
// A manager changes only members holding no role above theirs, and gives only roles they hold.
|
|
92
|
+
const editable = (roles: string[]) => manages && roles.every(role => held.has(role))
|
|
93
|
+
const grantable = settings.roles.filter(role => held.has(role))
|
|
94
|
+
const refused = [rename, setRoles, removeMember, invite, revoke, leave, remove].map(form => form.failed()?.data?.error).find(error => error !== undefined)
|
|
95
|
+
const invited = invite.succeeded()?.data
|
|
96
|
+
const date = (value: Date) => formatDate(value, { dateStyle: 'medium' })
|
|
97
|
+
const label = (user: { email: string | null, username: string | null, id: string }) => user.username ?? user.email ?? user.id
|
|
98
|
+
---
|
|
99
|
+
head
|
|
100
|
+
title {`${organization.name} - ${wording('auth.organizations.title', 'Organizations')}`}
|
|
101
|
+
PageSection(title={organization.name})
|
|
102
|
+
@actions
|
|
103
|
+
a(href={organizationsPage(features)}) {wording('auth.organizations.back', 'All organizations')}
|
|
104
|
+
.flex.flex-wrap.items-center.gap-2
|
|
105
|
+
for role of found.roles key={role}
|
|
106
|
+
Badge(label={role} tone="accent")
|
|
107
|
+
if organization.slug
|
|
108
|
+
span.text-muted {organization.slug}
|
|
109
|
+
if invited
|
|
110
|
+
Message(tone="success" text={wording('auth.organizations.invited', 'Invitation sent to {email}.', { email: invited })})
|
|
111
|
+
if refused
|
|
112
|
+
Message(text={failure(refused)})
|
|
113
|
+
PageSection(title={wording('auth.organizations.members', 'Members')})
|
|
114
|
+
.flex.flex-col.gap-2
|
|
115
|
+
for member of people key={member.user.id}
|
|
116
|
+
CardRow(data-member={member.user.id})
|
|
117
|
+
@right
|
|
118
|
+
if editable(member.roles) && member.user.id !== me?.id
|
|
119
|
+
form(action={removeMember})
|
|
120
|
+
input(type="hidden" name="userId" value={member.user.id})
|
|
121
|
+
Button(type="submit" tone="danger" label={wording('auth.organizations.remove', 'Remove')} aria-label={`${wording('auth.organizations.remove', 'Remove')}: ${label(member.user)}`})
|
|
122
|
+
.flex.flex-col.gap-2
|
|
123
|
+
span.flex.flex-wrap.items-center.gap-2
|
|
124
|
+
strong {label(member.user)}
|
|
125
|
+
if member.user.id === me?.id
|
|
126
|
+
Badge(label={wording('auth.organizations.you', 'You')})
|
|
127
|
+
for role of member.roles key={role}
|
|
128
|
+
Badge(label={role} tone="accent")
|
|
129
|
+
span.text-muted {wording('auth.organizations.joined', 'Joined {date}', { date: date(member.joinedAt) })}
|
|
130
|
+
if editable(member.roles) && member.user.id !== me?.id
|
|
131
|
+
form.flex.flex-wrap.items-center.gap-3(action={setRoles})
|
|
132
|
+
input(type="hidden" name="userId" value={member.user.id})
|
|
133
|
+
fieldset.flex.flex-wrap.items-center.gap-3
|
|
134
|
+
legend.sr-only {wording('auth.organizations.rolesOf', 'Roles of {name}', { name: label(member.user) })}
|
|
135
|
+
for role of grantable key={role}
|
|
136
|
+
label.flex.items-center.gap-2
|
|
137
|
+
input(type="checkbox" name="roles" value={role} checked={(member.roles as string[]).includes(role)})
|
|
138
|
+
| {role}
|
|
139
|
+
Button(type="submit" label={wording('auth.organizations.saveRoles', 'Save the roles')} aria-label={`${wording('auth.organizations.saveRoles', 'Save the roles')}: ${label(member.user)}`})
|
|
140
|
+
if manages
|
|
141
|
+
PageSection(title={wording('auth.organizations.invitations', 'Invitations')})
|
|
142
|
+
.flex.flex-col.gap-2
|
|
143
|
+
for invitation of invitations key={invitation.id}
|
|
144
|
+
CardRow(data-invitation={invitation.id})
|
|
145
|
+
@right
|
|
146
|
+
form(action={revoke})
|
|
147
|
+
input(type="hidden" name="id" value={invitation.id})
|
|
148
|
+
Button(type="submit" tone="danger" label={wording('auth.organizations.revoke', 'Revoke')} aria-label={`${wording('auth.organizations.revoke', 'Revoke')}: ${invitation.email}`})
|
|
149
|
+
.flex.flex-col
|
|
150
|
+
strong {invitation.email}
|
|
151
|
+
span.flex.flex-wrap.items-center.gap-2
|
|
152
|
+
for role of invitation.roles key={role}
|
|
153
|
+
Badge(label={role} tone="accent")
|
|
154
|
+
span.text-muted {wording('auth.organizations.expires', 'Expires {date}', { date: date(invitation.expiresAt) })}
|
|
155
|
+
else
|
|
156
|
+
EmptyState(title={wording('auth.organizations.noInvitations', 'No open invitation.')})
|
|
157
|
+
if features.email
|
|
158
|
+
form.flex.flex-col.gap-3(action={invite})
|
|
159
|
+
FormField(label={wording('auth.organizations.inviteEmail', 'Email address')} required)
|
|
160
|
+
input(type="email" name="email" maxlength="254" autocomplete="off" required)
|
|
161
|
+
if grantable.length > 0
|
|
162
|
+
FormField(label={wording('auth.organizations.inviteRoles', 'Roles')} group)
|
|
163
|
+
for role of grantable key={role}
|
|
164
|
+
label.flex.items-center.gap-2
|
|
165
|
+
input(type="checkbox" name="roles" value={role})
|
|
166
|
+
| {role}
|
|
167
|
+
Button(type="submit" tone="accent" label={wording('auth.organizations.invite', 'Invite')})
|
|
168
|
+
PageSection(title={wording('auth.organizations.rename', 'Name and address')})
|
|
169
|
+
form.flex.flex-col.gap-3(action={rename})
|
|
170
|
+
FormField(label={wording('auth.organizations.name', 'Name')} required)
|
|
171
|
+
input(name="name" maxlength="100" value={organization.name} required)
|
|
172
|
+
FormField(label={wording('auth.organizations.slug', 'Address')} hint={wording('auth.organizations.slugHint', 'Lowercase letters, digits and dashes. Leave empty for none.')})
|
|
173
|
+
input(name="slug" maxlength="64" autocapitalize="none" value={organization.slug ?? ''})
|
|
174
|
+
Button(type="submit" label={wording('auth.organizations.save', 'Save')})
|
|
175
|
+
PageSection(title={wording('auth.organizations.leave', 'Leave the organization')})
|
|
176
|
+
form.flex.flex-col.gap-3(action={leave})
|
|
177
|
+
label.flex.items-center.gap-2
|
|
178
|
+
input(type="checkbox" name="confirm" required)
|
|
179
|
+
| {wording('auth.organizations.leaveConfirm', 'I want to leave')}
|
|
180
|
+
Button(type="submit" tone="danger" label={wording('auth.organizations.leave', 'Leave the organization')})
|
|
181
|
+
if owns
|
|
182
|
+
PageSection(title={wording('auth.organizations.delete', 'Delete the organization')})
|
|
183
|
+
form.flex.flex-col.gap-3(action={remove})
|
|
184
|
+
p {wording('auth.organizations.deleteIntro', 'The organization, its memberships and its invitations are deleted for good. This cannot be undone.')}
|
|
185
|
+
label.flex.items-center.gap-2
|
|
186
|
+
input(type="checkbox" name="confirm" required)
|
|
187
|
+
| {wording('auth.organizations.deleteConfirm', 'I understand')}
|
|
188
|
+
Button(type="submit" tone="danger" label={wording('auth.organizations.delete', 'Delete the organization')})
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Auth } from '../../../../auth'
|
|
2
|
+
import { guard } from 'kay'
|
|
3
|
+
|
|
4
|
+
// The organization pages exist only on a site declaring `organizations`; anywhere else they answer 404, pages and actions alike.
|
|
5
|
+
export default guard<{ auth: Auth }>((ctx) => {
|
|
6
|
+
if (ctx.locals.auth.features().organizations === null)
|
|
7
|
+
ctx.deny(404)
|
|
8
|
+
})
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
--- server
|
|
2
|
+
import { action } from 'kay'
|
|
3
|
+
import { formatDate } from 'kay/i18n'
|
|
4
|
+
import { wording } from 'kay/words'
|
|
5
|
+
import Message from '../../../components/message.kay'
|
|
6
|
+
import { failure, schemas } from '../../../forms'
|
|
7
|
+
import { organizationPage } from '../../../organizationPage'
|
|
8
|
+
import Badge from '@kayjs/ui/badge.kay'
|
|
9
|
+
import Button from '@kayjs/ui/button.kay'
|
|
10
|
+
import CardRow from '@kayjs/ui/card-row.kay'
|
|
11
|
+
import EmptyState from '@kayjs/ui/empty-state.kay'
|
|
12
|
+
import FormField from '@kayjs/ui/form-field.kay'
|
|
13
|
+
import PageSection from '@kayjs/ui/page-section.kay'
|
|
14
|
+
|
|
15
|
+
export const create = action(schemas.organization, async ({ name, slug }, ctx) => {
|
|
16
|
+
const result = await ctx.locals.auth.organizations.create({ name: name.trim(), ...slug?.trim() && { slug: slug.trim() } })
|
|
17
|
+
|
|
18
|
+
if (!result.ok)
|
|
19
|
+
return ctx.fail(400, { error: result.error.code })
|
|
20
|
+
|
|
21
|
+
const { organization } = result.membership
|
|
22
|
+
|
|
23
|
+
return ctx.redirect(organizationPage(ctx.locals.auth.features(), organization))
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
// An invitation is accepted or turned down by its id, which only the signed-in user's own open ones answer to.
|
|
27
|
+
export const accept = action(schemas.id, async ({ id }, ctx) => {
|
|
28
|
+
const result = await ctx.locals.auth.organizations.acceptInvitation(id)
|
|
29
|
+
|
|
30
|
+
if (!result.ok)
|
|
31
|
+
return ctx.fail(400, { error: result.error.code })
|
|
32
|
+
|
|
33
|
+
const { organization } = result.membership
|
|
34
|
+
|
|
35
|
+
return ctx.redirect(organizationPage(ctx.locals.auth.features(), organization))
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
export const reject = action(schemas.id, async ({ id }, ctx) => {
|
|
39
|
+
const result = await ctx.locals.auth.organizations.rejectInvitation(id)
|
|
40
|
+
|
|
41
|
+
return result.ok ? 'rejected' : ctx.fail(400, { error: result.error.code })
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const auth = ctx.locals.auth
|
|
45
|
+
const features = auth.features()
|
|
46
|
+
const [memberships, invitations] = await Promise.all([auth.organizations.list(), auth.organizations.myInvitations()])
|
|
47
|
+
const refused = [create, accept, reject].map(form => form.failed()?.data?.error).find(error => error !== undefined)
|
|
48
|
+
const date = (value: Date) => formatDate(value, { dateStyle: 'medium' })
|
|
49
|
+
---
|
|
50
|
+
PageSection(title={wording('auth.organizations.title', 'Organizations')})
|
|
51
|
+
.flex.flex-col.gap-2
|
|
52
|
+
for membership of memberships key={membership.organization.id}
|
|
53
|
+
CardRow(data-organization={membership.organization.id})
|
|
54
|
+
.flex.flex-col
|
|
55
|
+
a(href={organizationPage(features, membership.organization)}) {membership.organization.name}
|
|
56
|
+
span.flex.flex-wrap.items-center.gap-2
|
|
57
|
+
for role of membership.roles key={role}
|
|
58
|
+
Badge(label={role} tone="accent")
|
|
59
|
+
span.text-muted {wording('auth.organizations.joined', 'Joined {date}', { date: date(membership.joinedAt) })}
|
|
60
|
+
else
|
|
61
|
+
EmptyState(title={wording('auth.organizations.none', 'You belong to no organization yet.')})
|
|
62
|
+
if invitations.length > 0
|
|
63
|
+
PageSection(title={wording('auth.organizations.invitationsForYou', 'Invitations for you')})
|
|
64
|
+
.flex.flex-col.gap-2
|
|
65
|
+
for invitation of invitations key={invitation.id}
|
|
66
|
+
CardRow(data-invitation={invitation.id})
|
|
67
|
+
@right
|
|
68
|
+
.flex.flex-wrap.gap-2
|
|
69
|
+
form(action={accept})
|
|
70
|
+
input(type="hidden" name="id" value={invitation.id})
|
|
71
|
+
Button(type="submit" tone="accent" label={wording('auth.organizations.accept', 'Join')} aria-label={`${wording('auth.organizations.accept', 'Join')}: ${invitation.organization.name}`})
|
|
72
|
+
form(action={reject})
|
|
73
|
+
input(type="hidden" name="id" value={invitation.id})
|
|
74
|
+
Button(type="submit" label={wording('auth.organizations.reject', 'Decline')} aria-label={`${wording('auth.organizations.reject', 'Decline')}: ${invitation.organization.name}`})
|
|
75
|
+
.flex.flex-col
|
|
76
|
+
strong {invitation.organization.name}
|
|
77
|
+
span.text-muted {wording('auth.organizations.expires', 'Expires {date}', { date: date(invitation.expiresAt) })}
|
|
78
|
+
if refused
|
|
79
|
+
Message(text={failure(refused)})
|
|
80
|
+
PageSection(title={wording('auth.organizations.create', 'Create an organization')})
|
|
81
|
+
form.flex.flex-col.gap-3(action={create})
|
|
82
|
+
FormField(label={wording('auth.organizations.name', 'Name')} required)
|
|
83
|
+
input(name="name" maxlength="100" required)
|
|
84
|
+
FormField(label={wording('auth.organizations.slug', 'Address')} hint={wording('auth.organizations.slugHint', 'Lowercase letters, digits and dashes. Leave empty for none.')})
|
|
85
|
+
input(name="slug" maxlength="64" autocapitalize="none")
|
|
86
|
+
Button(type="submit" tone="accent" label={wording('auth.organizations.create', 'Create an organization')})
|