create-mana 0.0.0-stage → 0.1.0

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 (101) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +28 -2
  3. package/bin/create-mana.mjs +5 -0
  4. package/kits/auth/files/src/api/auth.ts +100 -0
  5. package/kits/auth/files/src/api/profile.ts +48 -0
  6. package/kits/auth/files/src/api/types.ts +28 -0
  7. package/kits/auth/files/src/components/AppLayout.mana +31 -0
  8. package/kits/auth/files/src/components/VerifyBanner.mana +47 -0
  9. package/kits/auth/files/src/router/routes.ts +31 -0
  10. package/kits/auth/files/src/shared/events.ts +11 -0
  11. package/kits/auth/files/src/stores/session.ts +99 -0
  12. package/kits/auth/files/src/views/ForgotPassword.mana +83 -0
  13. package/kits/auth/files/src/views/Home.mana +15 -0
  14. package/kits/auth/files/src/views/Profile.mana +172 -0
  15. package/kits/auth/files/src/views/Register.mana +86 -0
  16. package/kits/auth/files/src/views/ResetPassword.mana +90 -0
  17. package/kits/auth/files/src/views/SignIn.mana +84 -0
  18. package/kits/auth/files/src/views/VerifyEmail.mana +78 -0
  19. package/kits/auth/files/tests/auth.test.mjs +93 -0
  20. package/kits/auth/files/tests/home.test.mjs +38 -0
  21. package/kits/auth/files/tests/profile.test.mjs +68 -0
  22. package/kits/auth/files/tests/support/users.mjs +2 -0
  23. package/kits/auth/kit.json +80 -0
  24. package/kits/base/files/README.md +39 -0
  25. package/kits/base/kit.json +7 -0
  26. package/kits/organisations/files/src/api/auth.ts +124 -0
  27. package/kits/organisations/files/src/api/members.ts +243 -0
  28. package/kits/organisations/files/src/api/notifications.ts +63 -0
  29. package/kits/organisations/files/src/api/organisations.ts +41 -0
  30. package/kits/organisations/files/src/api/scope.ts +98 -0
  31. package/kits/organisations/files/src/api/types.ts +72 -0
  32. package/kits/organisations/files/src/assets/styles/organisations.css +46 -0
  33. package/kits/organisations/files/src/components/AppLayout.mana +90 -0
  34. package/kits/organisations/files/src/components/NotificationBell.mana +35 -0
  35. package/kits/organisations/files/src/components/NotificationList.mana +112 -0
  36. package/kits/organisations/files/src/components/OrganisationSwitcher.mana +171 -0
  37. package/kits/organisations/files/src/composables/notifications.ts +28 -0
  38. package/kits/organisations/files/src/router/routes.ts +43 -0
  39. package/kits/organisations/files/src/shared/roles.ts +10 -0
  40. package/kits/organisations/files/src/stores/notifications.ts +41 -0
  41. package/kits/organisations/files/src/stores/session.ts +158 -0
  42. package/kits/organisations/files/src/utilities/format.ts +15 -0
  43. package/kits/organisations/files/src/views/Forbidden.mana +28 -0
  44. package/kits/organisations/files/src/views/Home.mana +15 -0
  45. package/kits/organisations/files/src/views/Invite.mana +156 -0
  46. package/kits/organisations/files/src/views/InviteMember.mana +99 -0
  47. package/kits/organisations/files/src/views/Members.mana +218 -0
  48. package/kits/organisations/files/src/views/NoOrganisation.mana +184 -0
  49. package/kits/organisations/files/src/views/Notifications.mana +30 -0
  50. package/kits/organisations/files/src/views/OrganisationForm.mana +76 -0
  51. package/kits/organisations/files/src/views/OrganisationSettings.mana +62 -0
  52. package/kits/organisations/files/src/views/Profile.mana +213 -0
  53. package/kits/organisations/files/src/views/Register.mana +113 -0
  54. package/kits/organisations/files/tests/auth.test.mjs +82 -0
  55. package/kits/organisations/files/tests/home.test.mjs +43 -0
  56. package/kits/organisations/files/tests/members.test.mjs +89 -0
  57. package/kits/organisations/files/tests/notifications.test.mjs +49 -0
  58. package/kits/organisations/files/tests/organisations.test.mjs +70 -0
  59. package/kits/organisations/files/tests/profile.test.mjs +79 -0
  60. package/kits/organisations/files/tests/support/users.mjs +3 -0
  61. package/kits/organisations/kit.json +51 -0
  62. package/lib/compose.mjs +209 -0
  63. package/lib/new.mjs +161 -0
  64. package/lib/prompts.mjs +182 -0
  65. package/package.json +36 -4
  66. package/template/.nvmrc +1 -0
  67. package/template/README.md +23 -0
  68. package/template/_gitignore +25 -0
  69. package/template/index.html +17 -0
  70. package/template/package.json +22 -0
  71. package/template/public/favicon.svg +1 -0
  72. package/template/public/icons.svg +24 -0
  73. package/template/src/App.mana +15 -0
  74. package/template/src/api/ApiError.ts +17 -0
  75. package/template/src/api/client.ts +116 -0
  76. package/template/src/assets/styles/main.css +158 -0
  77. package/template/src/components/AppLogo.mana +20 -0
  78. package/template/src/components/Button.mana +25 -0
  79. package/template/src/components/ConfirmHost.mana +43 -0
  80. package/template/src/components/TextField.mana +60 -0
  81. package/template/src/components/ToastHost.mana +40 -0
  82. package/template/src/composables/media.ts +19 -0
  83. package/template/src/composables/unsaved.ts +29 -0
  84. package/template/src/main.ts +25 -0
  85. package/template/src/router/index.ts +14 -0
  86. package/template/src/router/routes.ts +11 -0
  87. package/template/src/stores/confirm.ts +45 -0
  88. package/template/src/stores/toasts.ts +64 -0
  89. package/template/src/utilities/query.ts +40 -0
  90. package/template/src/utilities/timers.ts +12 -0
  91. package/template/src/views/Home.mana +12 -0
  92. package/template/src/views/NotFound.mana +13 -0
  93. package/template/tests/home.test.mjs +29 -0
  94. package/template/tests/support/api.mjs +44 -0
  95. package/template/tests/support/check.mjs +15 -0
  96. package/template/tests/support/dom.mjs +45 -0
  97. package/template/tests/support/paths.mjs +54 -0
  98. package/template/tests/support/settle.mjs +15 -0
  99. package/template/tsconfig.json +35 -0
  100. package/template/vite.config.ts +29 -0
  101. package/versions.json +3 -0
