diff --git a/apps/next/pages/account.tsx b/apps/next/pages/account.tsx new file mode 100644 index 0000000..857a9d9 --- /dev/null +++ b/apps/next/pages/account.tsx @@ -0,0 +1 @@ +export { default } from 'app/features/auth/my-account/screen' diff --git a/packages/app/features/auth/firebase/init.native.ts b/packages/app/features/auth/firebase/init.native.ts index a9d8017..0699557 100644 --- a/packages/app/features/auth/firebase/init.native.ts +++ b/packages/app/features/auth/firebase/init.native.ts @@ -11,7 +11,7 @@ const signInAnonymously: Firebase['signInAnonymously'] = async () => { } const onAuthStateChanged: Firebase['onAuthStateChanged'] = (callback) => { - return auth().onIdTokenChanged(callback) + return auth().onAuthStateChanged(callback) } const getCurrentUser: Firebase['getCurrentUser'] = () => auth().currentUser diff --git a/packages/app/features/auth/firebase/init.web.ts b/packages/app/features/auth/firebase/init.web.ts index d31bf4f..2e9fdc1 100644 --- a/packages/app/features/auth/firebase/init.web.ts +++ b/packages/app/features/auth/firebase/init.web.ts @@ -1,10 +1,11 @@ +// please note that firebase auth adds about 30kb to your bundle size on Web import { initializeApp } from 'firebase/app' import { initializeAuth, browserPopupRedirectResolver, browserLocalPersistence, signInAnonymously as signInAnonymouslyFirebase, - onIdTokenChanged as onIdTokenChangedFirebase, + onAuthStateChanged as onAuthStateChangedFirebase, } from 'firebase/auth' import { Firebase } from './types' @@ -12,7 +13,7 @@ let auth: ReturnType if (typeof window !== 'undefined') { const firebaseApp = initializeApp({ - // REPLACE THIS WITH YOUR AUTH CONFIG + // TODO REPLACE THIS WITH YOUR AUTH CONFIG apiKey: 'AIzaSyAQZ1A-bJMQqjdzNQhRPkbA7swEFnwUS_w', authDomain: 'solito-example.firebaseapp.com', projectId: 'solito-example', @@ -37,7 +38,7 @@ const signInAnonymously: Firebase['signInAnonymously'] = async () => { } const onAuthStateChanged: Firebase['onAuthStateChanged'] = (callback) => { - return onIdTokenChangedFirebase(auth, callback) + return onAuthStateChangedFirebase(auth, callback) } const getCurrentUser: Firebase['getCurrentUser'] = () => auth.currentUser diff --git a/packages/app/features/auth/gate/index.tsx b/packages/app/features/auth/gate/index.tsx new file mode 100644 index 0000000..9a75414 --- /dev/null +++ b/packages/app/features/auth/gate/index.tsx @@ -0,0 +1,30 @@ +import { Text, View } from '@dripsy/core' +import { useAuth } from '../context' +import { signInAnonymously } from '../firebase' + +export function AuthGate({ + children, +}: { + children: React.ReactNode | ((user: { uid: string }) => React.ReactNode) +}) { + const auth = useAuth() + + if (!auth) { + return ( + <> + + Sign In + + + ) + } + + return <>{typeof children == 'function' ? children(auth) : children} +} diff --git a/packages/app/features/auth/my-account/screen.tsx b/packages/app/features/auth/my-account/screen.tsx index e69de29..0d65505 100644 --- a/packages/app/features/auth/my-account/screen.tsx +++ b/packages/app/features/auth/my-account/screen.tsx @@ -0,0 +1,25 @@ +import { Text, View } from 'dripsy' +import { signOut } from '../firebase' +import { AuthGate } from '../gate' + +export default function MyAccountScreen() { + return ( + + {({ uid }) => ( + + Welcome, {uid}. + + Sign Out + + + )} + + ) +} diff --git a/packages/app/layout/web/index.tsx b/packages/app/layout/web/index.tsx index 6b34a66..a91fa54 100644 --- a/packages/app/layout/web/index.tsx +++ b/packages/app/layout/web/index.tsx @@ -18,12 +18,17 @@ const tabs: Array<{ isActive: (pathname) => pathname.startsWith('/users'), name: 'Users', }, + { + pathname: '/account', + isActive: (pathname) => pathname.startsWith('/account'), + name: 'My Account', + }, ] const height = 34 // this will only run on Web -export function WebLayout({ children }) { +export function WebLayout({ children }: { children: React.ReactNode }) { const { pathname } = useRouter() return ( <>