or3-provider-basic-auth 0.0.13 → 0.0.14

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.
Files changed (31) hide show
  1. package/README.md +177 -0
  2. package/dist/module.json +1 -1
  3. package/dist/runtime/assets/signin-hero-dark.webp +0 -0
  4. package/dist/runtime/assets/signin-hero-light.webp +0 -0
  5. package/dist/runtime/assets/signup-clouds-bottom-dark.webp +0 -0
  6. package/dist/runtime/assets/signup-clouds-bottom-light.webp +0 -0
  7. package/dist/runtime/assets/signup-hero-dark.webp +0 -0
  8. package/dist/runtime/assets/signup-hero-light.webp +0 -0
  9. package/dist/runtime/components/BasicAuthChangePasswordModal.client.vue +31 -5
  10. package/dist/runtime/components/BasicAuthLockPage.client.d.vue.ts +13 -1
  11. package/dist/runtime/components/BasicAuthLockPage.client.vue +68 -15
  12. package/dist/runtime/components/BasicAuthLockPage.client.vue.d.ts +13 -1
  13. package/dist/runtime/components/BasicAuthRegisterModal.client.d.vue.ts +1 -0
  14. package/dist/runtime/components/BasicAuthRegisterModal.client.vue +91 -31
  15. package/dist/runtime/components/BasicAuthRegisterModal.client.vue.d.ts +1 -0
  16. package/dist/runtime/components/BasicAuthSignInModal.client.d.vue.ts +2 -1
  17. package/dist/runtime/components/BasicAuthSignInModal.client.vue +71 -35
  18. package/dist/runtime/components/BasicAuthSignInModal.client.vue.d.ts +2 -1
  19. package/dist/runtime/components/SidebarAuthButtonBasic.client.d.vue.ts +9 -1
  20. package/dist/runtime/components/SidebarAuthButtonBasic.client.vue +33 -44
  21. package/dist/runtime/components/SidebarAuthButtonBasic.client.vue.d.ts +9 -1
  22. package/dist/runtime/lib/constants.d.ts +2 -0
  23. package/dist/runtime/lib/constants.js +1 -0
  24. package/dist/runtime/plugins/auth-status.client.js +7 -2
  25. package/dist/runtime/plugins/basic-auth-ui.client.js +4 -2
  26. package/dist/runtime/server/api/basic-auth/_helpers.js +1 -1
  27. package/dist/runtime/server/api/basic-auth/change-password.post.js +2 -2
  28. package/dist/runtime/server/api/basic-auth/register.post.js +19 -11
  29. package/dist/runtime/server/api/basic-auth/sign-in.post.js +14 -13
  30. package/dist/runtime/server/api/basic-auth/sign-out.post.js +2 -1
  31. package/package.json +2 -2
@@ -1,11 +1,13 @@
1
- import { defineNuxtPlugin } from "#imports";
1
+ import { defineNuxtPlugin, useRuntimeConfig } from "#imports";
2
2
  import { registerClientAuthStatusResolver } from "~/composables/auth/useClientAuthStatus.client";
3
3
  import { registerClientSessionRecovery } from "~/composables/auth/useClientSessionRecovery";
4
4
  import { silentRefreshOnce } from "../lib/silent-refresh.client.js";
