Skip to content

Commit

Permalink
Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Nov 24, 2023
1 parent 34f7d85 commit 32ebd57
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 29 deletions.
40 changes: 16 additions & 24 deletions gradegrid/src/components/GradeBookList.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<ul>
Gradebooks
<For each={gradeBookList}>{({ name }) => <li>{name}</li>}</For>
</ul>
</div>
)
},
}
})

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 (
<div>
<ul>
<For each={resultList.items}>{({ name }) => <li>{name}</li>}</For>
</ul>
</div>
)
}
1 change: 1 addition & 0 deletions gradegrid/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SESSION_COOKIE_KEY = 'session'
1 change: 1 addition & 0 deletions gradegrid/src/data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './model'
22 changes: 22 additions & 0 deletions gradegrid/src/data/model.ts
Original file line number Diff line number Diff line change
@@ -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 {}
3 changes: 3 additions & 0 deletions gradegrid/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ interface Props {
}
const { title } = Astro.props
// console.log(Astro.cookies.get(SESSION_COOKIE_KEY))
---

<!doctype html>
Expand All @@ -21,6 +23,7 @@ const { title } = Astro.props
</head>
<body>
<a href="/login">Login</a>
<a href="/gradebooks">Gradebooks</a>
<slot />
</body>
</html>
Expand Down
2 changes: 1 addition & 1 deletion gradegrid/src/pages/gradebooks.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Astro.cookies.set('counter', String(counter))
---

<Layout title="Gradebooks | GradeGrid">
<main><GradeBookList client:load /></main>
<main><GradeBookList client:only /></main>
</Layout>
20 changes: 16 additions & 4 deletions gradegrid/src/pages/login.astro
Original file line number Diff line number Diff line change
@@ -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)
}
}
---

Expand Down
1 change: 1 addition & 0 deletions gradegrid/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './pb'
13 changes: 13 additions & 0 deletions gradegrid/src/utils/pb.ts
Original file line number Diff line number Diff line change
@@ -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<GradeBook>()
}

0 comments on commit 32ebd57

Please sign in to comment.