|
| 1 | +const authModule = require("@firebase/auth") |
| 2 | +const { |
| 3 | + getStorybookAuthState, |
| 4 | + setStorybookAuthState, |
| 5 | + shouldAllowFirebaseCall, |
| 6 | + throwBlockedFirebaseCall |
| 7 | +} = require("./common") |
| 8 | + |
| 9 | +function emitAuthState(callback) { |
| 10 | + const { user } = getStorybookAuthState() |
| 11 | + callback(user) |
| 12 | +} |
| 13 | + |
| 14 | +function block(apiName) { |
| 15 | + return () => { |
| 16 | + throwBlockedFirebaseCall( |
| 17 | + "auth", |
| 18 | + apiName, |
| 19 | + "Mock auth state in your story providers or pass explicit props that avoid auth hooks." |
| 20 | + ) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function onAuthStateChanged(auth, nextOrObserver, error, completed) { |
| 25 | + if (shouldAllowFirebaseCall()) { |
| 26 | + return authModule.onAuthStateChanged(auth, nextOrObserver, error, completed) |
| 27 | + } |
| 28 | + |
| 29 | + const callback = |
| 30 | + typeof nextOrObserver === "function" |
| 31 | + ? nextOrObserver |
| 32 | + : nextOrObserver?.next ?? (() => undefined) |
| 33 | + |
| 34 | + emitAuthState(callback) |
| 35 | + |
| 36 | + return () => undefined |
| 37 | +} |
| 38 | + |
| 39 | +function patchAuthInstance(auth) { |
| 40 | + if (!auth || shouldAllowFirebaseCall()) return auth |
| 41 | + |
| 42 | + return new Proxy(auth, { |
| 43 | + get(target, prop, receiver) { |
| 44 | + if (prop === "onAuthStateChanged") { |
| 45 | + return onAuthStateChanged |
| 46 | + } |
| 47 | + return Reflect.get(target, prop, receiver) |
| 48 | + } |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +function getAuth(...args) { |
| 53 | + const auth = authModule.getAuth(...args) |
| 54 | + return patchAuthInstance(auth) |
| 55 | +} |
| 56 | + |
| 57 | +function initializeAuth(...args) { |
| 58 | + const auth = authModule.initializeAuth(...args) |
| 59 | + return patchAuthInstance(auth) |
| 60 | +} |
| 61 | + |
| 62 | +module.exports = { |
| 63 | + ...authModule, |
| 64 | + getAuth, |
| 65 | + initializeAuth, |
| 66 | + onAuthStateChanged, |
| 67 | + signInWithEmailAndPassword: block("signInWithEmailAndPassword"), |
| 68 | + signInWithPopup: block("signInWithPopup"), |
| 69 | + signInWithRedirect: block("signInWithRedirect"), |
| 70 | + signInAnonymously: block("signInAnonymously"), |
| 71 | + mockLoggedOutAuthState() { |
| 72 | + return setStorybookAuthState({ user: null }) |
| 73 | + }, |
| 74 | + mockLoggedInUserAuthState(overrides = {}) { |
| 75 | + const user = { |
| 76 | + uid: "storybook-user", |
| 77 | + email: "storybook-user@example.com", |
| 78 | + emailVerified: true, |
| 79 | + displayName: "Storybook User", |
| 80 | + isAnonymous: false, |
| 81 | + providerId: "firebase", |
| 82 | + photoURL: null, |
| 83 | + phoneNumber: null, |
| 84 | + tenantId: null, |
| 85 | + metadata: { |
| 86 | + creationTime: "", |
| 87 | + lastSignInTime: "" |
| 88 | + }, |
| 89 | + providerData: [], |
| 90 | + refreshToken: "storybook-refresh-token", |
| 91 | + stsTokenManager: { |
| 92 | + accessToken: "storybook-access-token", |
| 93 | + refreshToken: "storybook-refresh-token", |
| 94 | + expirationTime: Date.now() + 60 * 60 * 1000 |
| 95 | + }, |
| 96 | + getIdToken: async () => "storybook-id-token", |
| 97 | + getIdTokenResult: async () => ({ |
| 98 | + token: "storybook-id-token", |
| 99 | + authTime: "", |
| 100 | + issuedAtTime: "", |
| 101 | + expirationTime: "", |
| 102 | + signInProvider: null, |
| 103 | + claims: { |
| 104 | + role: "user", |
| 105 | + email_verified: true |
| 106 | + } |
| 107 | + }), |
| 108 | + reload: async () => undefined, |
| 109 | + delete: async () => undefined, |
| 110 | + toJSON: () => ({}) |
| 111 | + } |
| 112 | + |
| 113 | + return setStorybookAuthState({ |
| 114 | + user: { ...user, ...overrides }, |
| 115 | + claims: { role: "user", email_verified: true } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments