sveltekit-firebase-helpers 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -54,6 +54,8 @@ import { options } from './firebase'
|
|
|
54
54
|
export const handle = createOptionsHandle(options)
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
+
Optionally pass an additional number of seconds to the `createOptionsHandle` call to publicly cache the response (default = 300, set 0 to disable).
|
|
58
|
+
|
|
57
59
|
Example `options.ts`:
|
|
58
60
|
|
|
59
61
|
```ts
|
|
@@ -88,7 +90,7 @@ You can test the handle is working correctly by requesting the `/__/firebase/ini
|
|
|
88
90
|
To proxy auth requests so you can use `signInWithRedirect` on browsers that block 3rd party cookies (now all of them) use `createAuthHandle` passing in the `application-id.firebaseapp.com` domain name from you Firebase app config, e.g. `captaincodeman-experiment.firebaseapp.com`.
|
|
89
91
|
|
|
90
92
|
```ts
|
|
91
|
-
import {
|
|
93
|
+
import { createProxyHandle } from "sveltekit-firebase-helpers";
|
|
92
94
|
import { env } from '$env/dynamic/public'
|
|
93
95
|
|
|
94
96
|
const auth_domain = env.PUBLIC_FIREBASE_AUTH_DOMAIN
|
|
@@ -98,6 +100,15 @@ export const handle = createProxyHandle(auth_domain)
|
|
|
98
100
|
|
|
99
101
|
Any requests to `/__/auth/...` will be proxied to the `auth_domain` configured, effectively making your app serve the firebase auth endpoints itself to get around the 3rd party cookie restrictions.
|
|
100
102
|
|
|
103
|
+
If you need to set any additional http headers you can pass an optional `HeadersInit` as a separate object, e.g.:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
export const handle = createProxyHandle(
|
|
107
|
+
auth_domain,
|
|
108
|
+
{ 'Cross-Origin-Embedder-Policy': 'require-corp' }
|
|
109
|
+
)
|
|
110
|
+
```
|
|
111
|
+
|
|
101
112
|
#### Combined Handle
|
|
102
113
|
|
|
103
114
|
An alternative is to use a single combined `createHandle` function that will add each individual handle if the property it needs is included.
|
|
@@ -112,7 +123,16 @@ import { auth } from './routes/firebase-server'
|
|
|
112
123
|
|
|
113
124
|
const auth_domain = env.PUBLIC_FIREBASE_AUTH_DOMAIN
|
|
114
125
|
|
|
115
|
-
|
|
126
|
+
// optional additional http response headers
|
|
127
|
+
const init = { 'Cross-Origin-Embedder-Policy': 'require-corp' }
|
|
128
|
+
|
|
129
|
+
export const handle = createHandle({
|
|
130
|
+
auth,
|
|
131
|
+
options,
|
|
132
|
+
max_age: 86400,
|
|
133
|
+
auth_domain,
|
|
134
|
+
init,
|
|
135
|
+
})
|
|
116
136
|
```
|
|
117
137
|
|
|
118
138
|
### Service Worker
|
|
@@ -160,3 +180,7 @@ One additional advantage of this is that your client-side auth dependencies are
|
|
|
160
180
|
When using `signInWithRedirect` your page loads and will automatically handle the redirect token or you may call `getRedirectResult` to get the result yourself. At this point, your SvelteKit `load` functions will have already run with an unauthenticated user so to update your app data you may need to call `invalidateAll` to re-run the load functions. But because of the polling delay, the service-worker may not have the auth token yet so we've provided a `syncAuthToken` method that will send it to the client - call it before the `invalidateAll`.
|
|
161
181
|
|
|
162
182
|
The same situation happens if you update the auth claims on the server inside any endpoint or form action. You can use `syncAuthToken` to ensure that any `invalidateAll` call will include the latest token when data is refreshed from the server.
|
|
183
|
+
|
|
184
|
+
## Project configuration
|
|
185
|
+
|
|
186
|
+
TODO: detail project configutation for local development with and without Firebase Auth Emulator, and what .env settings should be used.
|
package/dist/index.d.ts
CHANGED
|
@@ -7,13 +7,14 @@ declare function syncAuthToken(auth: Auth): Promise<void>;
|
|
|
7
7
|
|
|
8
8
|
declare function createAuthHandle(auth: Auth$1): Handle;
|
|
9
9
|
|
|
10
|
-
declare function createOptionsHandle(options: FirebaseOptions): Handle;
|
|
10
|
+
declare function createOptionsHandle(options: FirebaseOptions, max_age?: number): Handle;
|
|
11
11
|
|
|
12
12
|
declare function createProxyHandle(auth_domain: string, init?: HeadersInit): Handle;
|
|
13
13
|
|
|
14
14
|
declare function createHandle(config: {
|
|
15
15
|
auth?: Auth$1;
|
|
16
16
|
options?: FirebaseOptions;
|
|
17
|
+
max_age?: number;
|
|
17
18
|
auth_domain?: string;
|
|
18
19
|
init?: HeadersInit;
|
|
19
20
|
}): Handle;
|
|
@@ -27,7 +28,7 @@ declare global {
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
/// <reference no-default-lib="true"/>
|
|
30
|
-
declare function addFirebaseAuth(config
|
|
31
|
+
declare function addFirebaseAuth(config?: {
|
|
31
32
|
auth_emulator?: string;
|
|
32
33
|
}): void;
|
|
33
34
|
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getIdToken as k}from"firebase/auth";async function
|
|
1
|
+
import{getIdToken as k}from"firebase/auth";async function W(t){if(t.currentUser){let n=await k(t.currentUser,!0),{serviceWorker:s}=navigator,{controller:r}=s;r&&await new Promise(c=>{s.addEventListener("message",()=>c(),{once:!0}),r.postMessage({type:"useToken",useToken:n})})}}function y(t){return async({event:s,resolve:r})=>{let{locals:c,request:e}=s,o=e.headers.get("Authorization")?.split("Bearer ")[1];if(o)try{c.user=await t.verifyIdToken(o)}catch(i){console.error(i)}return r(s)}}var g=globalThis.process?.env?.NODE_ENV,_=g&&!g.toLowerCase().startsWith("prod");function w(t,n){let s=JSON.stringify(t),r=new Headers(n?.headers);return r.has("content-length")||r.set("content-length",b.encode(s).byteLength.toString()),r.has("content-type")||r.set("content-type","application/json"),new Response(s,{...n,headers:r})}var b=new TextEncoder;function H(t,n=300){return async({event:r,resolve:c})=>{let{request:e,setHeaders:a,url:o}=r;return e.method==="GET"&&o.pathname.startsWith("/__/firebase/init.json")?(n&&a({"Cache-Control":`public, max-age: ${n}`}),w(t)):c(r)}}function T(t,n){return async({event:r,resolve:c})=>{let{request:e,url:a}=r;if(e.method==="GET"&&a.pathname.startsWith("/__/auth/")){a.host=t,a.port="443";let o=await fetch(a,{headers:{"Accept-Encoding":"identity"}}),i=await o.text(),f=new Headers({...n,"Cache-Control":o.headers.get("Cache-Control"),"Content-Type":o.headers.get("Content-Type"),Vary:"accept-encoding"});return new Response(i,{headers:f})}return c(r)}}function l(...t){let n=t.length;return n?({event:s,resolve:r})=>{return c(0,s,{});function c(e,a,o){let i=t[e];return i({event:a,resolve:(f,u)=>{let h=async({html:d,done:x})=>(u?.transformPageChunk&&(d=await u.transformPageChunk({html:d,done:x})??""),o?.transformPageChunk&&(d=await o.transformPageChunk({html:d,done:x})??""),d),p=o?.filterSerializedResponseHeaders??u?.filterSerializedResponseHeaders,m=o?.preload??u?.preload;return e<n-1?c(e+1,f,{transformPageChunk:h,filterSerializedResponseHeaders:p,preload:m}):r(f,{transformPageChunk:h,filterSerializedResponseHeaders:p,preload:m})}})}}:({event:s,resolve:r})=>r(s)}function ye(t){let n=[];return t.auth&&n.push(y(t.auth)),t.options&&n.push(H(t.options,t.max_age)),t.auth_domain&&n.push(T(t.auth_domain,t.init)),l(...n)}import{initializeApp as E}from"firebase/app";import{getIdToken as R,initializeAuth as S,connectAuthEmulator as j,indexedDBLocalPersistence as O}from"firebase/auth";function He(t){let n=new Promise(async(e,a)=>{let i=await(await fetch("/__/firebase/init.json")).json(),f=E(i),u=S(f,{persistence:[O]});t?.auth_emulator&&j(u,t.auth_emulator),await u.authStateReady(),e(u)}),s;self.addEventListener("message",e=>{function a(){self.clients.matchAll({}).then(function(o){o&&o.length&&o[0].postMessage({ack:!0})})}if(e.data)switch(e.data.type){case"useToken":s=e.data.useToken,a(),setTimeout(()=>s=void 0,5e3);break}});async function r(){let e=await n;if(e.currentUser)try{return await R(e.currentUser)}catch{return null}else return null}async function c(e){try{if(e.method!=="GET")if(e.headers.get("Content-Type")?.indexOf("json")!==-1){let a=await e.json();return JSON.stringify(a)}else return e.text()}catch{}}self.addEventListener("fetch",e=>{if(new URL(e.request.url).origin!==location.origin)return;async function o(){let i=e.request,f=s??await r();if(f){let u=new Headers(i.headers);u.append("Authorization","Bearer "+f);let h=await c(i);try{i=new Request(i.url,{method:i.method,headers:u,mode:"same-origin",credentials:i.credentials,cache:i.cache,redirect:i.redirect,referrer:i.referrer,body:h})}catch{}}return fetch(i)}e.respondWith(o())})}export{He as addFirebaseAuth,y as createAuthHandle,ye as createHandle,H as createOptionsHandle,T as createProxyHandle,W as syncAuthToken};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sveltekit-firebase-helpers",
|
|
3
3
|
"description": "Helpers for using Firebase with SvelteKit",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.6",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
7
7
|
"!dist/**/*.test.*",
|
|
@@ -25,21 +25,21 @@
|
|
|
25
25
|
"svelte": "^5.0.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@sveltejs/adapter-auto": "^6.0.
|
|
29
|
-
"@sveltejs/kit": "^2.
|
|
30
|
-
"@sveltejs/vite-plugin-svelte": "^5.
|
|
28
|
+
"@sveltejs/adapter-auto": "^6.0.1",
|
|
29
|
+
"@sveltejs/kit": "^2.22.2",
|
|
30
|
+
"@sveltejs/vite-plugin-svelte": "^5.1.0",
|
|
31
31
|
"esm-env": "^1.2.2",
|
|
32
|
-
"firebase": "^11.
|
|
32
|
+
"firebase": "^11.10.0",
|
|
33
33
|
"firebase-admin": "^13.4.0",
|
|
34
|
-
"prettier": "^3.
|
|
35
|
-
"prettier-plugin-svelte": "^3.
|
|
36
|
-
"publint": "^0.3.
|
|
37
|
-
"svelte": "^5.
|
|
38
|
-
"svelte-check": "^4.
|
|
34
|
+
"prettier": "^3.6.2",
|
|
35
|
+
"prettier-plugin-svelte": "^3.4.0",
|
|
36
|
+
"publint": "^0.3.12",
|
|
37
|
+
"svelte": "^5.35.1",
|
|
38
|
+
"svelte-check": "^4.2.2",
|
|
39
39
|
"tsup": "^8.5.0",
|
|
40
|
-
"typescript": "^5.
|
|
41
|
-
"vite": "^6.
|
|
42
|
-
"vite-plugin-devtools-json": "^0.2.
|
|
40
|
+
"typescript": "^5.8.3",
|
|
41
|
+
"vite": "^6.3.5",
|
|
42
|
+
"vite-plugin-devtools-json": "^0.2.1",
|
|
43
43
|
"vite-plugin-mkcert": "^1.17.8"
|
|
44
44
|
},
|
|
45
45
|
"keywords": [
|