|
| 1 | +import { initializeApp } from 'firebase/app' |
| 2 | +import { |
| 3 | + initializeAuth, |
| 4 | + browserPopupRedirectResolver, |
| 5 | + browserLocalPersistence, |
| 6 | + signInAnonymously as signInAnonymouslyFirebase, |
| 7 | + onIdTokenChanged as onIdTokenChangedFirebase, |
| 8 | +} from 'firebase/auth' |
| 9 | +import { Firebase } from './types' |
| 10 | + |
| 11 | +let auth: ReturnType<typeof initializeAuth> |
| 12 | + |
| 13 | +if (typeof window !== 'undefined') { |
| 14 | + const firebaseApp = initializeApp({ |
| 15 | + // REPLACE THIS WITH YOUR AUTH CONFIG |
| 16 | + }) |
| 17 | + |
| 18 | + auth = initializeAuth(firebaseApp, { |
| 19 | + popupRedirectResolver: browserPopupRedirectResolver, |
| 20 | + persistence: browserLocalPersistence, |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +const getIsSignedIn: Firebase['getIsSignedIn'] = () => |
| 25 | + Boolean(auth?.currentUser) |
| 26 | + |
| 27 | +const signOut: Firebase['signOut'] = () => auth.signOut() |
| 28 | + |
| 29 | +const signInAnonymously: Firebase['signInAnonymously'] = async () => { |
| 30 | + return (await signInAnonymouslyFirebase(auth)).user |
| 31 | +} |
| 32 | + |
| 33 | +const onIdTokenChanged: Firebase['onIdTokenChanged'] = (callback) => { |
| 34 | + return onIdTokenChangedFirebase(auth, callback) |
| 35 | +} |
| 36 | + |
| 37 | +export { getIsSignedIn, signInAnonymously, signOut, onIdTokenChanged } |
0 commit comments