From 32ebd5744509577f3dc8096897bf0c57f1abdcec Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Thu, 23 Nov 2023 23:21:59 -0600 Subject: [PATCH] Auth --- gradegrid/src/components/GradeBookList.tsx | 40 +++++++++------------- gradegrid/src/config.ts | 1 + gradegrid/src/data/index.ts | 1 + gradegrid/src/data/model.ts | 22 ++++++++++++ gradegrid/src/layouts/Layout.astro | 3 ++ gradegrid/src/pages/gradebooks.astro | 2 +- gradegrid/src/pages/login.astro | 20 ++++++++--- gradegrid/src/utils/index.ts | 1 + gradegrid/src/utils/pb.ts | 13 +++++++ 9 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 gradegrid/src/config.ts create mode 100644 gradegrid/src/data/index.ts create mode 100644 gradegrid/src/data/model.ts create mode 100644 gradegrid/src/utils/index.ts create mode 100644 gradegrid/src/utils/pb.ts diff --git a/gradegrid/src/components/GradeBookList.tsx b/gradegrid/src/components/GradeBookList.tsx index f4a4c89..dc44607 100644 --- a/gradegrid/src/components/GradeBookList.tsx +++ b/gradegrid/src/components/GradeBookList.tsx @@ -1,27 +1,19 @@ -import PocketBase from 'pocketbase' -import { For } from 'solid-js' +import { For, lazy } from 'solid-js' +import { listGradeBooks } from '../utils' -const pb = new PocketBase('http://127.0.0.1:8090') +export const GradeBookList = lazy(async () => { + const gradeBookList = await listGradeBooks() - -// fetch a paginated records list -const resultList = await pb.collection('gradebooks').getList(1, 50, { - filter: `created >= "2022-01-01 00:00:00" && name != 'someField2'`, + return { + default: function GradeBookList() { + return ( +
+ +
+ ) + }, + } }) - -console.log(resultList.items) - -// // you can also fetch all records at once via getFullList -// const records = await pb.collection('gradebooks').getFullList({ -// sort: '-created', -// }) - -export function GradeBookList() { - return ( -
- -
- ) -} diff --git a/gradegrid/src/config.ts b/gradegrid/src/config.ts new file mode 100644 index 0000000..d4f823b --- /dev/null +++ b/gradegrid/src/config.ts @@ -0,0 +1 @@ +export const SESSION_COOKIE_KEY = 'session' diff --git a/gradegrid/src/data/index.ts b/gradegrid/src/data/index.ts new file mode 100644 index 0000000..116e668 --- /dev/null +++ b/gradegrid/src/data/index.ts @@ -0,0 +1 @@ +export * from './model' diff --git a/gradegrid/src/data/model.ts b/gradegrid/src/data/model.ts new file mode 100644 index 0000000..d9db0a4 --- /dev/null +++ b/gradegrid/src/data/model.ts @@ -0,0 +1,22 @@ +export interface Entity { + id: string +} + +export interface GradeBookEntity extends Entity { + gradeBookId: string +} + +export interface GradeBook extends Entity { + name: string +} + +export interface Person extends GradeBookEntity { + first: string + last: string +} + +export interface Course extends GradeBookEntity { + name: string +} + +export interface Enrollment extends GradeBookEntity {} diff --git a/gradegrid/src/layouts/Layout.astro b/gradegrid/src/layouts/Layout.astro index 6a5efb4..4ab1670 100644 --- a/gradegrid/src/layouts/Layout.astro +++ b/gradegrid/src/layouts/Layout.astro @@ -7,6 +7,8 @@ interface Props { } const { title } = Astro.props + +// console.log(Astro.cookies.get(SESSION_COOKIE_KEY)) --- @@ -21,6 +23,7 @@ const { title } = Astro.props Login + Gradebooks diff --git a/gradegrid/src/pages/gradebooks.astro b/gradegrid/src/pages/gradebooks.astro index ad1abd6..7d46958 100644 --- a/gradegrid/src/pages/gradebooks.astro +++ b/gradegrid/src/pages/gradebooks.astro @@ -15,5 +15,5 @@ Astro.cookies.set('counter', String(counter)) --- -
+
diff --git a/gradegrid/src/pages/login.astro b/gradegrid/src/pages/login.astro index 2550656..b9a11b5 100644 --- a/gradegrid/src/pages/login.astro +++ b/gradegrid/src/pages/login.astro @@ -1,23 +1,35 @@ --- import PocketBase from 'pocketbase' import Layout from '../layouts/Layout.astro' +import { SESSION_COOKIE_KEY } from '../config' if (Astro.request.method === 'POST') { try { const data = await Astro.request.formData() const pb = new PocketBase('http://127.0.0.1:8090') - pb.authStore.clear() // may not be necessary on server? + // pb.authStore.clear() // may not be necessary on server? const email = (data.get('email') ?? '') as string const password = (data.get('password') ?? '') as string - pb.collection('users').authWithPassword(email, password) + await pb.collection('users').authWithPassword(email, password) const cookie = pb.authStore.exportToCookie() - Astro.cookies.set('session', cookie) - } catch {} + const [, session = '', expires = ''] = + /^pb_auth=(.*?);.*?Expires=(.*?);/.exec(decodeURIComponent(cookie)) ?? [] + + // console.log('cookie:', session) + + Astro.cookies.set(SESSION_COOKIE_KEY, session, { + sameSite: 'strict', + secure: true, + expires: new Date(expires), + }) + } catch (err) { + console.error(err) + } } --- diff --git a/gradegrid/src/utils/index.ts b/gradegrid/src/utils/index.ts new file mode 100644 index 0000000..49109e8 --- /dev/null +++ b/gradegrid/src/utils/index.ts @@ -0,0 +1 @@ +export * from './pb' diff --git a/gradegrid/src/utils/pb.ts b/gradegrid/src/utils/pb.ts new file mode 100644 index 0000000..2334e09 --- /dev/null +++ b/gradegrid/src/utils/pb.ts @@ -0,0 +1,13 @@ +import PocketBase from 'pocketbase' +import type { GradeBook } from '../data' +import { SESSION_COOKIE_KEY } from '../config' + +export function getPb(): PocketBase { + const pb = new PocketBase('http://127.0.0.1:8090') + pb.authStore.loadFromCookie(document.cookie, SESSION_COOKIE_KEY) + return pb +} + +export function listGradeBooks() { + return getPb().collection('gradebooks').getFullList() +}