pocketbase
Advanced tools
Comparing version 0.8.0 to 0.8.1
@@ -82,3 +82,3 @@ interface SerializeOptions { | ||
/** | ||
* Checks if the store has valid (aka. existing and unexpired) token. | ||
* Loosely checks if the store has valid token (aka. existing and unexpired exp claim). | ||
*/ | ||
@@ -97,2 +97,22 @@ get isValid(): boolean; | ||
* with the cookie's token and model data. | ||
* | ||
* NB! This function doesn't validate the token or its data. | ||
* Usually this isn't a concern if you are interacting only with the | ||
* PocketBase API because it has the proper server-side security checks in place, | ||
* but if you are using the store `isValid` state for permission controls | ||
* in a node server (eg. SSR), then it is recommended to call `authRefresh()` | ||
* after loading the cookie to ensure an up-to-date token and model state. | ||
* For example: | ||
* | ||
* ```js | ||
* pb.authStore.loadFromCookie("cookie string..."); | ||
* | ||
* try { | ||
* // get an up-to-date auth store state by veryfing and refreshing the loaded auth model (if any) | ||
* pb.authStore.isValid && await pb.collection('users').authRefresh(); | ||
* } catch (_) { | ||
* // clear the auth store on failed refresh | ||
* pb.authStore.clear(); | ||
* } | ||
* ``` | ||
*/ | ||
@@ -99,0 +119,0 @@ loadFromCookie(cookie: string, key?: string): void; |
@@ -82,3 +82,3 @@ interface SerializeOptions { | ||
/** | ||
* Checks if the store has valid (aka. existing and unexpired) token. | ||
* Loosely checks if the store has valid token (aka. existing and unexpired exp claim). | ||
*/ | ||
@@ -97,2 +97,22 @@ get isValid(): boolean; | ||
* with the cookie's token and model data. | ||
* | ||
* NB! This function doesn't validate the token or its data. | ||
* Usually this isn't a concern if you are interacting only with the | ||
* PocketBase API because it has the proper server-side security checks in place, | ||
* but if you are using the store `isValid` state for permission controls | ||
* in a node server (eg. SSR), then it is recommended to call `authRefresh()` | ||
* after loading the cookie to ensure an up-to-date token and model state. | ||
* For example: | ||
* | ||
* ```js | ||
* pb.authStore.loadFromCookie("cookie string..."); | ||
* | ||
* try { | ||
* // get an up-to-date auth store state by veryfing and refreshing the loaded auth model (if any) | ||
* pb.authStore.isValid && await pb.collection('users').authRefresh(); | ||
* } catch (_) { | ||
* // clear the auth store on failed refresh | ||
* pb.authStore.clear(); | ||
* } | ||
* ``` | ||
*/ | ||
@@ -99,0 +119,0 @@ loadFromCookie(cookie: string, key?: string): void; |
@@ -82,3 +82,3 @@ interface SerializeOptions { | ||
/** | ||
* Checks if the store has valid (aka. existing and unexpired) token. | ||
* Loosely checks if the store has valid token (aka. existing and unexpired exp claim). | ||
*/ | ||
@@ -97,2 +97,22 @@ get isValid(): boolean; | ||
* with the cookie's token and model data. | ||
* | ||
* NB! This function doesn't validate the token or its data. | ||
* Usually this isn't a concern if you are interacting only with the | ||
* PocketBase API because it has the proper server-side security checks in place, | ||
* but if you are using the store `isValid` state for permission controls | ||
* in a node server (eg. SSR), then it is recommended to call `authRefresh()` | ||
* after loading the cookie to ensure an up-to-date token and model state. | ||
* For example: | ||
* | ||
* ```js | ||
* pb.authStore.loadFromCookie("cookie string..."); | ||
* | ||
* try { | ||
* // get an up-to-date auth store state by veryfing and refreshing the loaded auth model (if any) | ||
* pb.authStore.isValid && await pb.collection('users').authRefresh(); | ||
* } catch (_) { | ||
* // clear the auth store on failed refresh | ||
* pb.authStore.clear(); | ||
* } | ||
* ``` | ||
*/ | ||
@@ -99,0 +119,0 @@ loadFromCookie(cookie: string, key?: string): void; |
@@ -82,3 +82,3 @@ interface SerializeOptions { | ||
/** | ||
* Checks if the store has valid (aka. existing and unexpired) token. | ||
* Loosely checks if the store has valid token (aka. existing and unexpired exp claim). | ||
*/ | ||
@@ -97,2 +97,22 @@ get isValid(): boolean; | ||
* with the cookie's token and model data. | ||
* | ||
* NB! This function doesn't validate the token or its data. | ||
* Usually this isn't a concern if you are interacting only with the | ||
* PocketBase API because it has the proper server-side security checks in place, | ||
* but if you are using the store `isValid` state for permission controls | ||
* in a node server (eg. SSR), then it is recommended to call `authRefresh()` | ||
* after loading the cookie to ensure an up-to-date token and model state. | ||
* For example: | ||
* | ||
* ```js | ||
* pb.authStore.loadFromCookie("cookie string..."); | ||
* | ||
* try { | ||
* // get an up-to-date auth store state by veryfing and refreshing the loaded auth model (if any) | ||
* pb.authStore.isValid && await pb.collection('users').authRefresh(); | ||
* } catch (_) { | ||
* // clear the auth store on failed refresh | ||
* pb.authStore.clear(); | ||
* } | ||
* ``` | ||
*/ | ||
@@ -99,0 +119,0 @@ loadFromCookie(cookie: string, key?: string): void; |
{ | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"name": "pocketbase", | ||
@@ -4,0 +4,0 @@ "description": "PocketBase JavaScript SDK", |
111
README.md
PocketBase JavaScript SDK | ||
====================================================================== | ||
> **⚠️ This is a pre-release, contains breaking changes and works only with the new PocketBase v0.8+ API!** | ||
Official JavaScript SDK (browser and node) for interacting with the [PocketBase API](https://pocketbase.io/docs). | ||
@@ -42,3 +40,3 @@ | ||
```sh | ||
npm install pocketbase@next --save | ||
npm install pocketbase --save | ||
``` | ||
@@ -350,11 +348,19 @@ | ||
export async function handle({ event, resolve }) { | ||
event.locals.pocketbase = new PocketBase("http://127.0.0.1:8090"); | ||
event.locals.pb = new PocketBase("http://127.0.0.1:8090"); | ||
// load the store data from the request cookie string | ||
event.locals.pocketbase.authStore.loadFromCookie(event.request.headers.get('cookie') || ''); | ||
event.locals.pb.authStore.loadFromCookie(event.request.headers.get('cookie') || ''); | ||
try { | ||
// get an up-to-date auth store state by veryfing 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(); | ||
} | ||
const response = await resolve(event); | ||
// send back the default 'pb_auth' cookie to the client with the latest store state | ||
response.headers.set('set-cookie', event.locals.pocketbase.authStore.exportToCookie()); | ||
response.headers.set('set-cookie', event.locals.pb.authStore.exportToCookie()); | ||
@@ -365,3 +371,3 @@ return response; | ||
And then, in some of your server-side actions, you could directly access the previously created `event.locals.pocketbase` instance: | ||
And then, in some of your server-side actions, you could directly access the previously created `event.locals.pb` instance: | ||
@@ -375,3 +381,3 @@ ```js | ||
const { token, user } = await locals.pocketbase.collection('users').authWithPassword(email, password); | ||
const { token, record } = await locals.pb.collection('users').authWithPassword(email, password); | ||
@@ -393,22 +399,26 @@ return new Response('Success...'); | ||
export default defineNuxtPlugin((nuxtApp) => { | ||
return { | ||
provide: { | ||
pocketbase: () => { | ||
const pb = new PocketBase('http://127.0.0.1:8090'); | ||
export default defineNuxtPlugin(async (nuxtApp) => { | ||
const pb = new PocketBase('http://127.0.0.1:8090'); | ||
// load the store data from the request cookie string | ||
pb.authStore.loadFromCookie(nuxtApp.ssrContext?.event?.req?.headers?.cookie || ''); | ||
// load the store data from the request cookie string | ||
pb.authStore.loadFromCookie(nuxtApp.ssrContext?.event?.req?.headers?.cookie || ''); | ||
// send back the default 'pb_auth' cookie to the client with the latest store state | ||
pb.authStore.onChange(() => { | ||
if (nuxtApp.ssrContext?.event?.res) { | ||
nuxtApp.ssrContext.event.res.setHeader('set-cookie', pb.authStore.exportToCookie()); | ||
} | ||
}); | ||
// send back the default 'pb_auth' cookie to the client with the latest store state | ||
pb.authStore.onChange(() => { | ||
if (nuxtApp.ssrContext?.event?.res) { | ||
nuxtApp.ssrContext.event.res.setHeader('set-cookie', pb.authStore.exportToCookie()); | ||
} | ||
}); | ||
return pb; | ||
} | ||
} | ||
try { | ||
// get an up-to-date auth store state by veryfing and refreshing the loaded auth model (if any) | ||
pb.authStore.isValid && await pb.collection('users').authRefresh(); | ||
} catch (_) { | ||
// clear the auth store on failed refresh | ||
pb.authStore.clear(); | ||
} | ||
return { | ||
provide: { pb } | ||
} | ||
}); | ||
@@ -428,6 +438,6 @@ ``` | ||
const { data } = await useAsyncData(async (nuxtApp) => { | ||
const pb = nuxtApp.$pocketbase(); | ||
// fetch and return all "example" records... | ||
const records = await nuxtApp.$pb.collection('example').getFullList(); | ||
// fetch and return all "example" records... | ||
return await pb.collection('example').getFullList(); | ||
return structuredClone(records); | ||
}) | ||
@@ -447,3 +457,3 @@ </script> | ||
export default (ctx, inject) => { | ||
export default async (ctx, inject) => { | ||
const pb = new PocketBase('http://127.0.0.1:8090'); | ||
@@ -459,2 +469,10 @@ | ||
try { | ||
// get an up-to-date auth store state by veryfing and refreshing the loaded auth model (if any) | ||
pb.authStore.isValid && await pb.collection('users').authRefresh(); | ||
} catch (_) { | ||
// clear the auth store on failed refresh | ||
pb.authStore.clear(); | ||
} | ||
inject('pocketbase', pb); | ||
@@ -496,30 +514,29 @@ }; | ||
```jsx | ||
import PocketBase, { BaseAuthStore } from 'pocketbase'; | ||
import PocketBase from 'pocketbase'; | ||
class NextAuthStore extends BaseAuthStore { | ||
constructor(req, res) { | ||
super(); | ||
// you can place this helper in a separate file so that it can be reused | ||
async function initPocketBase(req, res) { | ||
const pb = new PocketBase('http://127.0.0.1:8090'); | ||
this.req = req; | ||
this.res = res; | ||
// load the store data from the request cookie string | ||
pb.loadFromCookie(req?.headers?.cookie || ''); | ||
this.loadFromCookie(this.req?.headers?.cookie); | ||
} | ||
// send back the default 'pb_auth' cookie to the client with the latest store state | ||
pb.authStore.onChange(() => { | ||
res?.setHeader('set-cookie', pb.authStore.exportToCookie()); | ||
}); | ||
save(token, model) { | ||
super.save(token, model); | ||
this.res?.setHeader('set-cookie', this.exportToCookie()); | ||
try { | ||
// get an up-to-date auth store state by veryfing and refreshing the loaded auth model (if any) | ||
pb.authStore.isValid && await pb.collection('users').authRefresh(); | ||
} catch (_) { | ||
// clear the auth store on failed refresh | ||
pb.authStore.clear(); | ||
} | ||
clear() { | ||
super.clear(); | ||
this.res?.setHeader('set-cookie', this.exportToCookie()); | ||
} | ||
return pb | ||
} | ||
export async function getServerSideProps({ req, res }) { | ||
const pb = new PocketBase('http://127.0.0.1:8090'); | ||
pb.authStore = new NextAuthStore(req, res); | ||
const pb = await initPocketBase(req, res) | ||
@@ -526,0 +543,0 @@ // fetch example records... |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1125670
4048
807