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
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Keeps the unread count of the signed-in user's notifications current while a frame shows: asked when it appears, then every minute, and whenever the browser tab comes back to the front — someone who
|
|
3
|
+
* was away may have been told a good deal. The timer doesn't keep a Node process alive (a test), and nothing's left running once the frame is gone.
|
|
4
|
+
*/
|
|
5
|
+
import { onMounted, onUnmounted } from "@mana-js/mana";
|
|
6
|
+
|
|
7
|
+
import { useNotifications } from "@/stores/notifications";
|
|
8
|
+
import { unreffed } from "@/utilities/timers";
|
|
9
|
+
|
|
10
|
+
export function useNotificationCount() {
|
|
11
|
+
const notificationsStore = useNotifications();
|
|
12
|
+
const ask = () => void notificationsStore.refresh();
|
|
13
|
+
const visible = () => {
|
|
14
|
+
if(!document.hidden)
|
|
15
|
+
ask();
|
|
16
|
+
};
|
|
17
|
+
const poll = unreffed(setInterval(ask, 60_000));
|
|
18
|
+
|
|
19
|
+
onMounted(() => {
|
|
20
|
+
ask();
|
|
21
|
+
document.addEventListener("visibilitychange", visible);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
onUnmounted(() => {
|
|
25
|
+
clearInterval(poll);
|
|
26
|
+
document.removeEventListener("visibilitychange", visible);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
|
3
|
+
* pages. 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
|
+
* organisation
|
|
6
|
+
* only someone who works in an organisation: anyone in none goes to the page for it, /no-organisation
|
|
7
|
+
* without-organisation
|
|
8
|
+
* only someone in none (that page): anyone else goes to the home page
|
|
9
|
+
* guest only someone signed out (a signed-in user goes to the home page)
|
|
10
|
+
* role only the roles it lists: `role:owner,admin`; anyone else is told they don't have access
|
|
11
|
+
* Pages with none of them (confirming an email, an invitation's link) are open to anyone. The signed-in pages are children of AppLayout: it stays while they change, shows them through its nested
|
|
12
|
+
* <RouterView>, and its middleware runs for them all. Each page is loaded when it's first visited, so the first download stays small. The last route is the page for an address with no page.
|
|
13
|
+
*/
|
|
14
|
+
import AppLayoutComponent from "@/components/AppLayout.mana";
|
|
15
|
+
import SignInView from "@/views/SignIn.mana";
|
|
16
|
+
|
|
17
|
+
export default [
|
|
18
|
+
{
|
|
19
|
+
path: "/",
|
|
20
|
+
component: AppLayoutComponent,
|
|
21
|
+
middleware: ["auth", "organisation"],
|
|
22
|
+
children: [
|
|
23
|
+
{ path: "", name: "dashboard", component: () => import("@/views/Home.mana") },
|
|
24
|
+
{ path: "profile", name: "profile", component: () => import("@/views/Profile.mana") },
|
|
25
|
+
{ path: "notifications", name: "notifications", component: () => import("@/views/Notifications.mana") },
|
|
26
|
+
{ path: "organisations/new", name: "organisations.create", component: () => import("@/views/OrganisationForm.mana") },
|
|
27
|
+
{ path: "members", name: "members", component: () => import("@/views/Members.mana"), middleware: "role:owner,admin" },
|
|
28
|
+
{ path: "members/invite", name: "members.invite", component: () => import("@/views/InviteMember.mana"), middleware: "role:owner,admin" },
|
|
29
|
+
{ path: "settings", name: "settings", component: () => import("@/views/OrganisationSettings.mana"), middleware: "role:owner,admin" },
|
|
30
|
+
{ path: "forbidden", name: "forbidden", component: () => import("@/views/Forbidden.mana") },
|
|
31
|
+
// @mana:routes-auth
|
|
32
|
+
],
|
|
33
|
+
},
|
|
34
|
+
{ path: "/no-organisation", name: "organisations.none", component: () => import("@/views/NoOrganisation.mana"), middleware: ["auth", "without-organisation"] },
|
|
35
|
+
{ path: "/login", name: "login", component: SignInView, middleware: "guest" },
|
|
36
|
+
{ path: "/register", name: "register", component: () => import("@/views/Register.mana"), middleware: "guest" },
|
|
37
|
+
{ path: "/forgot-password", name: "password.request", component: () => import("@/views/ForgotPassword.mana"), middleware: "guest" },
|
|
38
|
+
{ path: "/reset-password", name: "password.reset", component: () => import("@/views/ResetPassword.mana"), middleware: "guest" },
|
|
39
|
+
{ path: "/verify-email", name: "verification.verify", component: () => import("@/views/VerifyEmail.mana") },
|
|
40
|
+
{ path: "/invite", name: "invite", component: () => import("@/views/Invite.mana") },
|
|
41
|
+
// @mana:routes
|
|
42
|
+
{ path: "/{path}", name: "not-found", where: { path: ".*" }, component: () => import("@/views/NotFound.mana") },
|
|
43
|
+
];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The roles a member of an organisation can have, with how the app describes them: on the organisation switcher, on Members and when inviting someone.
|
|
3
|
+
*/
|
|
4
|
+
import type { Role } from "@/api/types";
|
|
5
|
+
|
|
6
|
+
export const ROLES: Readonly<Record<Role, { label: string; description: string }>> = {
|
|
7
|
+
owner: { label: "Owner", description: "Everything, and the only one who can transfer ownership." },
|
|
8
|
+
admin: { label: "Admin", description: "Everything but ownership: settings and members." },
|
|
9
|
+
member: { label: "Member", description: "Works in the organisation. No settings or members." },
|
|
10
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* How many notifications the signed-in user hasn't read, and has in all, for the bell: asked of the API when the frame of the signed-in
|
|
3
|
+
* pages shows, when the user goes to another page, and when the API pushes a notification (H38, `arrive()`) — or, with no push, every minute and
|
|
4
|
+
* when the tab comes back to the front. A failure is quiet: the bell keeps the last count, and the page of notifications says if it can't
|
|
5
|
+
* load. Reading and deleting from the page tell it, so the count doesn't wait for the next ask. `arrived` counts the pushes, for a list
|
|
6
|
+
* that's showing to read itself again.
|
|
7
|
+
*/
|
|
8
|
+
import { defineStore, ref } from "@mana-js/mana";
|
|
9
|
+
|
|
10
|
+
import * as api from "@/api/notifications";
|
|
11
|
+
|
|
12
|
+
export const useNotifications = defineStore("notifications", () => {
|
|
13
|
+
const unread = ref(0);
|
|
14
|
+
const total = ref(0);
|
|
15
|
+
const arrived = ref(0);
|
|
16
|
+
|
|
17
|
+
async function refresh() {
|
|
18
|
+
try {
|
|
19
|
+
const { unread: count, total: all } = await api.notifications({ perPage: 1 });
|
|
20
|
+
|
|
21
|
+
unread.value = count;
|
|
22
|
+
total.value = all;
|
|
23
|
+
} catch {
|
|
24
|
+
// The bell isn't worth an error toast: the count stays as it was.
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// A notification has just been pushed: the count is asked again, and a list that's showing is told.
|
|
29
|
+
function arrive() {
|
|
30
|
+
arrived.value++;
|
|
31
|
+
void refresh();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The counts as a page has just seen them.
|
|
35
|
+
function set(count: number, all: number) {
|
|
36
|
+
unread.value = count;
|
|
37
|
+
total.value = all;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return { unread, total, arrived, refresh, arrive, set };
|
|
41
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
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. What the API answers after a change of its own
|
|
6
|
+
* (their profile saved, an invitation accepted, ownership handed over) replaces the user: receive(). Signing out announces "signedOut" on the app's event bus.
|
|
7
|
+
*
|
|
8
|
+
* A user belongs to one or more organisations and works in one at a time: `organisation` is it, with their `role` in it. The calls for an organisation's data are made for it, which workIn()
|
|
9
|
+
* (src/api/scope.ts) is told whenever the user, or the one they work in, changes.
|
|
10
|
+
*
|
|
11
|
+
* 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
|
|
12
|
+
* from the link.
|
|
13
|
+
*
|
|
14
|
+
* `_user` and `_bannerHidden` are kept in sessionStorage, so a page shows who's signed in before load() has its answer, which replaces it.
|
|
15
|
+
*/
|
|
16
|
+
import { computed, defineStore, ref, useEvents, watch } from "@mana-js/mana";
|
|
17
|
+
|
|
18
|
+
import * as auth from "@/api/auth";
|
|
19
|
+
import type { VerificationLink } from "@/api/auth";
|
|
20
|
+
import * as organisations from "@/api/organisations";
|
|
21
|
+
import { workIn } from "@/api/scope";
|
|
22
|
+
import type { NewOrganisation, OrganisationInput, OrganisationSettings, Registration, User } from "@/api/types";
|
|
23
|
+
|
|
24
|
+
export type { User };
|
|
25
|
+
|
|
26
|
+
export const useSession = defineStore("session", () => {
|
|
27
|
+
const events = useEvents();
|
|
28
|
+
const _user = ref<User | null>(null);
|
|
29
|
+
const _bannerHidden = ref(false);
|
|
30
|
+
|
|
31
|
+
const user = computed(() => _user.value);
|
|
32
|
+
const signedIn = computed(() => _user.value != null);
|
|
33
|
+
const showVerifyBanner = computed(() => _user.value != null && !_user.value.verified && !_bannerHidden.value);
|
|
34
|
+
const organisation = computed(() => _user.value?.organisations.find(one => one.id == _user.value?.organisationId) ?? null);
|
|
35
|
+
const role = computed(() => organisation.value?.role ?? null);
|
|
36
|
+
// Whether they may manage the organisation: its owner and admins.
|
|
37
|
+
const manages = computed(() => role.value == "owner" || role.value == "admin");
|
|
38
|
+
|
|
39
|
+
// What's asked of the API for an organisation is asked for the one the user works in, whichever way they came to: signing in, or a session the browser kept.
|
|
40
|
+
watch(() => _user.value?.organisationId ?? null, id => workIn(id), { immediate: true });
|
|
41
|
+
|
|
42
|
+
// The API's user, and the organisation they work in, or no one.
|
|
43
|
+
function become(next: User | null) {
|
|
44
|
+
workIn(next?.organisationId ?? null);
|
|
45
|
+
|
|
46
|
+
_user.value = next;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Asks the API who's signed in: as the app starts.
|
|
50
|
+
async function load() {
|
|
51
|
+
become(await auth.me());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function signIn(email: string, password: string) {
|
|
55
|
+
become(await auth.signIn(email, password));
|
|
56
|
+
_bannerHidden.value = false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// With an invitation, the account joins its organisation instead of starting one.
|
|
60
|
+
async function register(registration: Registration) {
|
|
61
|
+
become(await auth.register(registration));
|
|
62
|
+
_bannerHidden.value = false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 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.
|
|
66
|
+
async function verify(link: VerificationLink) {
|
|
67
|
+
await auth.verifyEmail(link);
|
|
68
|
+
|
|
69
|
+
if(_user.value)
|
|
70
|
+
become(await auth.me());
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function resendVerification() {
|
|
74
|
+
if(_user.value)
|
|
75
|
+
await auth.resendVerification();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// What the API answered about the user after a change of its own (members and invitations, their profile): the calls are made for the organisation they now work in.
|
|
79
|
+
function receive(next: User) {
|
|
80
|
+
if(_user.value)
|
|
81
|
+
become(next);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Works in another of the user's organisations.
|
|
85
|
+
async function switchOrganisation(id: number) {
|
|
86
|
+
receive(await organisations.switchOrganisation(id));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Starts a new organisation, which the user owns and works in from now on.
|
|
90
|
+
async function createOrganisation(input: NewOrganisation) {
|
|
91
|
+
receive(await organisations.createOrganisation(input));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// The settings of the organisation you're in.
|
|
95
|
+
async function organisationSettings(): Promise<OrganisationSettings> {
|
|
96
|
+
return organisations.organisationSettings(organisationId());
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Saves them, and reads the user again: the switcher shows the new name.
|
|
100
|
+
async function saveOrganisation(input: OrganisationInput) {
|
|
101
|
+
await organisations.updateOrganisation(organisationId(), input);
|
|
102
|
+
|
|
103
|
+
const next = await auth.me();
|
|
104
|
+
|
|
105
|
+
if(next)
|
|
106
|
+
receive(next);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function organisationId() {
|
|
110
|
+
const id = organisation.value?.id;
|
|
111
|
+
|
|
112
|
+
if(id === undefined)
|
|
113
|
+
throw new Error("You're not working in an organisation.");
|
|
114
|
+
|
|
115
|
+
return id;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function hideBanner() {
|
|
119
|
+
_bannerHidden.value = true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 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.
|
|
123
|
+
async function signOut() {
|
|
124
|
+
const user = _user.value;
|
|
125
|
+
|
|
126
|
+
if(!user)
|
|
127
|
+
return;
|
|
128
|
+
|
|
129
|
+
become(null);
|
|
130
|
+
events.emit("signedOut", user);
|
|
131
|
+
await auth.signOut().catch(() => {});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
_user,
|
|
136
|
+
_bannerHidden,
|
|
137
|
+
user,
|
|
138
|
+
signedIn,
|
|
139
|
+
showVerifyBanner,
|
|
140
|
+
organisation,
|
|
141
|
+
role,
|
|
142
|
+
manages,
|
|
143
|
+
load,
|
|
144
|
+
signIn,
|
|
145
|
+
register,
|
|
146
|
+
receive,
|
|
147
|
+
switchOrganisation,
|
|
148
|
+
createOrganisation,
|
|
149
|
+
organisationSettings,
|
|
150
|
+
saveOrganisation,
|
|
151
|
+
verify,
|
|
152
|
+
resendVerification,
|
|
153
|
+
hideBanner,
|
|
154
|
+
signOut,
|
|
155
|
+
};
|
|
156
|
+
}, {
|
|
157
|
+
persist: { storage: "session", pick: ["_user", "_bannerHidden"] },
|
|
158
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// How the app writes dates, for its pages: a day ("2026-10-31") in full, and a moment as a day and a time. In UTC, as the API's are.
|
|
2
|
+
|
|
3
|
+
// A day ("2026-10-31") in full: "31 Oct 2026".
|
|
4
|
+
export function fullDate(date: string) {
|
|
5
|
+
return new Intl.DateTimeFormat("en", { day: "numeric", month: "short", year: "numeric", timeZone: "UTC" }).format(new Date(`${date}T00:00:00Z`));
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// A moment ("2026-10-31T14:05:00Z") as a day and a time: "31 Oct 2026 at 14:05".
|
|
9
|
+
export function moment(timestamp: string) {
|
|
10
|
+
const time = new Date(timestamp);
|
|
11
|
+
const day = new Intl.DateTimeFormat("en", { day: "numeric", month: "short", year: "numeric", timeZone: "UTC" }).format(time);
|
|
12
|
+
const clock = new Intl.DateTimeFormat("en", { hour: "2-digit", minute: "2-digit", hourCycle: "h23", timeZone: "UTC" }).format(time);
|
|
13
|
+
|
|
14
|
+
return `${day} at ${clock}`;
|
|
15
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/*
|
|
3
|
+
* "You don't have access to this page": /forbidden ("forbidden"),
|
|
4
|
+
* inside AppLayout, where the role middleware sends someone whose role in
|
|
5
|
+
* the organisation can't use the page they asked for. It says which role
|
|
6
|
+
* they have, and who can change it.
|
|
7
|
+
*/
|
|
8
|
+
import { computed } from "@mana-js/mana";
|
|
9
|
+
|
|
10
|
+
import { ROLES } from "@/shared/roles";
|
|
11
|
+
import { useSession } from "@/stores/session";
|
|
12
|
+
|
|
13
|
+
defineOptions({ name: "Forbidden" });
|
|
14
|
+
|
|
15
|
+
const sessionStore = useSession();
|
|
16
|
+
|
|
17
|
+
const role = computed(() => sessionStore.role ? ROLES[sessionStore.role].label.toLowerCase() : null);
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<template>
|
|
21
|
+
<div class="page-head"><h1>You don't have access to this page</h1></div>
|
|
22
|
+
|
|
23
|
+
<div class="card form-narrow">
|
|
24
|
+
<p class="card-text" m-if="role">You're {{ role == "admin" || role == "accountant" || role == "owner" ? "an" : "a" }} {{ role }} in {{ sessionStore.organisation?.name }}. Its owner or an admin can change your role.</p>
|
|
25
|
+
<p class="card-text" m-else>Your role in this organisation doesn't include this page.</p>
|
|
26
|
+
<RouterLink class="btn" :to="{ name: 'dashboard' }">Go to the dashboard</RouterLink>
|
|
27
|
+
</div>
|
|
28
|
+
</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 working in <b>{{ sessionStore.organisation?.name }}</b> as {{ sessionStore.role }}. This is the page to start from: <code>src/views/Home.mana</code>.</p>
|
|
14
|
+
</main>
|
|
15
|
+
</template>
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/*
|
|
3
|
+
* Accepting an invitation: /invite?token=… ("invitation"), the link in
|
|
4
|
+
* the invitation's email, open to anyone (no middleware), on its own like the
|
|
5
|
+
* sign-in pages.
|
|
6
|
+
*
|
|
7
|
+
* It reads the invitation by its token, then says, by who's looking:
|
|
8
|
+
* signed in with the invited email — join it (Accept, which adds the
|
|
9
|
+
* organisation to the account and opens its dashboard, S6) or Decline;
|
|
10
|
+
* signed out — sign in to accept (coming back here), or create an account
|
|
11
|
+
* with that email, which joins it at once;
|
|
12
|
+
* signed in as someone else — whom it's for, with signing out to use that
|
|
13
|
+
* account (and coming back here);
|
|
14
|
+
* after 7 days — that it has expired, and whom to ask for a new one.
|
|
15
|
+
* A token that isn't (or is no longer) an invitation says so. Refusals show
|
|
16
|
+
* as toasts.
|
|
17
|
+
*/
|
|
18
|
+
import { computed, ref, resource, useRoute, useRouter } from "@mana-js/mana";
|
|
19
|
+
|
|
20
|
+
import ApiError from "@/api/ApiError";
|
|
21
|
+
import * as members from "@/api/members";
|
|
22
|
+
import AppLogo from "@/components/AppLogo.mana";
|
|
23
|
+
import { ROLES } from "@/shared/roles";
|
|
24
|
+
import { useSession } from "@/stores/session";
|
|
25
|
+
import { useToasts } from "@/stores/toasts";
|
|
26
|
+
|
|
27
|
+
defineOptions({ name: "Invite" });
|
|
28
|
+
|
|
29
|
+
const route = useRoute();
|
|
30
|
+
const router = useRouter();
|
|
31
|
+
const sessionStore = useSession();
|
|
32
|
+
const toastStore = useToasts();
|
|
33
|
+
|
|
34
|
+
const token = computed(() => route.query.token ?? "");
|
|
35
|
+
const details = resource(() => token.value ? members.invitation(token.value) : null);
|
|
36
|
+
const busy = ref(false);
|
|
37
|
+
|
|
38
|
+
// What the page shows, by the invitation and who's looking.
|
|
39
|
+
const state = computed(() => {
|
|
40
|
+
const data = details.data;
|
|
41
|
+
|
|
42
|
+
if(!token.value)
|
|
43
|
+
return "missing";
|
|
44
|
+
|
|
45
|
+
if(details.error)
|
|
46
|
+
return "invalid";
|
|
47
|
+
|
|
48
|
+
if(!data)
|
|
49
|
+
return "loading";
|
|
50
|
+
|
|
51
|
+
if(data.expired)
|
|
52
|
+
return "expired";
|
|
53
|
+
|
|
54
|
+
if(!sessionStore.user)
|
|
55
|
+
return "signed-out";
|
|
56
|
+
|
|
57
|
+
return sessionStore.user.email == data.email ? "invited" : "someone-else";
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// "an admin", "a member".
|
|
61
|
+
const roleText = computed(() => {
|
|
62
|
+
const role = details.data?.role;
|
|
63
|
+
|
|
64
|
+
return role ? `${role == "admin" || role == "owner" ? "an" : "a"} ${ROLES[role].label.toLowerCase()}` : "";
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const back = computed(() => ({ name: "login", query: { redirect: route.fullPath } }));
|
|
68
|
+
|
|
69
|
+
async function accept() {
|
|
70
|
+
busy.value = true;
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
sessionStore.receive(await members.acceptInvitation(token.value));
|
|
74
|
+
await router.replace({ name: "dashboard" });
|
|
75
|
+
} catch(error) {
|
|
76
|
+
toastStore.error("Couldn't accept the invitation", error instanceof ApiError ? error.message : "Something went wrong. Try again.");
|
|
77
|
+
} finally {
|
|
78
|
+
busy.value = false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function decline() {
|
|
83
|
+
busy.value = true;
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
await members.declineInvitation(token.value);
|
|
87
|
+
await router.replace({ name: "dashboard" });
|
|
88
|
+
} catch(error) {
|
|
89
|
+
toastStore.error("Couldn't decline the invitation", error instanceof ApiError ? error.message : "Something went wrong. Try again.");
|
|
90
|
+
} finally {
|
|
91
|
+
busy.value = false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Signs out, then signs in again (with the invited account), coming back here.
|
|
96
|
+
function useOtherAccount() {
|
|
97
|
+
sessionStore.signOut();
|
|
98
|
+
void router.replace(back.value);
|
|
99
|
+
}
|
|
100
|
+
</script>
|
|
101
|
+
|
|
102
|
+
<template>
|
|
103
|
+
<div class="auth invite">
|
|
104
|
+
<AppLogo />
|
|
105
|
+
|
|
106
|
+
<p class="lead" role="status" m-if="state == 'loading'">
|
|
107
|
+
<span class="spin dark" aria-hidden="true"></span>Opening the invitation…
|
|
108
|
+
</p>
|
|
109
|
+
|
|
110
|
+
<Template m-else-if="state == 'invited'">
|
|
111
|
+
<h1>Join {{ details.data?.organisation }}</h1>
|
|
112
|
+
<p class="lead">{{ details.data?.invitedBy }} invited you to join {{ details.data?.organisation }} as {{ roleText }}.</p>
|
|
113
|
+
<div class="actions-stack">
|
|
114
|
+
<button type="button" class="btn primary" :disabled="busy" @click="accept()">Accept and open {{ details.data?.organisation }}</button>
|
|
115
|
+
<button type="button" class="btn" :disabled="busy" @click="decline()">Decline</button>
|
|
116
|
+
</div>
|
|
117
|
+
<p class="hint">You keep your other organisations; switch between them from the top bar.</p>
|
|
118
|
+
</Template>
|
|
119
|
+
|
|
120
|
+
<Template m-else-if="state == 'signed-out'">
|
|
121
|
+
<h1>Join {{ details.data?.organisation }}</h1>
|
|
122
|
+
<p class="lead">{{ details.data?.invitedBy }} invited <b>{{ details.data?.email }}</b> to join {{ details.data?.organisation }} as {{ roleText }}.</p>
|
|
123
|
+
<div class="actions-stack">
|
|
124
|
+
<RouterLink class="btn primary" :to="back">Sign in to accept</RouterLink>
|
|
125
|
+
<RouterLink class="btn" :to="{ name: 'register', query: { invite: token } }">Create an account</RouterLink>
|
|
126
|
+
</div>
|
|
127
|
+
<p class="hint">Creating an account uses this email; you join {{ details.data?.organisation }} as soon as it's made.</p>
|
|
128
|
+
</Template>
|
|
129
|
+
|
|
130
|
+
<Template m-else-if="state == 'someone-else'">
|
|
131
|
+
<h1>This invitation is for someone else</h1>
|
|
132
|
+
<p class="lead">It was sent to <b>{{ details.data?.email }}</b>, and you're signed in as {{ sessionStore.user?.email }}.</p>
|
|
133
|
+
<div class="actions-stack">
|
|
134
|
+
<button type="button" class="btn primary" @click="useOtherAccount()">Sign out and use that account</button>
|
|
135
|
+
<RouterLink class="btn" :to="{ name: 'dashboard' }">Go to your dashboard</RouterLink>
|
|
136
|
+
</div>
|
|
137
|
+
</Template>
|
|
138
|
+
|
|
139
|
+
<Template m-else-if="state == 'expired'">
|
|
140
|
+
<h1>This invitation has expired</h1>
|
|
141
|
+
<p class="lead">Invitations work for 7 days. Ask {{ details.data?.invitedBy }} to send you a new one.</p>
|
|
142
|
+
<RouterLink class="btn" to="/">Go to the app</RouterLink>
|
|
143
|
+
</Template>
|
|
144
|
+
|
|
145
|
+
<Template m-else>
|
|
146
|
+
<h1>This invitation isn't valid</h1>
|
|
147
|
+
<div class="alert" role="alert">{{ state == "missing" ? "This page needs the link from your invitation email." : "It may have been revoked, or used already. Ask whoever invited you to send a new one." }}</div>
|
|
148
|
+
<RouterLink class="btn" to="/">Go to the app</RouterLink>
|
|
149
|
+
</Template>
|
|
150
|
+
</div>
|
|
151
|
+
</template>
|
|
152
|
+
|
|
153
|
+
<style scoped>
|
|
154
|
+
.actions-stack { display: grid; gap: 8px; margin: 0 0 14px; }
|
|
155
|
+
.actions-stack .btn { width: 100%; }
|
|
156
|
+
</style>
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/*
|
|
3
|
+
* Invite someone: /members/invite ("members.invite"),
|
|
4
|
+
* for the organisation's owner and admins (its route middleware, S11). A
|
|
5
|
+
* page, as the app's forms are: an email, and a role — admin, accountant
|
|
6
|
+
* or viewer, each described; ownership is transferred, not given.
|
|
7
|
+
* Sending it goes back to Members, where it waits with its 7 days. The API's
|
|
8
|
+
* answer about a field (someone already in, or invited already) shows on it,
|
|
9
|
+
* anything else as a toast. Leaving with an email typed and not sent
|
|
10
|
+
* asks first.
|
|
11
|
+
*/
|
|
12
|
+
import { reactive, ref, useRouter } from "@mana-js/mana";
|
|
13
|
+
|
|
14
|
+
import ApiError from "@/api/ApiError";
|
|
15
|
+
import * as members from "@/api/members";
|
|
16
|
+
import type { InvitationInput } from "@/api/members";
|
|
17
|
+
import type { Role } from "@/api/types";
|
|
18
|
+
import TextField from "@/components/TextField.mana";
|
|
19
|
+
import { useUnsavedChanges } from "@/composables/unsaved";
|
|
20
|
+
import { ROLES } from "@/shared/roles";
|
|
21
|
+
import { useSession } from "@/stores/session";
|
|
22
|
+
import { useToasts } from "@/stores/toasts";
|
|
23
|
+
|
|
24
|
+
const ROLE_OPTIONS: readonly Role[] = ["admin", "member"];
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<script setup lang="ts">
|
|
28
|
+
defineOptions({ name: "InviteMember" });
|
|
29
|
+
|
|
30
|
+
const router = useRouter();
|
|
31
|
+
const sessionStore = useSession();
|
|
32
|
+
const toastStore = useToasts();
|
|
33
|
+
|
|
34
|
+
const form = reactive<InvitationInput>({ email: "", role: "member" });
|
|
35
|
+
const errors = reactive<{ email?: string; role?: string }>({});
|
|
36
|
+
const sending = ref(false);
|
|
37
|
+
const sent = ref(false);
|
|
38
|
+
|
|
39
|
+
useUnsavedChanges(() => !sent.value && form.email.trim() != "");
|
|
40
|
+
|
|
41
|
+
async function submit() {
|
|
42
|
+
errors.email = /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(form.email.trim()) ? undefined : "Enter an email like name@company.com.";
|
|
43
|
+
errors.role = undefined;
|
|
44
|
+
|
|
45
|
+
const organisationId = sessionStore.organisation?.id;
|
|
46
|
+
|
|
47
|
+
if(errors.email || sending.value || organisationId === undefined)
|
|
48
|
+
return;
|
|
49
|
+
|
|
50
|
+
sending.value = true;
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
await members.invite(organisationId, { email: form.email.trim(), role: form.role });
|
|
54
|
+
sent.value = true;
|
|
55
|
+
await router.push({ name: "members" });
|
|
56
|
+
} catch(error) {
|
|
57
|
+
if(error instanceof ApiError && (error.field == "email" || error.field == "role"))
|
|
58
|
+
errors[error.field] = error.message;
|
|
59
|
+
else
|
|
60
|
+
toastStore.error("Couldn't send the invitation", error instanceof ApiError ? error.message : "Something went wrong. Try again.");
|
|
61
|
+
} finally {
|
|
62
|
+
sending.value = false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
</script>
|
|
66
|
+
|
|
67
|
+
<template>
|
|
68
|
+
<RouterLink class="back" :to="{ name: 'members' }">← Members</RouterLink>
|
|
69
|
+
<div class="page-head"><h1>Invite someone</h1></div>
|
|
70
|
+
|
|
71
|
+
<form class="card form-narrow" novalidate :class="{ 'is-submitting': sending }" @submit.prevent="submit">
|
|
72
|
+
<p class="lead">They'll get a link to join {{ sessionStore.organisation?.name }}. It works for 7 days.</p>
|
|
73
|
+
|
|
74
|
+
<TextField id="invite-email" type="email" label="Email" autocomplete="off" placeholder="name@company.com" :error="errors.email" m-model="form.email" />
|
|
75
|
+
|
|
76
|
+
<fieldset class="roles" aria-describedby="invite-role-error">
|
|
77
|
+
<legend>Role</legend>
|
|
78
|
+
<label class="role-option" m-for="role in ROLE_OPTIONS">
|
|
79
|
+
<input type="radio" name="invite-role" :value="role" m-model="form.role">
|
|
80
|
+
<b>{{ ROLES[role].label }}</b>
|
|
81
|
+
<span>{{ ROLES[role].description }}</span>
|
|
82
|
+
</label>
|
|
83
|
+
<p class="error" id="invite-role-error" m-if="errors.role">{{ errors.role }}</p>
|
|
84
|
+
</fieldset>
|
|
85
|
+
|
|
86
|
+
<div class="form-foot">
|
|
87
|
+
<button type="submit" class="btn primary" :disabled="sending">{{ sending ? "Sending…" : "Send invitation" }}</button>
|
|
88
|
+
<RouterLink class="btn" :to="{ name: 'members' }">Cancel</RouterLink>
|
|
89
|
+
</div>
|
|
90
|
+
</form>
|
|
91
|
+
</template>
|
|
92
|
+
|
|
93
|
+
<style scoped>
|
|
94
|
+
.role-option { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 2px 10px; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 3px; cursor: pointer; }
|
|
95
|
+
.role-option:has(input:checked) { border-color: var(--ink); }
|
|
96
|
+
.role-option input { margin: 3px 0 0; accent-color: var(--ink); grid-row: span 2; }
|
|
97
|
+
.role-option b { font-size: 14px; }
|
|
98
|
+
.role-option span { color: var(--muted); font-size: 13px; }
|
|
99
|
+
</style>
|