5
5
  async function fetchSession() {
6
6
  try {
7
7
  return await $fetch("/api/auth/session", {
8
- cache: "no-store"
8
+ cache: "no-store",
9
+ // ofetch would resend a failed GET at once, even after a 429.
10
+ retry: 0
9
11
  });
10
12
  } catch {
11
13
  return null;
@@ -13,6 +15,9 @@ async function fetchSession() {
13
15
  }
14
16
  export default defineNuxtPlugin(() => {
15
17
  if (import.meta.server) return;
18
+ const config = useRuntimeConfig();
19
+ if (!config.public.ssrAuthEnabled) return;
20
+ if (config.public.authProvider && config.public.authProvider !== "basic-auth") return;
16
21
  registerClientSessionRecovery(async () => silentRefreshOnce());
17
22
  registerClientAuthStatusResolver(async () => {
18
23
  const first = await fetchSession();
@@ -7,14 +7,16 @@ function tryRegisterAuthUiAdapter(component) {
7
7
  }
8
8
  nuxtApp.$registerAuthUiAdapter({
9
9
  id: BASIC_AUTH_PROVIDER_ID,
10
- component
10
+ component,
11
+ signInComponent: component
11
12
  });
12
13
  return true;
13
14
  }
14
15
  function enqueueAuthUiAdapter(component) {
15
16
  const payload = {
16
17
  id: BASIC_AUTH_PROVIDER_ID,
17
- component
18
+ component,
19
+ signInComponent: component
18
20
  };
19
21
  const globalState = globalThis;
20
22
  if (!Array.isArray(globalState.__or3AuthUiAdapterQueue__)) {
@@ -18,7 +18,7 @@ export async function parseBodyWithSchema(event, schema) {
18
18
  const firstIssue = result.error.issues[0];
19
19
  const path = firstIssue?.path?.join(".") || "input";
20
20
  const detail = firstIssue?.message || "Invalid request";
21
- if (detail === "Password must be 72 UTF-8 bytes or fewer.") {
21
+ if (detail === "Password is too long. Please shorten it.") {
22
22
  throw createError({ statusCode: 400, statusMessage: detail });
23
23
  }
24
24
  if (path === "password" || path === "confirmPassword") {
@@ -16,10 +16,10 @@ const changePasswordSchema = z.object({
16
16
  // Existing accounts may have legacy short passwords; only new password is length-enforced.
17
17
  currentPassword: z.string().min(1).max(512),
18
18
  newPassword: z.string().min(8).max(512).refine(newPasswordFitsBcrypt, {
19
- message: "Password must be 72 UTF-8 bytes or fewer."
19
+ message: "Password is too long. Please shorten it."
20
20
  }),
21
21
  confirmNewPassword: z.string().min(8).max(512).refine(newPasswordFitsBcrypt, {
22
- message: "Password must be 72 UTF-8 bytes or fewer."
22
+ message: "Password is too long. Please shorten it."
23
23
  })
24
24
  }).refine((input) => input.newPassword === input.confirmNewPassword, {
25
25
  message: "Passwords must match"
@@ -29,17 +29,17 @@ import { assertBasicAuthReady, noStore, parseBodyWithSchema } from "./_helpers.j
29
29
  const registerSchema = z.object({
30
30
  email: z.string().email().max(320),
31
31
  password: z.string().min(8).max(512).refine(newPasswordFitsBcrypt, {
32
- message: "Password must be 72 UTF-8 bytes or fewer."
32
+ message: "Password is too long. Please shorten it."
33
33
  }),
34
34
  confirmPassword: z.string().min(8).max(512).refine(newPasswordFitsBcrypt, {
35
- message: "Password must be 72 UTF-8 bytes or fewer."
35
+ message: "Password is too long. Please shorten it."
36
36
  }),
37
37
  displayName: z.string().trim().min(1).max(120).optional(),
38
38
  inviteToken: z.string().trim().min(1).max(4096).optional()
39
39
  }).refine((value) => value.password === value.confirmPassword, {
40
40
  message: "Passwords must match"
41
41
  });
42
- function mapRegistrationErrorToHttp(reason) {
42
+ function mapRegistrationErrorToHttp(reason, mode) {
43
43
  if (reason === "disabled") {
44
44
  throw createError({
45
45
  statusCode: 403,
@@ -52,10 +52,18 @@ function mapRegistrationErrorToHttp(reason) {
52
52
  statusMessage: "Invite-only registration is temporarily unavailable."
53
53
  });
54
54
  }
55
- throw createError({
56
- statusCode: 403,
57
- statusMessage: "A valid invite is required to register."
58
- });
55
+ if (reason === "invite_email_mismatch") {
56
+ throw createError({
57
+ statusCode: 403,
58
+ statusMessage: "This invitation is for a different email address. Check for typos or use the email that received the invite."
59
+ });
60
+ }
61
+ if (reason === "invite_required") {
62
+ throw createError({ statusCode: 403, statusMessage: "Enter an invite code to create an account." });
63
+ }
64
+ const nextStep = mode === "open" ? "Clear the invite code to sign up without one, or ask for a new invitation." : reason === "invite_expired" ? "Ask for a new invitation." : "Check your invite link or ask for a new invitation.";
65
+ const invalidMessage = reason === "invite_expired" ? "This invitation has expired." : "This invite code isn't valid. It may be mistyped, already used, or cancelled.";
66
+ throw createError({ statusCode: 403, statusMessage: `${invalidMessage} ${nextStep}` });
59
67
  }
60
68
  export async function handleRegister(event) {
61
69
  assertBasicAuthReady(event);
@@ -67,18 +75,18 @@ export async function handleRegister(event) {
67
75
  const runtimeConfig = useRuntimeConfig();
68
76
  const mode = resolveRegistrationMode(runtimeConfig);
69
77
  if (mode === "disabled") {
70
- mapRegistrationErrorToHttp("disabled");
78
+ mapRegistrationErrorToHttp("disabled", mode);
71
79
  }
72
80
  const inviteToken = body.inviteToken ?? getInviteTokenFromEvent(event);
73
81
  if (mode === "invite_only" && !inviteToken) {
74
- mapRegistrationErrorToHttp("invite_required");
82
+ mapRegistrationErrorToHttp("invite_required", mode);
75
83
  }
76
84
  if (inviteToken) {
77
85
  const syncConfig = runtimeConfig;
78
86
  const storeId = syncConfig.sync?.provider ?? syncConfig.public?.sync?.provider ?? "convex";
79
87
  const store = getAuthWorkspaceStore(storeId);
80
88
  if (!store) {
81
- mapRegistrationErrorToHttp("invite_unsupported");
89
+ mapRegistrationErrorToHttp("invite_unsupported", mode);
82
90
  }
83
91
  const decision = await validateInviteRegistration({
84
92
  event,
@@ -88,7 +96,7 @@ export async function handleRegister(event) {
88
96
  inviteToken
89
97
  });
90
98
  if (!decision.allowed) {
91
- mapRegistrationErrorToHttp(decision.reason);
99
+ mapRegistrationErrorToHttp(decision.reason, mode);
92
100
  }
93
101
  }
94
102
  const existing = findAccountByEmail(email);
@@ -1,7 +1,8 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { defineEventHandler, setCookie } from "h3";
3
3
  import { useRuntimeConfig } from "#imports";
4
- import { verifyInviteToken } from "~~/server/auth/invite-token";
4
+ import { resolveRegistrationMode, validateInviteRegistration } from "~~/server/auth/registration";
5
+ import { getAuthWorkspaceStore } from "~~/server/auth/store/registry";
5
6
  import { z } from "zod";
6
7
  import { clearAuthCookies, setAccessCookie, setRefreshCookie } from "../../lib/cookies.js";
7
8
  import { getBasicAuthConfig } from "../../lib/config.js";
@@ -45,6 +46,10 @@ export async function handleSignIn(event) {
45
46
  clearAuthCookies(event);
46
47
  throw createInvalidCredentialsError();
47
48
  }
49
+ const runtimeConfig = useRuntimeConfig();
50
+ const syncConfig = runtimeConfig;
51
+ const store = input.inviteToken ? getAuthWorkspaceStore(syncConfig.sync?.provider ?? syncConfig.public?.sync?.provider ?? "convex") : null;
52
+ const invitation = input.inviteToken && store ? await validateInviteRegistration({ event, store, mode: resolveRegistrationMode(runtimeConfig), email, inviteToken: input.inviteToken }) : null;
48
53
  const config = getBasicAuthConfig();
49
54
  if (config.refreshTtlSeconds <= 0 || config.accessTtlSeconds <= 0) {
50
55
  throw createInvalidRequestError();
@@ -71,18 +76,14 @@ export async function handleSignIn(event) {
71
76
  });
72
77
  setAccessCookie(event, accessToken, config.accessTtlSeconds);
73
78
  setRefreshCookie(event, refreshToken, config.refreshTtlSeconds);
74
- const secret = useRuntimeConfig().auth?.invite?.tokenSecret;
75
- if (input.inviteToken && secret) {
76
- const verified = verifyInviteToken(input.inviteToken, secret);
77
- if (verified.ok && normalizeEmail(verified.payload.email) === email) {
78
- setCookie(event, "or3_invite_token", input.inviteToken, {
79
- httpOnly: true,
80
- sameSite: "lax",
81
- secure: process.env.NODE_ENV === "production",
82
- path: "/",
83
- maxAge: Math.min(600, verified.payload.exp - Math.floor(Date.now() / 1e3))
84
- });
85
- }
79
+ if (invitation?.allowed && invitation.invite) {
80
+ setCookie(event, "or3_invite_token", input.inviteToken, {
81
+ httpOnly: true,
82
+ sameSite: "lax",
83
+ secure: process.env.NODE_ENV === "production",
84
+ path: "/",
85
+ maxAge: Math.min(600, invitation.invite.payload.exp - Math.floor(Date.now() / 1e3))
86
+ });
86
87
  }
87
88
  return { ok: true };
88
89
  }
@@ -1,4 +1,4 @@
1
- import { defineEventHandler } from "h3";
1
+ import { defineEventHandler, deleteCookie } from "h3";
2
2
  import { revokeHostActivationsForUser } from "~~/server/utils/plugins/isolation/activation-registry";
3
3
  import { clearAuthCookies } from "../../lib/cookies.js";
4
4
  import {
@@ -65,6 +65,7 @@ export async function handleSignOut(event) {
65
65
  }
66
66
  }
67
67
  clearAuthCookies(event);
68
+ deleteCookie(event, "or3_invite_token", { httpOnly: true, sameSite: "lax", path: "/" });
68
69
  return { ok: true };
69
70
  }
70
71
  export default defineEventHandler(async (event) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "or3-provider-basic-auth",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/Saluana/or3-provider-basic-auth.git"
@@ -53,7 +53,7 @@
53
53
  "zod": "^4.1.12"
54
54
  },
55
55
  "peerDependencies": {
56
- "@nuxt/kit": "^3.0.0",
56
+ "@nuxt/kit": "^3.0.0 || ^4.0.0",
57
57
  "nuxt": "^3.0.0 || ^4.0.0"
58
58
  },
59
59
  "devDependencies": {