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.
- package/LICENSE +21 -0
- package/README.md +28 -2
- package/bin/create-mana.mjs +5 -0
- package/kits/auth/files/src/api/auth.ts +100 -0
- package/kits/auth/files/src/api/profile.ts +48 -0
- package/kits/auth/files/src/api/types.ts +28 -0
- package/kits/auth/files/src/components/AppLayout.mana +31 -0
- package/kits/auth/files/src/components/VerifyBanner.mana +47 -0
- package/kits/auth/files/src/router/routes.ts +31 -0
- package/kits/auth/files/src/shared/events.ts +11 -0
- package/kits/auth/files/src/stores/session.ts +99 -0
- package/kits/auth/files/src/views/ForgotPassword.mana +83 -0
- package/kits/auth/files/src/views/Home.mana +15 -0
- package/kits/auth/files/src/views/Profile.mana +172 -0
- package/kits/auth/files/src/views/Register.mana +86 -0
- package/kits/auth/files/src/views/ResetPassword.mana +90 -0
- package/kits/auth/files/src/views/SignIn.mana +84 -0
- package/kits/auth/files/src/views/VerifyEmail.mana +78 -0
- package/kits/auth/files/tests/auth.test.mjs +93 -0
- package/kits/auth/files/tests/home.test.mjs +38 -0
- package/kits/auth/files/tests/profile.test.mjs +68 -0
- package/kits/auth/files/tests/support/users.mjs +2 -0
- package/kits/auth/kit.json +80 -0
- package/kits/base/files/README.md +39 -0
- package/kits/base/kit.json +7 -0
- package/kits/organisations/files/src/api/auth.ts +124 -0
- package/kits/organisations/files/src/api/members.ts +243 -0
- package/kits/organisations/files/src/api/notifications.ts +63 -0
- package/kits/organisations/files/src/api/organisations.ts +41 -0
- package/kits/organisations/files/src/api/scope.ts +98 -0
- package/kits/organisations/files/src/api/types.ts +72 -0
- package/kits/organisations/files/src/assets/styles/organisations.css +46 -0
- package/kits/organisations/files/src/components/AppLayout.mana +90 -0
- package/kits/organisations/files/src/components/NotificationBell.mana +35 -0
- package/kits/organisations/files/src/components/NotificationList.mana +112 -0
- package/kits/organisations/files/src/components/OrganisationSwitcher.mana +171 -0
- package/kits/organisations/files/src/composables/notifications.ts +28 -0
- package/kits/organisations/files/src/router/routes.ts +43 -0
- package/kits/organisations/files/src/shared/roles.ts +10 -0
- package/kits/organisations/files/src/stores/notifications.ts +41 -0
- package/kits/organisations/files/src/stores/session.ts +158 -0
- package/kits/organisations/files/src/utilities/format.ts +15 -0
- package/kits/organisations/files/src/views/Forbidden.mana +28 -0
- package/kits/organisations/files/src/views/Home.mana +15 -0
- package/kits/organisations/files/src/views/Invite.mana +156 -0
- package/kits/organisations/files/src/views/InviteMember.mana +99 -0
- package/kits/organisations/files/src/views/Members.mana +218 -0
- package/kits/organisations/files/src/views/NoOrganisation.mana +184 -0
- package/kits/organisations/files/src/views/Notifications.mana +30 -0
- package/kits/organisations/files/src/views/OrganisationForm.mana +76 -0
- package/kits/organisations/files/src/views/OrganisationSettings.mana +62 -0
- package/kits/organisations/files/src/views/Profile.mana +213 -0
- package/kits/organisations/files/src/views/Register.mana +113 -0
- package/kits/organisations/files/tests/auth.test.mjs +82 -0
- package/kits/organisations/files/tests/home.test.mjs +43 -0
- package/kits/organisations/files/tests/members.test.mjs +89 -0
- package/kits/organisations/files/tests/notifications.test.mjs +49 -0
- package/kits/organisations/files/tests/organisations.test.mjs +70 -0
- package/kits/organisations/files/tests/profile.test.mjs +79 -0
- package/kits/organisations/files/tests/support/users.mjs +3 -0
- package/kits/organisations/kit.json +51 -0
- package/lib/compose.mjs +209 -0
- package/lib/new.mjs +161 -0
- package/lib/prompts.mjs +182 -0
- package/package.json +36 -4
- package/template/.nvmrc +1 -0
- package/template/README.md +23 -0
- package/template/_gitignore +25 -0
- package/template/index.html +17 -0
- package/template/package.json +22 -0
- package/template/public/favicon.svg +1 -0
- package/template/public/icons.svg +24 -0
- package/template/src/App.mana +15 -0
- package/template/src/api/ApiError.ts +17 -0
- package/template/src/api/client.ts +116 -0
- package/template/src/assets/styles/main.css +158 -0
- package/template/src/components/AppLogo.mana +20 -0
- package/template/src/components/Button.mana +25 -0
- package/template/src/components/ConfirmHost.mana +43 -0
- package/template/src/components/TextField.mana +60 -0
- package/template/src/components/ToastHost.mana +40 -0
- package/template/src/composables/media.ts +19 -0
- package/template/src/composables/unsaved.ts +29 -0
- package/template/src/main.ts +25 -0
- package/template/src/router/index.ts +14 -0
- package/template/src/router/routes.ts +11 -0
- package/template/src/stores/confirm.ts +45 -0
- package/template/src/stores/toasts.ts +64 -0
- package/template/src/utilities/query.ts +40 -0
- package/template/src/utilities/timers.ts +12 -0
- package/template/src/views/Home.mana +12 -0
- package/template/src/views/NotFound.mana +13 -0
- package/template/tests/home.test.mjs +29 -0
- package/template/tests/support/api.mjs +44 -0
- package/template/tests/support/check.mjs +15 -0
- package/template/tests/support/dom.mjs +45 -0
- package/template/tests/support/paths.mjs +54 -0
- package/template/tests/support/settle.mjs +15 -0
- package/template/tsconfig.json +35 -0
- package/template/vite.config.ts +29 -0
- package/versions.json +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Innova GHX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# create-mana
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
```sh
|
|
4
|
+
npm create mana@latest school-web
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Or `npx create-mana school-web`. It asks for a **starter kit** — None (Vite, the router, a home page), Auth (+ sign in, register, forgot and reset password, email confirmation, a profile) or Organisations (+ workspaces: create,
|
|
8
|
+
switch, members, roles, invitations, notifications) — then makes the folder and, if you say so, runs `npm install`. The pages are for the API the [Coven installer](https://github.com/innova-ghx/coven-project) makes, at `/api`.
|
|
9
|
+
|
|
10
|
+
Everything it asks can be said on the command line, so it runs with no one there:
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm create mana@latest school-web -- --kit=organisations --no-install -y
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
| | |
|
|
17
|
+
|---|---|
|
|
18
|
+
| `--name="My School"` | the app's name as people see it (the folder's, unless said) |
|
|
19
|
+
| `--kit=none\|auth\|organisations` | the starter kit |
|
|
20
|
+
| `--no-install` | don't run npm install |
|
|
21
|
+
| `--yes`, `-y` | ask nothing: the Auth kit, unless said |
|
|
22
|
+
| `--force` | make it in a folder that has things in it |
|
|
23
|
+
|
|
24
|
+
## How it's made
|
|
25
|
+
|
|
26
|
+
This folder is part of the starter's own repository: the repository *is* the project a kit is laid over, so it builds and is tested as it stands. `kits/<id>/` is each kit's `kit.json` (what it needs, the lines it adds at the
|
|
27
|
+
markers — `@mana:imports`, `@mana:routes` — in the files it patches) and its `files/` (copied over the project, replacing what's there). `lib/compose.mjs` lays them over; `lib/verify.mjs` makes one kit's project and
|
|
28
|
+
installs, builds and tests it. `npm pack` copies the starter and the kits beside the installer (`scripts/prepack.mjs`), so what's published is complete; `MANA_VERSION` says which Mana the starter asks for when the
|
|
29
|
+
framework isn't beside it.
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Signing in, and its kin, from the API (the Coven starter's routes under /api) — its calls:
|
|
3
|
+
* signIn the user, signed in (422 on `email` for wrong credentials, or too many tries)
|
|
4
|
+
* register the user, signed in; a link to confirm their email is mailed
|
|
5
|
+
* signOut the session ended
|
|
6
|
+
* me the signed-in user, or null for no one
|
|
7
|
+
* requestReset asks for a password reset link; the API says the same whether or not the email has an account (204)
|
|
8
|
+
* resetPassword a new password, from the reset link's token and email
|
|
9
|
+
* verifyEmail confirms an email from its signed link
|
|
10
|
+
* resendVerification
|
|
11
|
+
* mails the signed-in user their link again
|
|
12
|
+
* The API answers in its own words (`pending_email`): userOf() makes that the app's User, and the fields' errors are given the form's names (`password_confirmation` is `confirm`).
|
|
13
|
+
*/
|
|
14
|
+
import ApiError from "@/api/ApiError";
|
|
15
|
+
import { request } from "@/api/client";
|
|
16
|
+
import type { Registration, User } from "@/api/types";
|
|
17
|
+
|
|
18
|
+
// What a confirmation link carries, which the API signed.
|
|
19
|
+
export interface VerificationLink {
|
|
20
|
+
id: string;
|
|
21
|
+
hash: string;
|
|
22
|
+
expires: string;
|
|
23
|
+
signature: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function signIn(email: string, password: string): Promise<User> {
|
|
27
|
+
return userOf(await call(() => request("POST", "/login", { email, password })));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function register(registration: Registration): Promise<User> {
|
|
31
|
+
return userOf(await call(() => request("POST", "/register", {
|
|
32
|
+
name: registration.name,
|
|
33
|
+
email: registration.email,
|
|
34
|
+
password: registration.password,
|
|
35
|
+
password_confirmation: registration.passwordConfirmation,
|
|
36
|
+
})));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function signOut(): Promise<void> {
|
|
40
|
+
await request("POST", "/logout");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// The signed-in user, or null when the API says no one is (401).
|
|
44
|
+
export async function me(): Promise<User | null> {
|
|
45
|
+
try {
|
|
46
|
+
return userOf(await request("GET", "/user"));
|
|
47
|
+
} catch(error) {
|
|
48
|
+
if(error instanceof ApiError && error.status == 401)
|
|
49
|
+
return null;
|
|
50
|
+
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function requestReset(email: string): Promise<void> {
|
|
56
|
+
await request("POST", "/forgot-password", { email });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function resetPassword(reset: { token: string; email: string; password: string; confirmation: string }): Promise<void> {
|
|
60
|
+
const { token, email, password, confirmation } = reset;
|
|
61
|
+
|
|
62
|
+
await call(() => request("POST", "/reset-password", { token, email, password, password_confirmation: confirmation }));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function verifyEmail(link: VerificationLink): Promise<void> {
|
|
66
|
+
const query = new URLSearchParams({ expires: link.expires, signature: link.signature });
|
|
67
|
+
|
|
68
|
+
await request("POST", `/verify-email/${encodeURIComponent(link.id)}/${encodeURIComponent(link.hash)}?${query}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function resendVerification(): Promise<void> {
|
|
72
|
+
await request("POST", "/email/verification-notification");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Runs a request, its fields' errors given the form's names.
|
|
76
|
+
async function call(run: () => Promise<unknown>): Promise<unknown> {
|
|
77
|
+
try {
|
|
78
|
+
return await run();
|
|
79
|
+
} catch(error) {
|
|
80
|
+
if(error instanceof ApiError && error.field == "password_confirmation")
|
|
81
|
+
throw new ApiError(error.message, "confirm", error.status);
|
|
82
|
+
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// The API's user as the app's User; an answer that isn't one is an ApiError.
|
|
88
|
+
export function userOf(data: unknown): User {
|
|
89
|
+
const read = (name: string): unknown => typeof data == "object" && data !== null ? Reflect.get(data, name) : undefined;
|
|
90
|
+
const id = read("id");
|
|
91
|
+
const name = read("name");
|
|
92
|
+
const email = read("email");
|
|
93
|
+
const verified = read("verified");
|
|
94
|
+
const pending = read("pending_email");
|
|
95
|
+
|
|
96
|
+
if(typeof id != "number" || typeof name != "string" || typeof email != "string" || typeof verified != "boolean" || (pending !== null && pending !== undefined && typeof pending != "string"))
|
|
97
|
+
throw new ApiError("The API answered with a user this app can't read.");
|
|
98
|
+
|
|
99
|
+
return { id, name, email, verified, pendingEmail: pending ?? null };
|
|
100
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Profile, from the API (coven-project's profile endpoints):
|
|
3
|
+
* its calls.
|
|
4
|
+
* updateProfile the name, and an email, which only becomes the user's
|
|
5
|
+
* once its link is opened: until then the old one
|
|
6
|
+
* signs in and `pendingEmail` says what's waiting. The
|
|
7
|
+
* user as the API has them (422 on `email` for one
|
|
8
|
+
* another account has)
|
|
9
|
+
* resendEmailChange the new address's link again (409 with none waiting)
|
|
10
|
+
* cancelEmailChange no change after all: the user
|
|
11
|
+
* changePassword the current password proved and a new one set (422 on
|
|
12
|
+
* `current`); the user's other sessions are signed out
|
|
13
|
+
* The API names the current password `current_password`, and wants the new
|
|
14
|
+
* one confirmed, which the page has done: it's sent again as its own
|
|
15
|
+
* confirmation.
|
|
16
|
+
*/
|
|
17
|
+
import { userOf } from "@/api/auth";
|
|
18
|
+
import ApiError from "@/api/ApiError";
|
|
19
|
+
import { request } from "@/api/client";
|
|
20
|
+
import type { PasswordInput, ProfileInput, User } from "@/api/types";
|
|
21
|
+
|
|
22
|
+
export async function updateProfile(input: ProfileInput): Promise<User> {
|
|
23
|
+
return userOf(await request("PATCH", "/user", { name: input.name, email: input.email }));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function resendEmailChange(): Promise<void> {
|
|
27
|
+
await request("POST", "/user/email/resend");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function cancelEmailChange(): Promise<User> {
|
|
31
|
+
return userOf(await request("DELETE", "/user/email/pending"));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function changePassword(input: PasswordInput): Promise<void> {
|
|
35
|
+
try {
|
|
36
|
+
await request("PUT", "/user/password", {
|
|
37
|
+
current_password: input.current,
|
|
38
|
+
password: input.password,
|
|
39
|
+
password_confirmation: input.password,
|
|
40
|
+
});
|
|
41
|
+
} catch(error) {
|
|
42
|
+
// Its fields are given the app's names.
|
|
43
|
+
if(error instanceof ApiError && error.field == "current_password")
|
|
44
|
+
throw new ApiError(error.message, "current", error.status);
|
|
45
|
+
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// What the API's users look like to the app: its UserResource, in the app's own spelling.
|
|
2
|
+
export interface User {
|
|
3
|
+
id: number;
|
|
4
|
+
name: string;
|
|
5
|
+
email: string;
|
|
6
|
+
verified: boolean;
|
|
7
|
+
// The new email waiting for its link to be opened, if there is one.
|
|
8
|
+
pendingEmail: string | null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// What the register form sends.
|
|
12
|
+
export interface Registration {
|
|
13
|
+
name: string;
|
|
14
|
+
email: string;
|
|
15
|
+
password: string;
|
|
16
|
+
passwordConfirmation: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// What the profile's forms send.
|
|
20
|
+
export interface ProfileInput {
|
|
21
|
+
name: string;
|
|
22
|
+
email: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PasswordInput {
|
|
26
|
+
current: string;
|
|
27
|
+
password: string;
|
|
28
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/*
|
|
3
|
+
* The frame of every signed-in page: the top bar (the app's name, Profile, the user's name, Sign out), the email confirmation banner when it applies, then the page itself, shown through a nested
|
|
4
|
+
* <RouterView>: the signed-in pages are this route's children (src/router/routes.ts), so the frame stays while they change.
|
|
5
|
+
*/
|
|
6
|
+
import VerifyBanner from "@/components/VerifyBanner.mana";
|
|
7
|
+
import { useSession } from "@/stores/session";
|
|
8
|
+
|
|
9
|
+
const sessionStore = useSession();
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<template>
|
|
13
|
+
<header class="topbar">
|
|
14
|
+
<RouterLink to="/" class="brand">Mana Starter</RouterLink>
|
|
15
|
+
<!-- @mana:nav -->
|
|
16
|
+
<div class="spacer"></div>
|
|
17
|
+
<RouterLink to="/profile" class="user">{{ sessionStore.user?.name }}</RouterLink>
|
|
18
|
+
<button type="button" class="btn small" @click="sessionStore.signOut()">Sign out</button>
|
|
19
|
+
</header>
|
|
20
|
+
|
|
21
|
+
<VerifyBanner />
|
|
22
|
+
|
|
23
|
+
<RouterView />
|
|
24
|
+
</template>
|
|
25
|
+
|
|
26
|
+
<style scoped>
|
|
27
|
+
.topbar { display: flex; align-items: center; gap: 12px; padding: 10px 20px; background: var(--paper); border-bottom: 1.5px solid var(--line); }
|
|
28
|
+
.brand { font-weight: 700; color: var(--ink); text-decoration: none; }
|
|
29
|
+
.user { color: var(--ink); text-decoration: none; }
|
|
30
|
+
.user:hover { text-decoration: underline; }
|
|
31
|
+
</style>
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/*
|
|
3
|
+
* The banner asking a signed-in user to confirm their email, shown by
|
|
4
|
+
* AppLayout under the top bar while the session store says so. Resend link
|
|
5
|
+
* sends the email again (once per banner: it then says it's sent); × hides
|
|
6
|
+
* the banner until the next sign-in.
|
|
7
|
+
*/
|
|
8
|
+
import { ref } from "@mana-js/mana";
|
|
9
|
+
|
|
10
|
+
import { useSession } from "@/stores/session";
|
|
11
|
+
import { useToasts } from "@/stores/toasts";
|
|
12
|
+
|
|
13
|
+
const sessionStore = useSession();
|
|
14
|
+
const toastStore = useToasts();
|
|
15
|
+
|
|
16
|
+
const sending = ref(false);
|
|
17
|
+
const sent = ref(false);
|
|
18
|
+
|
|
19
|
+
async function resend() {
|
|
20
|
+
sending.value = true;
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
await sessionStore.resendVerification();
|
|
24
|
+
sent.value = true;
|
|
25
|
+
} catch {
|
|
26
|
+
toastStore.error("Couldn't resend the link", "Something went wrong. Try again.");
|
|
27
|
+
} finally {
|
|
28
|
+
sending.value = false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
<template>
|
|
34
|
+
<div class="banner" role="status" m-if="sessionStore.showVerifyBanner">
|
|
35
|
+
<p>Confirm your email. We sent a link to <strong>{{ sessionStore.user?.email }}</strong>.</p>
|
|
36
|
+
|
|
37
|
+
<div class="banner-actions">
|
|
38
|
+
<span class="muted" m-if="sent">Sent. Check your inbox.</span>
|
|
39
|
+
<button type="button" class="btn small" :disabled="sending || sent" @click="resend">Resend link</button>
|
|
40
|
+
<button type="button" class="close" aria-label="Hide until next sign-in" @click="sessionStore.hideBanner()">×</button>
|
|
41
|
+
</div>
|
|
42
|
+
</div>
|
|
43
|
+
</template>
|
|
44
|
+
|
|
45
|
+
<style scoped>
|
|
46
|
+
.banner-actions { display: flex; align-items: center; gap: 8px; }
|
|
47
|
+
</style>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The pages, with Laravel-style routes. Each has a name, which links and redirects use instead of its path (`router.url("login")`, `{ name: "dashboard" }`), following Laravel's own names for the auth pages.
|
|
3
|
+
* Who may see a page is its middleware, registered in ./index.ts:
|
|
4
|
+
* auth only a signed-in user (anyone else goes to the sign-in page, with the way back in ?redirect)
|
|
5
|
+
* guest only someone signed out (a signed-in user goes to the home page)
|
|
6
|
+
* Pages with neither (confirming an email) are open to anyone. The signed-in pages are children of AppLayout: it stays while they change, shows them through its nested <RouterView>, and its
|
|
7
|
+
* middleware runs for them all. Each page is loaded when it's first visited, so the first download stays small; AppLayout, which frames every signed-in page, comes with the app. The last route is
|
|
8
|
+
* the page for an address with no page.
|
|
9
|
+
*/
|
|
10
|
+
import AppLayoutComponent from "@/components/AppLayout.mana";
|
|
11
|
+
import SignInView from "@/views/SignIn.mana";
|
|
12
|
+
|
|
13
|
+
export default [
|
|
14
|
+
{
|
|
15
|
+
path: "/",
|
|
16
|
+
component: AppLayoutComponent,
|
|
17
|
+
middleware: ["auth"],
|
|
18
|
+
children: [
|
|
19
|
+
{ path: "", name: "dashboard", component: () => import("@/views/Home.mana") },
|
|
20
|
+
{ path: "profile", name: "profile", component: () => import("@/views/Profile.mana") },
|
|
21
|
+
// @mana:routes-auth
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
{ path: "/login", name: "login", component: SignInView, middleware: "guest" },
|
|
25
|
+
{ path: "/register", name: "register", component: () => import("@/views/Register.mana"), middleware: "guest" },
|
|
26
|
+
{ path: "/forgot-password", name: "password.request", component: () => import("@/views/ForgotPassword.mana"), middleware: "guest" },
|
|
27
|
+
{ path: "/reset-password", name: "password.reset", component: () => import("@/views/ResetPassword.mana"), middleware: "guest" },
|
|
28
|
+
{ path: "/verify-email", name: "verification.verify", component: () => import("@/views/VerifyEmail.mana") },
|
|
29
|
+
// @mana:routes
|
|
30
|
+
{ path: "/{path}", name: "not-found", where: { path: ".*" }, component: () => import("@/views/NotFound.mana") },
|
|
31
|
+
];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The app's events, sent by name on the app's event bus (installed in main.ts; useEvents() in setup or a store) and typed here:
|
|
3
|
+
* signedOut the session ended (main.ts sends the user to /login)
|
|
4
|
+
*/
|
|
5
|
+
import type { User } from "@/api/types";
|
|
6
|
+
|
|
7
|
+
declare module "@mana-js/mana" {
|
|
8
|
+
interface ManaEvents {
|
|
9
|
+
signedOut: [user: User];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The signed-in user, as a store: the router's guard and any page read it with useSession().
|
|
3
|
+
*
|
|
4
|
+
* Who's signed in is the API's: load() asks it, as the app starts (main.ts awaits it before the router's first page), and only its actions — signIn(), register(), signOut(), load() — change who's
|
|
5
|
+
* signed in: `_user` is private (a leading `_` hides it from useSession()), and the store hands out `user` as a computed value, which can't be set. While someone's signed in, what the API answers
|
|
6
|
+
* after a change (their profile saved) replaces the user: receive(). Signing out announces "signedOut" on the app's event bus.
|
|
7
|
+
*
|
|
8
|
+
* An unconfirmed email restricts nothing: `showVerifyBanner` says when the layout shows the banner asking to confirm, hideBanner() hides it until the next sign-in, and verify() confirms the email
|
|
9
|
+
* from the link.
|
|
10
|
+
*
|
|
11
|
+
* `_user` and `_bannerHidden` are kept in sessionStorage, so a page shows who's signed in before load() has its answer, which replaces it.
|
|
12
|
+
*/
|
|
13
|
+
import { computed, defineStore, ref, useEvents } from "@mana-js/mana";
|
|
14
|
+
|
|
15
|
+
import * as auth from "@/api/auth";
|
|
16
|
+
import type { VerificationLink } from "@/api/auth";
|
|
17
|
+
import type { Registration, User } from "@/api/types";
|
|
18
|
+
// @mana:imports
|
|
19
|
+
|
|
20
|
+
export type { User };
|
|
21
|
+
|
|
22
|
+
export const useSession = defineStore("session", () => {
|
|
23
|
+
const events = useEvents();
|
|
24
|
+
const _user = ref<User | null>(null);
|
|
25
|
+
const _bannerHidden = ref(false);
|
|
26
|
+
|
|
27
|
+
const user = computed(() => _user.value);
|
|
28
|
+
const signedIn = computed(() => _user.value != null);
|
|
29
|
+
const showVerifyBanner = computed(() => _user.value != null && !_user.value.verified && !_bannerHidden.value);
|
|
30
|
+
// @mana:computed
|
|
31
|
+
|
|
32
|
+
// Asks the API who's signed in: as the app starts.
|
|
33
|
+
async function load() {
|
|
34
|
+
_user.value = await auth.me();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function signIn(email: string, password: string) {
|
|
38
|
+
_user.value = await auth.signIn(email, password);
|
|
39
|
+
_bannerHidden.value = false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function register(registration: Registration) {
|
|
43
|
+
_user.value = await auth.register(registration);
|
|
44
|
+
_bannerHidden.value = false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Confirms an email from its link. Anyone can follow it; if someone's signed in, they're read again, so the banner goes for good.
|
|
48
|
+
async function verify(link: VerificationLink) {
|
|
49
|
+
await auth.verifyEmail(link);
|
|
50
|
+
|
|
51
|
+
if(_user.value)
|
|
52
|
+
_user.value = await auth.me();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function resendVerification() {
|
|
56
|
+
if(_user.value)
|
|
57
|
+
await auth.resendVerification();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// What the API answered after a change, while someone's signed in: their profile saved.
|
|
61
|
+
function receive(next: User) {
|
|
62
|
+
_user.value = next;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function hideBanner() {
|
|
66
|
+
_bannerHidden.value = true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Signs out here at once and announces it, then ends the session with the API — signed out whatever it answers, as the session may have ended already.
|
|
70
|
+
async function signOut() {
|
|
71
|
+
const user = _user.value;
|
|
72
|
+
|
|
73
|
+
if(!user)
|
|
74
|
+
return;
|
|
75
|
+
|
|
76
|
+
_user.value = null;
|
|
77
|
+
events.emit("signedOut", user);
|
|
78
|
+
await auth.signOut().catch(() => {});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
_user,
|
|
83
|
+
_bannerHidden,
|
|
84
|
+
user,
|
|
85
|
+
signedIn,
|
|
86
|
+
showVerifyBanner,
|
|
87
|
+
// @mana:store
|
|
88
|
+
load,
|
|
89
|
+
signIn,
|
|
90
|
+
register,
|
|
91
|
+
receive,
|
|
92
|
+
verify,
|
|
93
|
+
resendVerification,
|
|
94
|
+
hideBanner,
|
|
95
|
+
signOut,
|
|
96
|
+
};
|
|
97
|
+
}, {
|
|
98
|
+
persist: { storage: "session", pick: ["_user", "_bannerHidden"] },
|
|
99
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/*
|
|
3
|
+
* The forgot-password page, at /forgot-password: asks for an email and
|
|
4
|
+
* says a link is on its way. It says the same whether or not the email has
|
|
5
|
+
* an account, so the page can't be used to find out who has one. The link
|
|
6
|
+
* leads to /reset-password. It's the first page on the app's real API
|
|
7
|
+
* (src/api/auth.ts): an email the API turns down shows under the field.
|
|
8
|
+
*
|
|
9
|
+
* Components used: AppLogo, TextField, Button.
|
|
10
|
+
*/
|
|
11
|
+
import { ref } from "@mana-js/mana";
|
|
12
|
+
|
|
13
|
+
import ApiError from "@/api/ApiError";
|
|
14
|
+
import { requestReset } 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 toastStore = useToasts();
|
|
21
|
+
|
|
22
|
+
const email = ref("");
|
|
23
|
+
const error = ref<string | null>(null);
|
|
24
|
+
const sentTo = ref<string | null>(null);
|
|
25
|
+
const submitting = ref(false);
|
|
26
|
+
|
|
27
|
+
async function submit() {
|
|
28
|
+
error.value = email.value ? null : "Enter your email.";
|
|
29
|
+
|
|
30
|
+
if(error.value)
|
|
31
|
+
return;
|
|
32
|
+
|
|
33
|
+
submitting.value = true;
|
|
34
|
+
toastStore.clear();
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
await requestReset(email.value);
|
|
38
|
+
sentTo.value = email.value;
|
|
39
|
+
} catch(cause) {
|
|
40
|
+
if(cause instanceof ApiError && cause.field == "email")
|
|
41
|
+
error.value = cause.message;
|
|
42
|
+
else
|
|
43
|
+
toastStore.error("Couldn't send the link", "Something went wrong. Try again.");
|
|
44
|
+
} finally {
|
|
45
|
+
submitting.value = false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
</script>
|
|
49
|
+
|
|
50
|
+
<template>
|
|
51
|
+
<div class="auth">
|
|
52
|
+
<AppLogo />
|
|
53
|
+
|
|
54
|
+
<h1>Reset your password</h1>
|
|
55
|
+
|
|
56
|
+
<Template m-if="sentTo">
|
|
57
|
+
<div class="notice" role="status">
|
|
58
|
+
If an account exists for <strong>{{ sentTo }}</strong>, we've sent it a link to set a new password.
|
|
59
|
+
</div>
|
|
60
|
+
|
|
61
|
+
<p class="switch">
|
|
62
|
+
<RouterLink to="/login">Back to sign in</RouterLink>
|
|
63
|
+
</p>
|
|
64
|
+
</Template>
|
|
65
|
+
|
|
66
|
+
<Template m-else>
|
|
67
|
+
<p class="lead">Enter your account's email and we'll send you a link to set a new password.</p>
|
|
68
|
+
|
|
69
|
+
<form novalidate :class="{ 'is-submitting': submitting }" :aria-busy="submitting" @submit.prevent="submit">
|
|
70
|
+
<TextField id="forgot-email" name="email" label="Email" type="email" autocomplete="email" placeholder="name@company.com" :error="error" m-model="email" />
|
|
71
|
+
|
|
72
|
+
<div class="submit">
|
|
73
|
+
<Button type="submit" :label="submitting ? 'Sending…' : 'Send link'" :disabled="submitting" />
|
|
74
|
+
</div>
|
|
75
|
+
</form>
|
|
76
|
+
|
|
77
|
+
<p class="switch">
|
|
78
|
+
Remembered it?
|
|
79
|
+
<RouterLink to="/login">Sign in</RouterLink>
|
|
80
|
+
</p>
|
|
81
|
+
</Template>
|
|
82
|
+
</div>
|
|
83
|
+
</template>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/*
|
|
3
|
+
* The home page of the signed-in app: a greeting, to replace with your own first page.
|
|
4
|
+
*/
|
|
5
|
+
import { useSession } from "@/stores/session";
|
|
6
|
+
|
|
7
|
+
const sessionStore = useSession();
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
<template>
|
|
11
|
+
<main class="page">
|
|
12
|
+
<h1>Welcome, {{ sessionStore.user?.name }}</h1>
|
|
13
|
+
<p class="muted">You're signed in as {{ sessionStore.user?.email }}. This is the page to start from: <code>src/views/Home.mana</code>.</p>
|
|
14
|
+
</main>
|
|
15
|
+
</template>
|