adds user management
This commit is contained in:
@@ -1,23 +1,31 @@
|
||||
import PocketBase from 'pocketbase';
|
||||
import { createInstance } from '$lib/pocketbase'
|
||||
import type { Handle } from '@sveltejs/kit'
|
||||
|
||||
export async function handle({ event, resolve }) {
|
||||
event.locals.pb = new PocketBase('http://127.0.0.1:8090');
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
const pb = createInstance()
|
||||
|
||||
// load the store data from the request cookie string
|
||||
event.locals.pb.authStore.loadFromCookie(event.request.headers.get('cookie') || '');
|
||||
|
||||
try {
|
||||
// get an up-to-date auth store state by verifying and refreshing the loaded auth model (if any)
|
||||
event.locals.pb.authStore.isValid && await event.locals.pb.collection('users').authRefresh();
|
||||
} catch (_) {
|
||||
// clear the auth store on failed refresh
|
||||
event.locals.pb.authStore.clear();
|
||||
// load the store data from the request cookie string
|
||||
pb.authStore.loadFromCookie(event.request.headers.get('cookie') || '')
|
||||
try {
|
||||
// get an up-to-date auth store state by verifying and refreshing the loaded auth model (if any)
|
||||
if (pb.authStore.isValid) {
|
||||
await pb.collection('users').authRefresh()
|
||||
}
|
||||
} catch (_) {
|
||||
// clear the auth store on failed refresh
|
||||
pb.authStore.clear()
|
||||
}
|
||||
|
||||
const response = await resolve(event);
|
||||
event.locals.pb = pb
|
||||
event.locals.user = pb.authStore.model
|
||||
|
||||
// send back the default 'pb_auth' cookie to the client with the latest store state
|
||||
response.headers.append('set-cookie', event.locals.pb.authStore.exportToCookie());
|
||||
const response = await resolve(event)
|
||||
|
||||
return response;
|
||||
// send back the default 'pb_auth' cookie to the client with the latest store state
|
||||
response.headers.set(
|
||||
'set-cookie',
|
||||
pb.authStore.exportToCookie({ httpOnly: false })
|
||||
)
|
||||
|
||||
return response
|
||||
}
|
||||
Reference in New Issue
Block a user