This commit is contained in:
@@ -1,31 +1,36 @@
|
||||
import { env } from '$env/dynamic/private';
|
||||
import { fail } from '@sveltejs/kit';
|
||||
import { db } from '$lib/server/db';
|
||||
import { listItemCompletions } from '$lib/server/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
export const actions = {
|
||||
authenticate: async ({ request, cookies }) => {
|
||||
const data = await request.formData();
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const lists = await db.query.lists.findMany({
|
||||
columns: { id: true, name: true, slug: true, isProgress: true },
|
||||
with: { items: { columns: { id: true } } },
|
||||
orderBy: { id: 'desc' }
|
||||
});
|
||||
|
||||
const token = data.get('token');
|
||||
if (!token) {
|
||||
return {
|
||||
status: 400,
|
||||
body: { error: 'Missing token' }
|
||||
};
|
||||
}
|
||||
|
||||
if (token !== env.AUTH_TOKEN) {
|
||||
return {
|
||||
status: 401,
|
||||
body: { error: 'Invalid token' }
|
||||
};
|
||||
}
|
||||
|
||||
cookies.set('token', token, {
|
||||
path: '/',
|
||||
maxAge: 60 * 60 * 24 * 7
|
||||
});
|
||||
|
||||
return {
|
||||
success: true
|
||||
};
|
||||
}
|
||||
const completed = new Set(
|
||||
locals.user
|
||||
? (
|
||||
await db
|
||||
.select({ itemId: listItemCompletions.itemId })
|
||||
.from(listItemCompletions)
|
||||
.where(eq(listItemCompletions.userId, locals.user.id))
|
||||
).map((row) => row.itemId)
|
||||
: []
|
||||
);
|
||||
return {
|
||||
lists: lists.map(({ items, ...list }) => ({
|
||||
...list,
|
||||
total: items.length,
|
||||
checked: list.isProgress ? items.filter((item) => completed.has(item.id)).length : 0
|
||||
}))
|
||||
};
|
||||
};
|
||||
|
||||
// Retire the old shared-token login; legacy cookies grant no permissions.
|
||||
export const actions = {
|
||||
authenticate: () => fail(410, { message: 'Use the central account login at /login' })
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user