Skip to content

Commit

Permalink
3️⃣ 3.2 My Account Screen + AuthGate
Browse files Browse the repository at this point in the history
  • Loading branch information
nandorojo committed Oct 7, 2022
1 parent 34af7f2 commit 76e6f3e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/next/pages/account.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'app/features/auth/my-account/screen'
2 changes: 1 addition & 1 deletion packages/app/features/auth/firebase/init.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions packages/app/features/auth/firebase/init.web.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// 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'

let auth: ReturnType<typeof initializeAuth>

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',
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions packages/app/features/auth/gate/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<View
sx={{
bg: '$background',
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}}
>
<Text onPress={signInAnonymously}>Sign In</Text>
</View>
</>
)
}

return <>{typeof children == 'function' ? children(auth) : children}</>
}
25 changes: 25 additions & 0 deletions packages/app/features/auth/my-account/screen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Text, View } from 'dripsy'
import { signOut } from '../firebase'
import { AuthGate } from '../gate'

export default function MyAccountScreen() {
return (
<AuthGate>
{({ uid }) => (
<View
sx={{
flex: 1,
bg: '$background',
alignItems: 'center',
justifyContent: 'center',
}}
>
<Text>Welcome, {uid}.</Text>
<Text sx={{ mt: '$3', color: '#F04F88' }} onPress={signOut}>
Sign Out
</Text>
</View>
)}
</AuthGate>
)
}
7 changes: 6 additions & 1 deletion packages/app/layout/web/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand Down

0 comments on commit 76e6f3e

Please sign in to comment.