create-spillway 0.1.24 → 0.1.26
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/dist/index.d.ts +3 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/templates/astro/.env.example +8 -0
- package/templates/astro/README.md +7 -1
- package/templates/astro/src/env.d.ts +7 -0
- package/templates/astro/src/layouts/Layout.astro +13 -1
- package/templates/astro/src/lib/members.ts +62 -0
- package/templates/astro/src/lib/spillway.ts +2 -2
- package/templates/astro/src/middleware.ts +20 -0
- package/templates/astro/src/pages/[...path].astro +32 -3
- package/templates/astro/src/pages/auth/callback.ts +17 -0
- package/templates/astro/src/pages/auth/login.ts +11 -0
- package/templates/astro/src/pages/auth/logout.ts +12 -0
- package/templates/astro/src/styles/site.css +25 -0
- package/templates/next/.env.example +8 -0
- package/templates/next/README.md +7 -1
- package/templates/next/app/[[...slug]]/page.tsx +15 -4
- package/templates/next/app/auth/callback/route.ts +20 -0
- package/templates/next/app/auth/login/route.ts +14 -0
- package/templates/next/app/auth/logout/route.ts +16 -0
- package/templates/next/app/globals.css +25 -0
- package/templates/next/app/member/[[...slug]]/page.tsx +69 -0
- package/templates/next/components/MemberLinks.tsx +26 -0
- package/templates/next/components/MembersOnly.tsx +17 -0
- package/templates/next/components/SiteShell.tsx +5 -1
- package/templates/next/lib/member-auth.ts +31 -0
- package/templates/next/lib/members.ts +20 -0
- package/templates/next/proxy.ts +31 -0
- package/templates/nuxt/.env.example +8 -0
- package/templates/nuxt/README.md +7 -1
- package/templates/nuxt/app/assets/site.css +25 -0
- package/templates/nuxt/app/components/SiteShell.vue +12 -1
- package/templates/nuxt/app/pages/[...slug].vue +9 -1
- package/templates/nuxt/server/api/_spillway/view.get.ts +2 -1
- package/templates/nuxt/server/middleware/members.ts +9 -0
- package/templates/nuxt/server/routes/auth/callback.get.ts +20 -0
- package/templates/nuxt/server/routes/auth/login.get.ts +12 -0
- package/templates/nuxt/server/routes/auth/logout.post.ts +13 -0
- package/templates/nuxt/server/utils/members.ts +52 -0
- package/templates/nuxt/server/utils/spillway.ts +32 -6
- package/templates/nuxt/shared/spillway.ts +7 -1
- package/templates/sveltekit/.env.example +8 -0
- package/templates/sveltekit/README.md +7 -1
- package/templates/sveltekit/src/app.d.ts +4 -0
- package/templates/sveltekit/src/env.ts +3 -0
- package/templates/sveltekit/src/hooks.server.ts +6 -0
- package/templates/sveltekit/src/lib/components/SiteShell.svelte +16 -1
- package/templates/sveltekit/src/lib/server/members.ts +42 -0
- package/templates/sveltekit/src/lib/server/spillway.ts +32 -6
- package/templates/sveltekit/src/lib/site.css +25 -0
- package/templates/sveltekit/src/lib/view.ts +7 -1
- package/templates/sveltekit/src/routes/[...path]/+page.server.ts +5 -2
- package/templates/sveltekit/src/routes/[...path]/+page.svelte +11 -0
- package/templates/sveltekit/src/routes/auth/callback/+server.ts +20 -0
- package/templates/sveltekit/src/routes/auth/login/+server.ts +14 -0
- package/templates/sveltekit/src/routes/auth/logout/+server.ts +12 -0
|
@@ -48,7 +48,13 @@ On a site with more than one language (**Site settings → Languages**), every l
|
|
|
48
48
|
|
|
49
49
|
## Members
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
Members of the Spillway site sign in on this front end through the site's own OpenID Connect provider, and see the pages only members (or members of their groups) may see. Sign-up, password resets and two-step sign-in stay on Spillway's account pages, which send the member back here.
|
|
52
|
+
|
|
53
|
+
1. In Spillway, turn on accounts (Site settings → Accounts) and, under **Front ends**, add this front end: a public front end, with `<SITE_URL>/auth/callback` as its redirect URI and `<SITE_URL>/` as its post-logout redirect URI (`http://localhost:5173/auth/callback` and `http://localhost:5173/` while you develop). A confidential front end also gets a secret, shown once.
|
|
54
|
+
2. Set `SPILLWAY_MEMBERS_CLIENT_ID` (and `SPILLWAY_MEMBERS_CLIENT_SECRET` for a confidential front end) in the env file. `MEMBER_SESSION_SECRET` seals the session cookie; create-spillway wrote a random one. `SPILLWAY_URL` must be the site's own address, the provider's issuer.
|
|
55
|
+
3. Restart. The header has **Sign in**; a members-only page asks visitors to sign in, and shows a member what Spillway shows them.
|
|
56
|
+
|
|
57
|
+
The session is one `HttpOnly` cookie, sealed with `MEMBER_SESSION_SECRET`, holding the member's tokens; the browser cannot read it, and over https it is named `__Host-spillway_member`, so no other site, not even a sibling subdomain, can plant one. `src/hooks.server.ts` reads the session into `event.locals.member` (refreshing it when its access token is about to expire), and `resolveView` reads a member's pages with their own access token, never through the cache. The routes are `src/routes/auth/login`, `callback` and `logout`; `src/lib/server/members.ts` holds the rest. Sign out ends the sign-in at Spillway too. Set `ORIGIN` to the front end's address where it runs (`ORIGIN=https://www.example.org`): without it the Node adapter assumes https, and SvelteKit refuses the Sign out form, a POST, from any other origin. The flow is `@spillway-cms/client/members`: the authorization code flow with PKCE, `state` and `nonce`, and the ID token checked against the site's keys, with no other dependency.
|
|
52
58
|
|
|
53
59
|
## Previews
|
|
54
60
|
|
|
@@ -7,6 +7,10 @@ declare global {
|
|
|
7
7
|
motion?: string | null;
|
|
8
8
|
/** Set by the page's load when there is no page at the address, so the answer is a 404. */
|
|
9
9
|
notFound?: boolean;
|
|
10
|
+
/** The signed-in member (src/hooks.server.ts, README.md Members), or null. */
|
|
11
|
+
member?: import('@spillway-cms/client/members').MemberSession | null;
|
|
12
|
+
/** Set by the page's load for a members-only page this visitor may not see: 401, or 403 for a member without the group. */
|
|
13
|
+
membersOnly?: 401 | 403;
|
|
10
14
|
}
|
|
11
15
|
}
|
|
12
16
|
}
|
|
@@ -15,6 +15,9 @@ export const variables = defineEnvVars({
|
|
|
15
15
|
SPILLWAY_WEBHOOK_SECRET: { description: 'The secret of the webhook rules that post to /api/revalidate.', schema: optional },
|
|
16
16
|
SPILLWAY_PREVIEW_SECRET: { description: 'Guards /api/preview?preview=1 and signs its cookie.', schema: optional },
|
|
17
17
|
DEPLOY_HOOK_URL: { description: 'A deploy hook the revalidation route calls on brand.published; optional.', schema: optional },
|
|
18
|
+
SPILLWAY_MEMBERS_CLIENT_ID: { description: 'The front end registered under Site settings → Accounts → Front ends; turns members on.', schema: optional },
|
|
19
|
+
SPILLWAY_MEMBERS_CLIENT_SECRET: { description: "That front end's secret, when it is confidential; optional.", schema: optional },
|
|
20
|
+
MEMBER_SESSION_SECRET: { description: 'Seals the member session cookie: a random string of at least 32 characters.', schema: optional },
|
|
18
21
|
SITE_URL: { description: "This front end's own address.", schema: address('http://localhost:5173') },
|
|
19
22
|
SPILLWAY_CACHE_SECONDS: { description: 'Seconds a page stays in the in-memory cache; 0 turns it off.', schema: optional },
|
|
20
23
|
});
|
|
@@ -3,9 +3,13 @@
|
|
|
3
3
|
* Spillway-rendered site: 301 or 302 to the target, with the query string kept; the list is cached and purged by the
|
|
4
4
|
* redirects.updated webhook. Pages then get the language and motion level of what they show on <html>, and the 404
|
|
5
5
|
* status when there is nothing at the address.
|
|
6
|
+
*
|
|
7
|
+
* Members (README.md, Members): the signed-in member, if any, goes to `event.locals.member` (their session refreshed when
|
|
8
|
+
* due), and what is rendered for them is private.
|
|
6
9
|
*/
|
|
7
10
|
import type { Handle } from '@sveltejs/kit/hooks';
|
|
8
11
|
import { redirectFor } from './lib/server/spillway';
|
|
12
|
+
import { readMember } from './lib/server/members';
|
|
9
13
|
|
|
10
14
|
const SKIP = ['/api/', '/_app/'];
|
|
11
15
|
const attr = (value: string) => value.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<');
|
|
@@ -13,6 +17,7 @@ const attr = (value: string) => value.replace(/&/g, '&').replace(/"/g, '&quo
|
|
|
13
17
|
export const handle: Handle = async ({ event, resolve }) => {
|
|
14
18
|
const { pathname, search } = event.url;
|
|
15
19
|
const method = event.request.method;
|
|
20
|
+
event.locals.member = pathname.startsWith('/_app/') ? null : await readMember(event.cookies);
|
|
16
21
|
if ((method === 'GET' || method === 'HEAD') && !SKIP.some((p) => pathname.startsWith(p))) {
|
|
17
22
|
const hit = await redirectFor(pathname);
|
|
18
23
|
if (hit) return new Response(null, { status: hit.status, headers: { location: search && !hit.to.includes('?') ? `${hit.to}${search}` : hit.to, 'cache-control': 'no-store' } });
|
|
@@ -22,5 +27,6 @@ export const handle: Handle = async ({ event, resolve }) => {
|
|
|
22
27
|
html.replace('%spillway.lang%', attr(event.locals.lang ?? 'en')).replace('%spillway.motion%', event.locals.motion ? ` data-sw-motion="${attr(event.locals.motion)}"` : ''),
|
|
23
28
|
});
|
|
24
29
|
if (event.locals.notFound && response.status === 200) return new Response(response.body, { status: 404, statusText: 'Not Found', headers: response.headers });
|
|
30
|
+
if (event.locals.membersOnly && response.status === 200) return new Response(response.body, { status: event.locals.membersOnly, headers: response.headers });
|
|
25
31
|
return response;
|
|
26
32
|
};
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* The shell around every page: the site name, the main and footer menus, the language switcher, the preview bar and, with
|
|
4
|
+
* members set up (README.md, Members), Sign in or the member's name and Sign out.
|
|
5
|
+
*/
|
|
3
6
|
import type { Snippet } from 'svelte';
|
|
4
7
|
import type { Shell } from '../view';
|
|
5
8
|
import SpillwayMenu from './SpillwayMenu.svelte';
|
|
6
9
|
|
|
10
|
+
import { page } from '$app/state';
|
|
11
|
+
|
|
7
12
|
let { shell, children }: { shell: Shell; children: Snippet } = $props();
|
|
8
13
|
</script>
|
|
9
14
|
|
|
@@ -26,6 +31,16 @@
|
|
|
26
31
|
</ul>
|
|
27
32
|
</nav>
|
|
28
33
|
{/if}
|
|
34
|
+
{#if shell.members}
|
|
35
|
+
{#if shell.member}
|
|
36
|
+
<form class="member-links" method="post" action="/auth/logout">
|
|
37
|
+
<span class="member-name">{shell.member.name || shell.member.email || 'Signed in'}</span>
|
|
38
|
+
<button type="submit">Sign out</button>
|
|
39
|
+
</form>
|
|
40
|
+
{:else}
|
|
41
|
+
<a class="member-link" href={`/auth/login?returnTo=${encodeURIComponent(page.url.pathname + page.url.search)}`}>Sign in</a>
|
|
42
|
+
{/if}
|
|
43
|
+
{/if}
|
|
29
44
|
</header>
|
|
30
45
|
<main id="main">{@render children()}</main>
|
|
31
46
|
<footer class="site-footer">
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Members (README.md, Members): sign-in through the Spillway site's OpenID Connect provider, with this front end registered
|
|
3
|
+
* under Site settings → Accounts → Front ends. On while SPILLWAY_MEMBERS_CLIENT_ID and MEMBER_SESSION_SECRET are set.
|
|
4
|
+
*
|
|
5
|
+
* The session is one cookie, sealed with MEMBER_SESSION_SECRET and HttpOnly. src/hooks.server.ts reads it into
|
|
6
|
+
* `event.locals.member`, refreshing it when its access token is about to expire; `resolveView` then reads Spillway with the
|
|
7
|
+
* member's own access token, never through the shared cache, so members-only pages render for them.
|
|
8
|
+
*/
|
|
9
|
+
import type { Cookies } from '@sveltejs/kit';
|
|
10
|
+
import { MEMBER_SESSION_MAX_AGE, createMemberAuth, memberCookieNames, memberCookieOptions, type MemberAuth, type MemberSession } from '@spillway-cms/client/members';
|
|
11
|
+
import { MEMBER_SESSION_SECRET, SITE_URL, SPILLWAY_MEMBERS_CLIENT_ID, SPILLWAY_MEMBERS_CLIENT_SECRET, SPILLWAY_URL } from '$app/env/private';
|
|
12
|
+
|
|
13
|
+
export const members: MemberAuth | null =
|
|
14
|
+
SPILLWAY_MEMBERS_CLIENT_ID && MEMBER_SESSION_SECRET
|
|
15
|
+
? createMemberAuth({
|
|
16
|
+
issuer: SPILLWAY_URL,
|
|
17
|
+
clientId: SPILLWAY_MEMBERS_CLIENT_ID,
|
|
18
|
+
clientSecret: SPILLWAY_MEMBERS_CLIENT_SECRET,
|
|
19
|
+
redirectUri: `${SITE_URL}/auth/callback`,
|
|
20
|
+
postLogoutRedirectUri: `${SITE_URL}/`,
|
|
21
|
+
secret: MEMBER_SESSION_SECRET,
|
|
22
|
+
})
|
|
23
|
+
: null;
|
|
24
|
+
|
|
25
|
+
export const secureCookies = SITE_URL.startsWith('https://');
|
|
26
|
+
/** The cookies' names: `__Host-` prefixed over https, so no other site, not even a sibling subdomain, can plant one. */
|
|
27
|
+
export const { session: SESSION_COOKIE, flow: FLOW_COOKIE } = memberCookieNames(secureCookies);
|
|
28
|
+
export const cookieOptions = (maxAge: number) => memberCookieOptions(maxAge, secureCookies);
|
|
29
|
+
|
|
30
|
+
/** The member signed in on this request (refreshed, and the cookie set again, when due), or null. */
|
|
31
|
+
export async function readMember(cookies: Cookies): Promise<MemberSession | null> {
|
|
32
|
+
const raw = cookies.get(SESSION_COOKIE);
|
|
33
|
+
if (!members || !raw) return null;
|
|
34
|
+
const current = await members.session(raw).catch(() => undefined);
|
|
35
|
+
if (current === undefined) return null; // Spillway did not answer: a visitor for now, the cookie stays
|
|
36
|
+
if (!current) {
|
|
37
|
+
cookies.delete(SESSION_COOKIE, { path: '/', secure: secureCookies });
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
if (current.refreshed) cookies.set(SESSION_COOKIE, current.sessionCookie, cookieOptions(MEMBER_SESSION_MAX_AGE));
|
|
41
|
+
return current.session;
|
|
42
|
+
}
|
|
@@ -21,6 +21,8 @@ import type { HeadData, SearchHit, SearchView, Shell, View } from '../view';
|
|
|
21
21
|
// The brand as the last build wrote it (scripts/brand.mjs); a newer published brand is inlined until the next build.
|
|
22
22
|
import builtBrand from '../spillway-brand.json';
|
|
23
23
|
import { cached } from './cache';
|
|
24
|
+
import { memberClient, type MemberSession } from '@spillway-cms/client/members';
|
|
25
|
+
import { members } from './members';
|
|
24
26
|
|
|
25
27
|
export { SITE_URL, SPILLWAY_URL };
|
|
26
28
|
|
|
@@ -52,14 +54,16 @@ export function loadSite(preview: PreviewState | null = null): Promise<PublicSit
|
|
|
52
54
|
return (preview ? load() : cached('site', async () => ({ value: await load(), tags: [SHELL_TAG] }))).catch(() => null);
|
|
53
55
|
}
|
|
54
56
|
|
|
55
|
-
/**
|
|
57
|
+
/**
|
|
58
|
+
* The page at `path`, or null when Spillway has none. A preview skips the cache; so does a page whose blocks read the query.
|
|
59
|
+
* A members-only page throws on (README.md, Members); `loadMemberPage` reads pages as a signed-in member.
|
|
60
|
+
*/
|
|
56
61
|
export async function loadPage(path: string, query: Record<string, string>, preview: PreviewState | null): Promise<HtmlPage | null> {
|
|
57
62
|
const fetchPage = async (params: Record<string, string>) => {
|
|
58
63
|
try {
|
|
59
64
|
return await spillway.renderHtml(path, { previewState: preview, params });
|
|
60
65
|
} catch (err) {
|
|
61
|
-
|
|
62
|
-
if (err instanceof SpillwayError && (err.status === 404 || err.code === 'members_only')) return null;
|
|
66
|
+
if (err instanceof SpillwayError && err.status === 404) return null;
|
|
63
67
|
throw err;
|
|
64
68
|
}
|
|
65
69
|
};
|
|
@@ -73,6 +77,16 @@ export async function loadPage(path: string, query: Record<string, string>, prev
|
|
|
73
77
|
return page?.queryDependent && Object.keys(query).length ? fetchPage(query) : page;
|
|
74
78
|
}
|
|
75
79
|
|
|
80
|
+
/** The page as a signed-in member sees it: with their own access token, never cached. */
|
|
81
|
+
export async function loadMemberPage(member: MemberSession, path: string, query: Record<string, string>): Promise<HtmlPage | null> {
|
|
82
|
+
try {
|
|
83
|
+
return await memberClient(spillway, member).renderHtml(path, { params: query });
|
|
84
|
+
} catch (err) {
|
|
85
|
+
if (err instanceof SpillwayError && err.status === 404) return null;
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
76
90
|
async function loadShellParts(locale: string, preview: PreviewState | null) {
|
|
77
91
|
const c = spillway.withPreview(preview);
|
|
78
92
|
const read = <T>(key: string, load: () => Promise<T>): Promise<T | null> =>
|
|
@@ -109,14 +123,23 @@ export function pointApiAtSpillway(html: string, origin: string): string {
|
|
|
109
123
|
return origin ? html.replace(/ action="\/api\/forms\//g, ` action="${origin}/api/forms/`).replace(/ href="\/api\/collections\//g, ` href="${origin}/api/collections/`) : html;
|
|
110
124
|
}
|
|
111
125
|
|
|
112
|
-
/** What the request at `path` (with its query string) shows. */
|
|
113
|
-
export async function resolveView(path: string, query: Record<string, string>, request: Request): Promise<View> {
|
|
126
|
+
/** What the request at `path` (with its query string) shows; `member` is the signed-in member (README.md, Members), or null. */
|
|
127
|
+
export async function resolveView(path: string, query: Record<string, string>, request: Request, member: MemberSession | null = null): Promise<View> {
|
|
114
128
|
const preview = readPreview(request);
|
|
115
129
|
const params = Object.fromEntries(Object.entries(query).filter(([k]) => !PREVIEW_PARAMS.has(k)));
|
|
116
130
|
const site = await loadSite(preview);
|
|
117
131
|
const local = site ? splitLocalePath(site, path) : { locale: 'en', path, prefixed: false };
|
|
118
132
|
const isSearch = local.path === '/search';
|
|
119
|
-
const
|
|
133
|
+
const blocked: { kind: 'members' | 'forbidden' | null } = { kind: null };
|
|
134
|
+
const page = isSearch
|
|
135
|
+
? null
|
|
136
|
+
: await (member && !preview ? loadMemberPage(member, path, params) : loadPage(path, params, preview)).catch((err) => {
|
|
137
|
+
if (!(err instanceof SpillwayError) || (err.code !== 'members_only' && err.code !== 'invalid_token')) throw err;
|
|
138
|
+
// invalid_token: the sign-in ended at Spillway (signed out, removed, deactivated); a visitor from here on.
|
|
139
|
+
if (err.code === 'invalid_token') member = null;
|
|
140
|
+
blocked.kind = err.status === 403 ? 'forbidden' : 'members';
|
|
141
|
+
return null;
|
|
142
|
+
});
|
|
120
143
|
const locale = page?.locale ?? local.locale;
|
|
121
144
|
const parts = await loadShellParts(locale, preview);
|
|
122
145
|
const siteName = site?.name ?? page?.site.name ?? 'Spillway site';
|
|
@@ -132,6 +155,8 @@ export async function resolveView(path: string, query: Record<string, string>, r
|
|
|
132
155
|
suggestions: site?.searchSuggestions ?? true,
|
|
133
156
|
searchPath,
|
|
134
157
|
preview: Boolean(preview),
|
|
158
|
+
members: Boolean(members),
|
|
159
|
+
member: member ? { ...(member.name ? { name: member.name } : {}), ...(member.email ? { email: member.email } : {}) } : null,
|
|
135
160
|
};
|
|
136
161
|
|
|
137
162
|
if (page) {
|
|
@@ -170,6 +195,7 @@ export async function resolveView(path: string, query: Record<string, string>, r
|
|
|
170
195
|
return { kind: 'search', shell, head: { ...head, title: q ? `Search: ${q} | ${siteName}` : `Search | ${siteName}` }, search };
|
|
171
196
|
}
|
|
172
197
|
shell.languages = site ? languageLinks(site, { locale, alternates: [] }) : [];
|
|
198
|
+
if (blocked.kind) return { kind: 'members-only', shell, head: { ...head, title: `Members only | ${siteName}` }, forbidden: blocked.kind === 'forbidden', path };
|
|
173
199
|
return { kind: 'not-found', shell, head: { ...head, title: `Page not found | ${siteName}` } };
|
|
174
200
|
}
|
|
175
201
|
|
|
@@ -202,3 +202,28 @@ body {
|
|
|
202
202
|
white-space: nowrap;
|
|
203
203
|
border: 0;
|
|
204
204
|
}
|
|
205
|
+
|
|
206
|
+
/* Members (README.md, Members): Sign in, or the member's name and Sign out, in the header. */
|
|
207
|
+
.member-link,
|
|
208
|
+
.member-links button {
|
|
209
|
+
font: inherit;
|
|
210
|
+
color: var(--sw-color-primary, #1d4ed8);
|
|
211
|
+
}
|
|
212
|
+
.member-links {
|
|
213
|
+
display: flex;
|
|
214
|
+
align-items: center;
|
|
215
|
+
gap: 0.75rem;
|
|
216
|
+
margin: 0;
|
|
217
|
+
}
|
|
218
|
+
.member-links button {
|
|
219
|
+
padding: 0.25rem 0.75rem;
|
|
220
|
+
border: 1px solid currentColor;
|
|
221
|
+
border-radius: var(--sw-radius, 8px);
|
|
222
|
+
background: transparent;
|
|
223
|
+
cursor: pointer;
|
|
224
|
+
}
|
|
225
|
+
.members-only {
|
|
226
|
+
max-width: var(--sw-max-width, 1120px);
|
|
227
|
+
margin: 2rem auto;
|
|
228
|
+
padding: 0 1rem;
|
|
229
|
+
}
|
|
@@ -34,6 +34,10 @@ export interface Shell {
|
|
|
34
34
|
/** The search page in the current locale (/search, /es/search). */
|
|
35
35
|
searchPath: string;
|
|
36
36
|
preview: boolean;
|
|
37
|
+
/** Members are set up (README.md, Members): the header offers Sign in, or the member's name and Sign out. */
|
|
38
|
+
members: boolean;
|
|
39
|
+
/** The signed-in member, or null. */
|
|
40
|
+
member: { name?: string; email?: string } | null;
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
/** One search result: the page and the passage that matched, with the matched words marked. */
|
|
@@ -57,4 +61,6 @@ export interface SearchView {
|
|
|
57
61
|
export type View =
|
|
58
62
|
| { kind: 'page'; shell: Shell; head: HeadData; body: string; scripts: string[] }
|
|
59
63
|
| { kind: 'search'; shell: Shell; head: HeadData; search: SearchView }
|
|
60
|
-
| { kind: 'not-found'; shell: Shell; head: HeadData }
|
|
64
|
+
| { kind: 'not-found'; shell: Shell; head: HeadData }
|
|
65
|
+
/** A members-only page this visitor may not see: `forbidden` for a member without the group, else a sign-in is needed. */
|
|
66
|
+
| { kind: 'members-only'; shell: Shell; head: HeadData; forbidden: boolean; path: string };
|
|
@@ -6,10 +6,13 @@ import { resolveView } from '../../lib/server/spillway';
|
|
|
6
6
|
import type { PageServerLoad } from './$types';
|
|
7
7
|
|
|
8
8
|
export const load: PageServerLoad = async ({ url, request, locals, setHeaders }) => {
|
|
9
|
-
|
|
9
|
+
// locals.member: the signed-in member (src/hooks.server.ts), whose pages are read with their own token.
|
|
10
|
+
const view = await resolveView(url.pathname, Object.fromEntries(url.searchParams), request, locals.member ?? null);
|
|
10
11
|
locals.lang = view.shell.lang;
|
|
11
12
|
locals.motion = view.head.motion;
|
|
12
13
|
locals.notFound = view.kind === 'not-found';
|
|
13
|
-
if (view.
|
|
14
|
+
if (view.kind === 'members-only') locals.membersOnly = view.forbidden ? 403 : 401;
|
|
15
|
+
// A preview, and anything rendered for a signed-in member, is private.
|
|
16
|
+
if (view.shell.preview || view.shell.member) setHeaders({ 'cache-control': 'private, no-store' });
|
|
14
17
|
return { view };
|
|
15
18
|
};
|
|
@@ -43,6 +43,17 @@
|
|
|
43
43
|
<div class="spillway-page">{@html view.body}</div>
|
|
44
44
|
{:else if view.kind === 'search'}
|
|
45
45
|
<SearchPage search={view.search} shell={view.shell} />
|
|
46
|
+
{:else if view.kind === 'members-only'}
|
|
47
|
+
<section class="members-only">
|
|
48
|
+
<h1>{view.forbidden ? 'Not available' : 'Members only'}</h1>
|
|
49
|
+
{#if view.forbidden}
|
|
50
|
+
<p>This page is for other members of the site.</p>
|
|
51
|
+
{:else if view.shell.members}
|
|
52
|
+
<p>This page is for members of the site. <a href={`/auth/login?returnTo=${encodeURIComponent(view.path)}`}>Sign in</a> to see it.</p>
|
|
53
|
+
{:else}
|
|
54
|
+
<p>This page is for members of the site.</p>
|
|
55
|
+
{/if}
|
|
56
|
+
</section>
|
|
46
57
|
{:else}
|
|
47
58
|
<section class="not-found">
|
|
48
59
|
<h1>Page not found</h1>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** GET /auth/callback: the code from the Spillway site becomes the member's session cookie (README.md, Members). */
|
|
2
|
+
import { error, redirect } from '@sveltejs/kit';
|
|
3
|
+
import { MEMBER_SESSION_MAX_AGE, MemberAuthError } from '@spillway-cms/client/members';
|
|
4
|
+
import { cookieOptions, members, SESSION_COOKIE, FLOW_COOKIE, secureCookies } from '../../../lib/server/members';
|
|
5
|
+
import type { RequestHandler } from './$types';
|
|
6
|
+
|
|
7
|
+
export const GET: RequestHandler = async ({ url, cookies }) => {
|
|
8
|
+
if (!members) error(404, 'Members are not set up on this site');
|
|
9
|
+
let returnTo = '/';
|
|
10
|
+
try {
|
|
11
|
+
const done = await members.callback(url, cookies.get(FLOW_COOKIE));
|
|
12
|
+
cookies.set(SESSION_COOKIE, done.sessionCookie, cookieOptions(MEMBER_SESSION_MAX_AGE));
|
|
13
|
+
cookies.delete(FLOW_COOKIE, { path: '/', secure: secureCookies });
|
|
14
|
+
returnTo = done.returnTo;
|
|
15
|
+
} catch (err) {
|
|
16
|
+
const message = err instanceof MemberAuthError ? err.message : 'Signing in did not work. Try again.';
|
|
17
|
+
return new Response(`<!doctype html><title>Sign in</title><p>${message.replace(/[<&]/g, '')}</p><p><a href="/auth/login">Sign in again</a></p>`, { status: 400, headers: { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' } });
|
|
18
|
+
}
|
|
19
|
+
redirect(303, returnTo);
|
|
20
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** GET /auth/login?returnTo=/path: off to the Spillway site's sign-in, coming back to /auth/callback (README.md, Members). */
|
|
2
|
+
import { error } from '@sveltejs/kit';
|
|
3
|
+
import { MEMBER_FLOW_MAX_AGE } from '@spillway-cms/client/members';
|
|
4
|
+
import { cookieOptions, members, FLOW_COOKIE } from '../../../lib/server/members';
|
|
5
|
+
import type { RequestHandler } from './$types';
|
|
6
|
+
|
|
7
|
+
export const GET: RequestHandler = async ({ url, cookies, setHeaders }) => {
|
|
8
|
+
if (!members) error(404, 'Members are not set up on this site');
|
|
9
|
+
const { url: to, flowCookie } = await members.signIn({ returnTo: url.searchParams.get('returnTo') ?? '/' });
|
|
10
|
+
cookies.set(FLOW_COOKIE, flowCookie, cookieOptions(MEMBER_FLOW_MAX_AGE));
|
|
11
|
+
setHeaders({ 'cache-control': 'no-store' });
|
|
12
|
+
// A Response rather than redirect(): SvelteKit's redirect() refuses another origin. The cookie still goes along.
|
|
13
|
+
return new Response(null, { status: 302, headers: { location: to } });
|
|
14
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** POST /auth/logout (a form on this site): the sign-in ends at Spillway too, which sends the browser back here. */
|
|
2
|
+
|
|
3
|
+
import { members, SESSION_COOKIE, secureCookies } from '../../../lib/server/members';
|
|
4
|
+
import type { RequestHandler } from './$types';
|
|
5
|
+
|
|
6
|
+
export const POST: RequestHandler = async ({ cookies, locals }) => {
|
|
7
|
+
// SvelteKit refuses form posts from other origins before they get here (csrf.checkOrigin).
|
|
8
|
+
const to = members ? await members.signOut(locals.member ?? null) : '/';
|
|
9
|
+
cookies.delete(SESSION_COOKIE, { path: '/', secure: secureCookies });
|
|
10
|
+
// A Response rather than redirect(): SvelteKit's redirect() refuses another origin (the provider's logout).
|
|
11
|
+
return new Response(null, { status: 303, headers: { location: to, 'cache-control': 'no-store' } });
|
|
12
|
+
};
|