sveltekit-firebase-helpers 0.0.3 → 0.0.5
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 +23 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -88,7 +88,7 @@ You can test the handle is working correctly by requesting the `/__/firebase/ini
|
|
|
88
88
|
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
89
|
|
|
90
90
|
```ts
|
|
91
|
-
import {
|
|
91
|
+
import { createProxyHandle } from "sveltekit-firebase-helpers";
|
|
92
92
|
import { env } from '$env/dynamic/public'
|
|
93
93
|
|
|
94
94
|
const auth_domain = env.PUBLIC_FIREBASE_AUTH_DOMAIN
|
|
@@ -98,6 +98,15 @@ export const handle = createProxyHandle(auth_domain)
|
|
|
98
98
|
|
|
99
99
|
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
100
|
|
|
101
|
+
If you need to set any additional http headers you can pass an optional `HeadersInit` as a separate object, e.g.:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
export const handle = createProxyHandle(
|
|
105
|
+
auth_domain,
|
|
106
|
+
{ 'Cross-Origin-Embedder-Policy': 'require-corp' }
|
|
107
|
+
)
|
|
108
|
+
```
|
|
109
|
+
|
|
101
110
|
#### Combined Handle
|
|
102
111
|
|
|
103
112
|
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 +121,15 @@ import { auth } from './routes/firebase-server'
|
|
|
112
121
|
|
|
113
122
|
const auth_domain = env.PUBLIC_FIREBASE_AUTH_DOMAIN
|
|
114
123
|
|
|
115
|
-
|
|
124
|
+
// optional additional http response headers
|
|
125
|
+
const init = { 'Cross-Origin-Embedder-Policy': 'require-corp' }
|
|
126
|
+
|
|
127
|
+
export const handle = createHandle({
|
|
128
|
+
auth,
|
|
129
|
+
options,
|
|
130
|
+
auth_domain,
|
|
131
|
+
init,
|
|
132
|
+
})
|
|
116
133
|
```
|
|
117
134
|
|
|
118
135
|
### Service Worker
|
|
@@ -160,3 +177,7 @@ One additional advantage of this is that your client-side auth dependencies are
|
|
|
160
177
|
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
178
|
|
|
162
179
|
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.
|
|
180
|
+
|
|
181
|
+
## Project configuration
|
|
182
|
+
|
|
183
|
+
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
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getIdToken as k}from"firebase/auth";async function C(t){if(t.currentUser){let o=await k(t.currentUser,!0),{serviceWorker:n}=navigator,{controller:r}=n;r&&await new Promise(c=>{n.addEventListener("message",()=>c(),{once:!0}),r.postMessage({type:"useToken",useToken:o})})}}function y(t){return async({event:n,resolve:r})=>{let{locals:c,request:e}=n,s=e.headers.get("Authorization")?.split("Bearer ")[1];if(s)try{c.user=await t.verifyIdToken(s)}catch(a){console.error(a)}return r(n)}}var g=globalThis.process?.env?.NODE_ENV,_=g&&!g.toLowerCase().startsWith("prod");function w(t,o){let n=JSON.stringify(t),r=new Headers(o?.headers);return r.has("content-length")||r.set("content-length",b.encode(n).byteLength.toString()),r.has("content-type")||r.set("content-type","application/json"),new Response(n,{...o,headers:r})}var b=new TextEncoder;function H(t){return async({event:n,resolve:r})=>{let{request:c,url:e}=n;return c.method==="GET"&&e.pathname.startsWith("/__/firebase/init.json")?
|
|
1
|
+
import{getIdToken as k}from"firebase/auth";async function C(t){if(t.currentUser){let o=await k(t.currentUser,!0),{serviceWorker:n}=navigator,{controller:r}=n;r&&await new Promise(c=>{n.addEventListener("message",()=>c(),{once:!0}),r.postMessage({type:"useToken",useToken:o})})}}function y(t){return async({event:n,resolve:r})=>{let{locals:c,request:e}=n,s=e.headers.get("Authorization")?.split("Bearer ")[1];if(s)try{c.user=await t.verifyIdToken(s)}catch(a){console.error(a)}return r(n)}}var g=globalThis.process?.env?.NODE_ENV,_=g&&!g.toLowerCase().startsWith("prod");function w(t,o){let n=JSON.stringify(t),r=new Headers(o?.headers);return r.has("content-length")||r.set("content-length",b.encode(n).byteLength.toString()),r.has("content-type")||r.set("content-type","application/json"),new Response(n,{...o,headers:r})}var b=new TextEncoder;function H(t){return async({event:n,resolve:r})=>{let{request:c,url:e}=n;return c.method==="GET"&&e.pathname.startsWith("/__/firebase/init.json")?w(t):r(n)}}function T(t,o){return async({event:r,resolve:c})=>{let{request:e,url:i}=r;if(e.method==="GET"&&i.pathname.startsWith("/__/auth/")){i.host=t,i.port="443";let s=await fetch(i,{headers:{"Accept-Encoding":"identity"}}),a=await s.text(),f=new Headers({...o,"Cache-Control":s.headers.get("Cache-Control"),"Content-Type":s.headers.get("Content-Type"),Vary:"accept-encoding"});return new Response(a,{headers:f})}return c(r)}}function l(...t){let o=t.length;return o?({event:n,resolve:r})=>{return c(0,n,{});function c(e,i,s){let a=t[e];return a({event:i,resolve:(f,u)=>{let h=async({html:d,done:x})=>(u?.transformPageChunk&&(d=await u.transformPageChunk({html:d,done:x})??""),s?.transformPageChunk&&(d=await s.transformPageChunk({html:d,done:x})??""),d),p=s?.filterSerializedResponseHeaders??u?.filterSerializedResponseHeaders,m=s?.preload??u?.preload;return e<o-1?c(e+1,f,{transformPageChunk:h,filterSerializedResponseHeaders:p,preload:m}):r(f,{transformPageChunk:h,filterSerializedResponseHeaders:p,preload:m})}})}}:({event:n,resolve:r})=>r(n)}function ye(t){let o=[];return t.auth&&o.push(y(t.auth)),t.options&&o.push(H(t.options)),t.auth_domain&&o.push(T(t.auth_domain,t.init)),l(...o)}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 o=new Promise(async(e,i)=>{let a=await(await fetch("/__/firebase/init.json")).json(),f=E(a),u=S(f,{persistence:[O]});t?.auth_emulator&&j(u,t.auth_emulator),await u.authStateReady(),e(u)}),n;self.addEventListener("message",e=>{function i(){self.clients.matchAll({}).then(function(s){s&&s.length&&s[0].postMessage({ack:!0})})}if(e.data)switch(e.data.type){case"useToken":n=e.data.useToken,i(),setTimeout(()=>n=void 0,5e3);break}});async function r(){let e=await o;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 i=await e.json();return JSON.stringify(i)}else return e.text()}catch{}}self.addEventListener("fetch",e=>{if(new URL(e.request.url).origin!==location.origin)return;async function s(){let a=e.request,f=n??await r();if(f){let u=new Headers(a.headers);u.append("Authorization","Bearer "+f);let h=await c(a);try{a=new Request(a.url,{method:a.method,headers:u,mode:"same-origin",credentials:a.credentials,cache:a.cache,redirect:a.redirect,referrer:a.referrer,body:h})}catch{}}return fetch(a)}e.respondWith(s())})}export{He as addFirebaseAuth,y as createAuthHandle,ye as createHandle,H as createOptionsHandle,T as createProxyHandle,C as syncAuthToken};
|