@@ -0,0 +1,172 @@
1
+ <script setup lang="ts">
2
+ /*
3
+ * Profile: /profile ("profile"), for everyone signed in.
4
+ *
5
+ * Your details: your name, and your email, which changes only once a link
6
+ * sent to the new address is opened; until then the old one signs in,
7
+ * and the page says what's waiting, with Resend and Cancel the change. Save
8
+ * changes is on once something changed; what the API answers replaces the
9
+ * session's user, so the top bar follows, and "Saved." shows (toasts are
10
+ * for errors, A4).
11
+ * Password: the current one, a new one and its confirmation, checked as on
12
+ * sign-up, the API having the last word.
13
+ * Leaving the page with something typed and not saved asks first.
14
+ */
15
+ import { computed, reactive, ref, watch } from "@mana-js/mana";
16
+
17
+ import ApiError from "@/api/ApiError";
18
+ import * as profile from "@/api/profile";
19
+ import TextField from "@/components/TextField.mana";
20
+ import { useUnsavedChanges } from "@/composables/unsaved";
21
+ import { useSession } from "@/stores/session";
22
+ import { useToasts } from "@/stores/toasts";
23
+
24
+ defineOptions({ name: "Profile" });
25
+
26
+ const sessionStore = useSession();
27
+ const toastStore = useToasts();
28
+
29
+ const details = reactive({ name: "", email: "" });
30
+ const detailErrors = reactive<{ name?: string; email?: string }>({});
31
+ const savingDetails = ref(false);
32
+ const detailsSaved = ref(false);
33
+ const emailSent = ref<string | null>(null);
34
+
35
+ const password = reactive({ current: "", password: "", confirm: "" });
36
+ const passwordErrors = reactive<{ current?: string; password?: string; confirm?: string }>({});
37
+ const savingPassword = ref(false);
38
+ const passwordSaved = ref(false);
39
+
40
+ // The form starts from the user, and again when they change (saved, or confirmed elsewhere).
41
+ watch(() => sessionStore.user, user => {
42
+ details.name = user?.name ?? "";
43
+ details.email = user?.email ?? "";
44
+ }, { immediate: true });
45
+
46
+ const detailsChanged = computed(() => details.name != (sessionStore.user?.name ?? "") || details.email.trim().toLowerCase() != (sessionStore.user?.email ?? ""));
47
+ const passwordTyped = computed(() => !!(password.current || password.password || password.confirm));
48
+
49
+ useUnsavedChanges(() => detailsChanged.value || passwordTyped.value);
50
+
51
+ // A new change hides "Saved.".
52
+ watch(() => [details.name, details.email], () => detailsSaved.value = false);
53
+ watch(passwordTyped, typed => {
54
+ if(typed)
55
+ passwordSaved.value = false;
56
+ });
57
+
58
+ function failed(title: string, error: unknown) {
59
+ toastStore.error(title, error instanceof ApiError ? error.message : "Something went wrong. Try again.");
60
+ }
61
+
62
+ async function saveDetails() {
63
+ detailErrors.name = details.name.trim() ? undefined : "Enter your name.";
64
+ detailErrors.email = details.email.trim() ? undefined : "Enter your email.";
65
+
66
+ if(detailErrors.name || detailErrors.email || savingDetails.value)
67
+ return;
68
+
69
+ const email = details.email.trim().toLowerCase();
70
+ const newEmail = email != sessionStore.user?.email;
71
+
72
+ savingDetails.value = true;
73
+
74
+ try {
75
+ sessionStore.receive(await profile.updateProfile({ name: details.name, email }));
76
+ emailSent.value = newEmail ? email : null;
77
+ detailsSaved.value = true;
78
+ } catch(error) {
79
+ if(error instanceof ApiError && (error.field == "name" || error.field == "email"))
80
+ detailErrors[error.field] = error.message;
81
+ else
82
+ failed("Couldn't save your details", error);
83
+ } finally {
84
+ savingDetails.value = false;
85
+ }
86
+ }
87
+
88
+ async function resend() {
89
+ try {
90
+ await profile.resendEmailChange();
91
+ emailSent.value = sessionStore.user?.pendingEmail ?? null;
92
+ } catch(error) {
93
+ failed("Couldn't send the link", error);
94
+ }
95
+ }
96
+
97
+ async function cancelChange() {
98
+ try {
99
+ sessionStore.receive(await profile.cancelEmailChange());
100
+ emailSent.value = null;
101
+ } catch(error) {
102
+ failed("Couldn't cancel the change", error);
103
+ }
104
+ }
105
+
106
+ async function savePassword() {
107
+ passwordErrors.current = password.current ? undefined : "Enter your current password.";
108
+ passwordErrors.password = password.password.length >= 8 ? undefined : "Use at least 8 characters.";
109
+ passwordErrors.confirm = password.confirm == password.password ? undefined : "The passwords don't match.";
110
+
111
+ if(passwordErrors.current || passwordErrors.password || passwordErrors.confirm || savingPassword.value)
112
+ return;
113
+
114
+ savingPassword.value = true;
115
+
116
+ try {
117
+ await profile.changePassword({ current: password.current, password: password.password });
118
+ password.current = password.password = password.confirm = "";
119
+ passwordSaved.value = true;
120
+ } catch(error) {
121
+ if(error instanceof ApiError && (error.field == "current" || error.field == "password"))
122
+ passwordErrors[error.field] = error.message;
123
+ else
124
+ failed("Couldn't change your password", error);
125
+ } finally {
126
+ savingPassword.value = false;
127
+ }
128
+ }
129
+
130
+ </script>
131
+
132
+ <template>
133
+ <div class="page-head"><h1>Profile</h1></div>
134
+
135
+ <form class="card form-narrow" novalidate aria-labelledby="details-title" :class="{ 'is-submitting': savingDetails }" @submit.prevent="saveDetails">
136
+ <div class="card-head"><h2 id="details-title">Your details</h2></div>
137
+ <TextField id="profile-name" label="Name" autocomplete="name" :error="detailErrors.name" m-model="details.name" />
138
+ <TextField id="profile-email" type="email" label="Email" autocomplete="email" hint="Changing it sends a link to the new address; it changes once you open it." :error="detailErrors.email" m-model="details.email" />
139
+
140
+ <div class="pending" role="status" m-if="sessionStore.user?.pendingEmail">
141
+ <p m-if="emailSent">We sent a link to <b>{{ emailSent }}</b>. Your email changes once you open it; until then you sign in with {{ sessionStore.user?.email }}.</p>
142
+ <p m-else>Waiting for you to confirm <b>{{ sessionStore.user?.pendingEmail }}</b>. Until you open the link we sent there, you sign in with {{ sessionStore.user?.email }}.</p>
143
+ <span class="inline-actions">
144
+ <button type="button" class="link-button" @click="resend()">Resend the link</button>
145
+ <button type="button" class="link-button" @click="cancelChange()">Cancel the change</button>
146
+ </span>
147
+ </div>
148
+
149
+ <div class="form-foot">
150
+ <button type="submit" class="btn primary" :disabled="!detailsChanged || savingDetails">{{ savingDetails ? "Saving…" : "Save changes" }}</button>
151
+ <span class="saved" role="status" m-if="detailsSaved && !detailsChanged">Saved.</span>
152
+ </div>
153
+ </form>
154
+
155
+ <form class="card form-narrow" novalidate aria-labelledby="password-title" :class="{ 'is-submitting': savingPassword }" @submit.prevent="savePassword">
156
+ <div class="card-head"><h2 id="password-title">Password</h2></div>
157
+ <TextField id="profile-current" type="password" label="Current password" autocomplete="current-password" :error="passwordErrors.current" m-model="password.current" />
158
+ <TextField id="profile-password" type="password" label="New password" autocomplete="new-password" hint="At least 8 characters." :error="passwordErrors.password" m-model="password.password" />
159
+ <TextField id="profile-confirm" type="password" label="Confirm the new password" autocomplete="new-password" :error="passwordErrors.confirm" m-model="password.confirm" />
160
+ <div class="form-foot">
161
+ <button type="submit" class="btn" :disabled="!passwordTyped || savingPassword">{{ savingPassword ? "Changing…" : "Change password" }}</button>
162
+ <span class="saved" role="status" m-if="passwordSaved && !passwordTyped">Password changed.</span>
163
+ </div>
164
+ </form>
165
+
166
+ </template>
167
+
168
+ <style scoped>
169
+ .pending { margin: 0 0 14px; padding: 8px 10px; border: 1.5px dashed var(--ink); border-radius: 3px; font-size: 13px; }
170
+ .pending p { margin: 0 0 4px; }
171
+ .inline-actions { display: inline-flex; flex-wrap: wrap; gap: 12px; font-size: 13px; }
172
+ </style>
@@ -0,0 +1,86 @@
1
+ <script setup lang="ts">
2
+ /*
3
+ * The Register page: a name, an email, a password of 8 characters or more and its confirmation. The API's answer about a field shows on it, anything else as a toast; signed in, the user lands on the
4
+ * home page, where a banner asks them to confirm their email (a link was mailed).
5
+ */
6
+ import { reactive, ref, useRouter } from "@mana-js/mana";
7
+
8
+ import ApiError from "@/api/ApiError";
9
+ import AppLogo from "@/components/AppLogo.mana";
10
+ import Button from "@/components/Button.mana";
11
+ import TextField from "@/components/TextField.mana";
12
+ import { useSession } from "@/stores/session";
13
+ import { useToasts } from "@/stores/toasts";
14
+
15
+ type RegisterField = "name" | "email" | "password" | "confirm";
16
+ // @mana:register-types
17
+
18
+ const router = useRouter();
19
+ const sessionStore = useSession();
20
+ const toastStore = useToasts();
21
+ const form = reactive({ name: "", email: "", password: "", confirm: "" });
22
+ const errors = reactive<Partial<Record<RegisterField, string>>>({});
23
+ const submitting = ref(false);
24
+
25
+ // A field of the form's, as the API's errors name them (src/api/auth.ts gives them the form's names).
26
+ function isField(field: string | null): field is RegisterField {
27
+ return field == "name" || field == "email" || field == "password" || field == "confirm";
28
+ }
29
+
30
+ function validate() {
31
+ errors.name = form.name.trim() ? undefined : "Enter your full name.";
32
+ errors.email = !form.email ? "Enter your email." : !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(form.email) ? "Enter an email like name@company.com." : undefined;
33
+ errors.password = form.password.length >= 8 ? undefined : "Use at least 8 characters.";
34
+ errors.confirm = !form.confirm ? "Enter your password again." : form.confirm != form.password ? "The passwords don't match." : undefined;
35
+
36
+ return Object.values(errors).every(error => !error);
37
+ }
38
+
39
+ async function submit() {
40
+ if(!validate())
41
+ return;
42
+
43
+ submitting.value = true;
44
+ toastStore.clear();
45
+
46
+ try {
47
+ await sessionStore.register({ name: form.name.trim(), email: form.email, password: form.password, passwordConfirmation: form.confirm });
48
+ } catch(error) {
49
+ if(error instanceof ApiError && isField(error.field))
50
+ errors[error.field] = error.message;
51
+ else
52
+ toastStore.error("Couldn't create your account", error instanceof ApiError ? error.message : "Something went wrong. Try again.");
53
+
54
+ return;
55
+ } finally {
56
+ submitting.value = false;
57
+ }
58
+
59
+ await router.replace({ name: "dashboard" });
60
+ }
61
+ </script>
62
+
63
+ <template>
64
+ <div class="auth">
65
+ <AppLogo />
66
+
67
+ <h1>Create your account</h1>
68
+
69
+ <form novalidate :class="{ 'is-submitting': submitting }" :aria-busy="submitting" @submit.prevent="submit">
70
+ <TextField id="register-name" name="name" label="Full name" autocomplete="name" :error="errors.name" m-model="form.name" />
71
+ <TextField id="register-email" name="email" label="Email" type="email" autocomplete="email" placeholder="name@company.com" :error="errors.email" m-model="form.email" />
72
+ <!-- @mana:register-fields -->
73
+ <TextField id="register-password" name="password" label="Password" type="password" autocomplete="new-password" hint="At least 8 characters." :error="errors.password" m-model="form.password" />
74
+ <TextField id="register-confirm" name="confirm" label="Confirm password" type="password" autocomplete="new-password" :error="errors.confirm" m-model="form.confirm" />
75
+
76
+ <div class="submit">
77
+ <Button type="submit" :label="submitting ? 'Creating account…' : 'Create account'" :disabled="submitting" />
78
+ </div>
79
+ </form>
80
+
81
+ <p class="switch">
82
+ Already have an account?
83
+ <RouterLink to="/login">Sign in</RouterLink>
84
+ </p>
85
+ </div>
86
+ </template>
@@ -0,0 +1,90 @@
1
+ <script setup lang="ts">
2
+ /*
3
+ * The reset-password page, at /reset-password?token=…&email=…, where the
4
+ * link in the email leads: a new password and its confirmation, sent to
5
+ * the API with the link's token and email. Without them, it points back
6
+ * to /forgot-password; a token that's expired or been used says so in a
7
+ * toast, with the same way back.
8
+ *
9
+ * Components used: AppLogo, TextField, Button.
10
+ */
11
+ import { computed, reactive, ref, useRoute } from "@mana-js/mana";
12
+
13
+ import ApiError from "@/api/ApiError";
14
+ import * as auth from "@/api/auth";
15
+ import AppLogo from "@/components/AppLogo.mana";
16
+ import Button from "@/components/Button.mana";
17
+ import TextField from "@/components/TextField.mana";
18
+ import { useToasts } from "@/stores/toasts";
19
+
20
+ const route = useRoute();
21
+ const toastStore = useToasts();
22
+ const token = computed(() => route.query.token ?? "");
23
+ const email = computed(() => route.query.email ?? "");
24
+ const form = reactive({ password: "", confirm: "" });
25
+ const errors = reactive<{ password?: string; confirm?: string }>({});
26
+ const done = ref(false);
27
+ const submitting = ref(false);
28
+
29
+ async function submit() {
30
+ errors.password = form.password.length >= 8 ? undefined : "Use at least 8 characters.";
31
+ errors.confirm = !form.confirm ? "Enter your new password again."
32
+ : form.confirm != form.password ? "The passwords don't match."
33
+ : undefined;
34
+
35
+ if(errors.password || errors.confirm)
36
+ return;
37
+
38
+ submitting.value = true;
39
+ toastStore.clear();
40
+
41
+ try {
42
+ await auth.resetPassword({ token: token.value, email: email.value, password: form.password, confirmation: form.confirm });
43
+ done.value = true;
44
+ } catch(error) {
45
+ if(error instanceof ApiError && error.field == "password")
46
+ errors.password = error.message;
47
+ else if(error instanceof ApiError)
48
+ toastStore.error("Couldn't set the password", error.message, { label: "Ask for a new link", to: "/forgot-password" });
49
+ else
50
+ toastStore.error("Couldn't set the password", "Something went wrong. Try again.");
51
+ } finally {
52
+ submitting.value = false;
53
+ }
54
+ }
55
+ </script>
56
+
57
+ <template>
58
+ <div class="auth">
59
+ <AppLogo />
60
+
61
+ <h1>Set a new password</h1>
62
+
63
+ <Template m-if="!token || !email">
64
+ <div class="alert" role="alert">
65
+ This page needs the link from your reset email.
66
+ <RouterLink to="/forgot-password">Ask for a new link</RouterLink>.
67
+ </div>
68
+ </Template>
69
+
70
+ <Template m-else-if="done">
71
+ <div class="notice" role="status">Your password has been changed.</div>
72
+
73
+ <p class="switch">
74
+ <RouterLink to="/login">Sign in with your new password</RouterLink>
75
+ </p>
76
+ </Template>
77
+
78
+ <Template m-else>
79
+ <form novalidate :class="{ 'is-submitting': submitting }" :aria-busy="submitting" @submit.prevent="submit">
80
+ <TextField id="reset-password" name="password" label="New password" type="password" autocomplete="new-password" hint="At least 8 characters." :error="errors.password" m-model="form.password" />
81
+
82
+ <TextField id="reset-confirm" name="confirm" label="Confirm new password" type="password" autocomplete="new-password" :error="errors.confirm" m-model="form.confirm" />
83
+
84
+ <div class="submit">
85
+ <Button type="submit" :label="submitting ? 'Saving…' : 'Set password'" :disabled="submitting" />
86
+ </div>
87
+ </form>
88
+ </Template>
89
+ </div>
90
+ </template>
@@ -0,0 +1,84 @@
1
+ <script setup lang="ts">
2
+ /*
3
+ * The sign-in page, at /login. Signing in goes to where the auth middleware
4
+ * sent the user from (?redirect), or to the dashboard.
5
+ *
6
+ * Components used: AppLogo, TextField, Button, imported below, which the
7
+ * template finds by name.
8
+ *
9
+ * The script gives the template:
10
+ * email, password string refs, bound with m-model
11
+ * errors reactive { email?: string; password?: string }
12
+ * submitting boolean ref
13
+ * submit() validates, then signs in through the session; what
14
+ * the API says of the credentials — wrong, or too many
15
+ * tries — shows as a toast
16
+ */
17
+ import { reactive, ref, useRoute, useRouter } from "@mana-js/mana";
18
+
19
+ import ApiError from "@/api/ApiError";
20
+ import AppLogo from "@/components/AppLogo.mana";
21
+ import Button from "@/components/Button.mana";
22
+ import TextField from "@/components/TextField.mana";
23
+ import { useSession } from "@/stores/session";
24
+ import { useToasts } from "@/stores/toasts";
25
+
26
+ const route = useRoute();
27
+ const router = useRouter();
28
+ const sessionStore = useSession();
29
+ const toastStore = useToasts();
30
+ const email = ref("");
31
+ const password = ref("");
32
+ const errors = reactive<{ email?: string; password?: string }>({});
33
+ const submitting = ref(false);
34
+
35
+ async function submit() {
36
+ errors.email = email.value ? undefined : "Enter your email.";
37
+ errors.password = password.value ? undefined : "Enter your password.";
38
+
39
+ if(errors.email || errors.password)
40
+ return;
41
+
42
+ submitting.value = true;
43
+ toastStore.clear();
44
+
45
+ try {
46
+ await sessionStore.signIn(email.value, password.value);
47
+ } catch(error) {
48
+ toastStore.error("Couldn't sign you in", error instanceof ApiError ? error.message : "Something went wrong. Try again.");
49
+
50
+ return;
51
+ } finally {
52
+ submitting.value = false;
53
+ }
54
+
55
+ await router.replace(route.query.redirect ?? { name: "dashboard" });
56
+ }
57
+ </script>
58
+
59
+ <template>
60
+ <div class="auth">
61
+ <AppLogo />
62
+
63
+ <h1>Sign in to Mana Starter</h1>
64
+
65
+ <form novalidate :class="{ 'is-submitting': submitting }" :aria-busy="submitting" @submit.prevent="submit">
66
+ <TextField id="login-email" name="email" label="Email" type="email" autocomplete="email" placeholder="name@company.com" :error="errors.email" m-model="email" />
67
+
68
+ <TextField id="login-password" name="password" label="Password" type="password" autocomplete="current-password" :error="errors.password" m-model="password">
69
+ <template #action>
70
+ <RouterLink to="/forgot-password" class="link">Forgot password?</RouterLink>
71
+ </template>
72
+ </TextField>
73
+
74
+ <div class="submit">
75
+ <Button type="submit" :label="submitting ? 'Signing in…' : 'Sign in'" :disabled="submitting" />
76
+ </div>
77
+ </form>
78
+
79
+ <p class="switch">
80
+ No account yet?
81
+ <RouterLink to="/register">Create account</RouterLink>
82
+ </p>
83
+ </div>
84
+ </template>
@@ -0,0 +1,78 @@
1
+ <script setup lang="ts">
2
+ /*
3
+ * The confirm-email page, at /verify-email?id=…&hash=…&expires=…&signature=…
4
+ *, where the link in the confirmation email leads: what the API
5
+ * signed, which the page posts back to it. It confirms as soon as it
6
+ * opens and says how it went (again if the link changes while it's open).
7
+ * Open to anyone, signed in or not: signed in, confirming removes the
8
+ * banner; an expired link points back to the app, where the banner can send
9
+ * a new one.
10
+ *
11
+ * Components used: AppLogo.
12
+ */
13
+ import { computed, onWatcherCleanup, ref, useRoute, watch } from "@mana-js/mana";
14
+
15
+ import type { VerificationLink } from "@/api/auth";
16
+
17
+ import AppLogo from "@/components/AppLogo.mana";
18
+ import { useSession } from "@/stores/session";
19
+
20
+ const route = useRoute();
21
+ const sessionStore = useSession();
22
+ const status = ref<"confirming" | "confirmed" | "expired" | "missing">("missing");
23
+
24
+ // The link's four values, or null when any is missing.
25
+ const link = computed((): VerificationLink | null => {
26
+ const { id, hash, expires, signature } = route.query;
27
+
28
+ return id && hash && expires && signature ? { id, hash, expires, signature } : null;
29
+ });
30
+
31
+ // One confirmation per link: an answer that arrives after the link has
32
+ // changed, or after the page has gone, is ignored.
33
+ watch(() => link.value, verification => {
34
+ let current = true;
35
+
36
+ onWatcherCleanup(() => current = false);
37
+ status.value = verification ? "confirming" : "missing";
38
+
39
+ if(!verification)
40
+ return;
41
+
42
+ const settle = (outcome: "confirmed" | "expired") => {
43
+ if(current)
44
+ status.value = outcome;
45
+ };
46
+
47
+ sessionStore.verify(verification)
48
+ .then(() => settle("confirmed"))
49
+ .catch(() => settle("expired"));
50
+ }, { immediate: true });
51
+ </script>
52
+
53
+ <template>
54
+ <div class="auth">
55
+ <AppLogo />
56
+
57
+ <h1>Confirm your email</h1>
58
+
59
+ <p class="lead" role="status" m-if="status == 'confirming'">
60
+ <span class="spin dark" aria-hidden="true"></span>Confirming your email…
61
+ </p>
62
+
63
+ <Template m-else-if="status == 'confirmed'">
64
+ <div class="notice" role="status">Your email is confirmed.</div>
65
+
66
+ <p class="switch">
67
+ <RouterLink to="/">Continue to Mana Starter</RouterLink>
68
+ </p>
69
+ </Template>
70
+
71
+ <div class="alert" role="alert" m-else-if="status == 'expired'">
72
+ This confirmation link has expired.
73
+ <RouterLink to="/">Back to Mana Starter</RouterLink> to send a new one.
74
+ </div>
75
+
76
+ <div class="alert" role="alert" m-else>This page needs the link from your confirmation email.</div>
77
+ </div>
78
+ </template>
@@ -0,0 +1,93 @@
1
+ // Signing in, registering, the guards and signing out, through the real pages and router against a faked API: the sign-in page asks the API and lands on the home page; a wrong password says so as a
2
+ // toast and stays; the register page checks what it can before asking, and shows the API's answer about a field on that field; a signed-out visit to a signed-in page goes to sign in; the
3
+ // confirmation banner shows for an unconfirmed email; forgot password says the same whether or not there's an account; signing out lands on the sign-in page.
4
+ import { check } from "./support/check.mjs";
5
+ import { settle } from "./support/settle.mjs";
6
+ import { useDom } from "./support/dom.mjs";
7
+ import { useFakeApi } from "./support/api.mjs";
8
+ import { apiUser } from "./support/users.mjs";
9
+
10
+ const w = useDom('<main id="app"></main>', { url: "http://app.test/login" });
11
+ const api = useFakeApi();
12
+ let signedIn = false;
13
+ let user = apiUser();
14
+
15
+ api.get("/api/user", () => signedIn ? { json: user } : { status: 401, json: { message: "Unauthenticated." } });
16
+ api.post("/api/login", body => body.password == "password" ? (signedIn = true, { json: user }) : { status: 422, json: { message: "Wrong email or password.", errors: { email: ["Wrong email or password."] } } });
17
+ api.post("/api/logout", () => (signedIn = false, { status: 204 }));
18
+ api.post("/api/register", body => body.email == "taken@example.test"
19
+ ? { status: 422, json: { message: "An account with this email already exists.", errors: { email: ["An account with this email already exists."] } } }
20
+ : (signedIn = true, user = apiUser({ name: body.name, email: body.email, verified: false }), { status: 201, json: user }));
21
+ api.post("/api/forgot-password", () => ({ status: 204 }));
22
+ api.post("/api/email/verification-notification", () => ({ status: 204 }));
23
+
24
+ const { createApp, createEvents, createStores } = await import("@mana-js/mana");
25
+ const { default: App } = await import("@/App.mana");
26
+ const { default: router } = await import("@/router");
27
+ const { useSession } = await import("@/stores/session");
28
+ const events = createEvents();
29
+ let session;
30
+ const Probe = { setup() { session = useSession(); return {}; }, template: `<i></i>` };
31
+
32
+ w.sessionStorage.clear();
33
+ createApp({ components: { App, Probe }, template: `<div><App /><Probe /></div>` }).use(createStores()).use(events).use(router).mount("#app");
34
+ events.on("signedOut", () => void router.replace({ name: "login" }));
35
+ await router.isReady();
36
+
37
+ const root = document.getElementById("app");
38
+ const $ = s => root.querySelector(s);
39
+ const text = s => $(s)?.textContent.replace(/\s+/g, " ").trim() ?? null;
40
+ const type = (id, value) => { const input = $(id); input.value = value; input.dispatchEvent(new w.Event("input")); };
41
+ const click = el => el.dispatchEvent(new w.MouseEvent("click", { bubbles: true, cancelable: true }));
42
+ const button = label => [...root.querySelectorAll("button")].find(b => b.textContent.trim() == label);
43
+ const go = async path => { await router.push(path); await settle(); };
44
+ const wait = async () => { await new Promise(resolve => setTimeout(resolve, 30)); await settle(); };
45
+
46
+ await settle();
47
+ check("signed out, the sign-in page: its heading, the way to register and to reset a password", w.location.pathname == "/login" && /^Sign in to /.test(text("h1")) && [...root.querySelectorAll("a")].some(a => a.getAttribute("href") == "/register") && [...root.querySelectorAll("a")].some(a => a.getAttribute("href") == "/forgot-password"));
48
+
49
+ await go("/");
50
+ check("a signed-in page, signed out: it goes to sign in", w.location.pathname == "/login");
51
+
52
+ click(button("Sign in")); await settle();
53
+ check("empty: it asks for both and sends nothing", text("#login-email-error") != null && api.calls.every(call => call.path != "/api/login"));
54
+
55
+ type("#login-email", "ama@example.test"); type("#login-password", "wrong");
56
+ click(button("Sign in")); await wait();
57
+ check("a wrong password: told so, and still on the sign-in page", w.location.pathname == "/login" && document.body.textContent.includes("Wrong email or password."));
58
+
59
+ type("#login-password", "password");
60
+ click(button("Sign in")); await wait();
61
+ check("the right one: signed in, on the home page, greeted by name", w.location.pathname == "/" && /Welcome, Ama Owusu/.test(text("h1")) && api.calls.some(call => call.path == "/api/login"));
62
+ check("a signed-in user has no use for the sign-in page: it goes home", await (async () => { await go("/login"); return w.location.pathname == "/"; })());
63
+
64
+ click(button("Sign out")); await wait();
65
+ check("signing out: the API's asked, the user's gone, the sign-in page shows", api.calls.some(call => call.path == "/api/logout") && session.user === null && w.location.pathname == "/login");
66
+
67
+ // Registering.
68
+ await go("/register");
69
+ check("the register page: a name, an email, a password and its confirmation", ["#register-name", "#register-email", "#register-password", "#register-confirm"].every(id => $(id)));
70
+ click(button("Create account")); await settle();
71
+ check("empty: each field says what's wrong, and nothing's sent", text("#register-name-error") != null && text("#register-password-error") != null && api.calls.every(call => call.path != "/api/register"));
72
+ type("#register-name", "Kofi Boateng"); type("#register-email", "taken@example.test"); type("#register-password", "longenough"); type("#register-confirm", "different");
73
+ click(button("Create account")); await settle();
74
+ check("a password that isn't confirmed: said, on its field, before asking the API", /don't match/.test(text("#register-confirm-error") ?? "") && api.calls.every(call => call.path != "/api/register"));
75
+ type("#register-confirm", "longenough");
76
+ click(button("Create account")); await wait();
77
+ check("an email that has an account: the API's words, on the email", text("#register-email-error") == "An account with this email already exists." && w.location.pathname == "/register");
78
+ type("#register-email", "kofi@example.test");
79
+ click(button("Create account")); await wait();
80
+ const registered = api.calls.find(call => call.path == "/api/register" && call.body.email == "kofi@example.test");
81
+ check("registered: sent as the API takes it, signed in, on the home page", registered?.body.password_confirmation == "longenough" && registered.body.name == "Kofi Boateng" && w.location.pathname == "/" && /Welcome, Kofi Boateng/.test(text("h1")));
82
+ check("an unconfirmed email: the banner asks to confirm, and Resend link asks the API", /Confirm your email/.test(text(".banner") ?? "") && (click(button("Resend link")), await settle(), api.calls.some(call => call.path == "/api/email/verification-notification")));
83
+
84
+ // Forgot password.
85
+ click(button("Sign out")); await wait();
86
+ await go("/forgot-password");
87
+ type("#forgot-email", "nobody@example.test");
88
+ click(button("Send link")); await wait();
89
+ check("forgot password: the API's asked, and the page says the same whatever the email is", api.calls.some(call => call.path == "/api/forgot-password" && call.body.email == "nobody@example.test") && /we.ve sent it a link/.test(document.body.textContent));
90
+
91
+ // A page that isn't one.
92
+ await go("/nowhere/at/all");
93
+ check("an address with no page: not found, with the way back", /Page not found/.test(text("h1")));
@@ -0,0 +1,38 @@
1
+ // The home page, signed in: it greets the user by name; and an address with no page, said so. Signed out, the home page is the sign-in page (tests/auth.test.mjs).
2
+ import { check } from "./support/check.mjs";
3
+ import { settle } from "./support/settle.mjs";
4
+ import { useDom } from "./support/dom.mjs";
5
+ import { useFakeApi } from "./support/api.mjs";
6
+ import { apiUser } from "./support/users.mjs";
7
+
8
+ useDom('<main id="app"></main>', { url: "http://app.test/" });
9
+
10
+ const api = useFakeApi();
11
+
12
+ api.get("/api/user", () => ({ json: apiUser() }));
13
+
14
+ const { createApp, createEvents, createStores } = await import("@mana-js/mana");
15
+ const { default: App } = await import("@/App.mana");
16
+ const { default: router } = await import("@/router");
17
+ const { useSession } = await import("@/stores/session");
18
+ let session;
19
+ const Probe = { setup() { session = useSession(); return {}; }, template: `<i></i>` };
20
+
21
+ createApp({ components: { App, Probe }, template: `<div><App /><Probe /></div>` }).use(createStores()).use(createEvents()).use(router).mount("#app");
22
+ await session.load();
23
+ await router.isReady();
24
+ await router.push("/");
25
+ await settle();
26
+
27
+ const root = document.getElementById("app");
28
+ const text = selector => root.querySelector(selector)?.textContent.replace(/\s+/g, " ").trim() ?? null;
29
+
30
+ check("the home page, signed in: it greets them by name, in the frame with their name and Sign out", text("h1") == "Welcome, Ama Owusu" && /Ama Owusu/.test(text(".topbar") ?? "") && /Sign out/.test(text(".topbar") ?? ""));
31
+
32
+ await router.push("/nowhere/at/all");
33
+ await settle();
34
+ check("an address with no page: not found, with the way back", text("h1") == "Page not found" && root.querySelector("a[href='/']") !== null);
35
+
36
+ await router.push("/");
37
+ await settle();
38
+ check("…and back at the home page", text("h1") == "Welcome, Ama Owusu");