Skip to content

Commit 8e63f10

Browse files
committed
2️⃣ Chapter 2.3: Unify Native Stack
1 parent 0b98a68 commit 8e63f10

11 files changed

Lines changed: 832 additions & 91 deletions

File tree

apps/expo/app/(tabs)/(home).tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
import { NativeStack as Stack } from 'expo-router'
21
import { useDripsyTheme } from 'dripsy'
2+
import { Stack } from '../../src/stack'
33

4-
export default function MyStack({ children }) {
5-
const { colors } = useDripsyTheme().theme
4+
export default function HomeTab({ children }) {
65
return (
7-
<Stack
8-
screenOptions={{
9-
headerStyle: {
10-
backgroundColor: colors.$background2,
11-
},
12-
headerTitleStyle: {
13-
color: colors.$text,
14-
},
15-
headerTintColor: '#D864D8',
16-
headerShadowVisible: false,
17-
}}
18-
>
6+
<Stack>
197
<Stack.Screen
208
name="index"
219
options={{

apps/expo/app/(tabs)/users.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
import { NativeStack as Stack } from 'expo-router'
21
import { useDripsyTheme } from 'dripsy'
2+
import { Stack } from '../../src/stack'
33

4-
export default function MyStack({ children }) {
5-
const { colors } = useDripsyTheme().theme
4+
export default function UserTab({ children }) {
65
return (
7-
<Stack
8-
screenOptions={{
9-
headerStyle: {
10-
backgroundColor: colors.$background2,
11-
},
12-
headerTitleStyle: {
13-
color: colors.$text,
14-
},
15-
headerTintColor: '#D864D8',
16-
headerShadowVisible: false,
17-
}}
18-
>
6+
<Stack>
197
<Stack.Screen
208
name="index"
219
options={{

apps/expo/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"dependencies": {
3+
"@react-native-firebase/app": "^15.7.0",
4+
"@react-native-firebase/auth": "^15.7.0",
35
"app": "*",
46
"expo": "46.0.13",
57
"expo-router": "^0.0.25",

apps/expo/src/stack.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { NativeStack } from 'expo-router'
2+
import { useDripsyTheme } from 'dripsy'
3+
4+
export function Stack({ children }) {
5+
const { colors } = useDripsyTheme().theme
6+
return (
7+
<NativeStack
8+
screenOptions={{
9+
headerStyle: {
10+
backgroundColor: colors.$background2,
11+
},
12+
headerTitleStyle: {
13+
color: colors.$text,
14+
},
15+
headerTintColor: '#D864D8',
16+
headerShadowVisible: false,
17+
}}
18+
>
19+
{children}
20+
</NativeStack>
21+
)
22+
}
23+
24+
Stack.Screen = NativeStack.Screen
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Constants from 'expo-constants'
2+
import { Firebase } from './types'
3+
4+
const isExpoGo = Constants.appOwnership == 'expo'
5+
6+
let exports: Firebase
7+
8+
// here we check if the user has properly set up their native app
9+
// if not, we fall back to firebase JS
10+
11+
if (isExpoGo) {
12+
exports = require('./init.web')
13+
} else {
14+
exports = require('./init.native')
15+
}
16+
17+
module.exports = exports
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './init.web'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import auth from '@react-native-firebase/auth'
2+
import { Firebase } from './types'
3+
4+
const getIsSignedIn: Firebase['getIsSignedIn'] = () =>
5+
Boolean(auth().currentUser)
6+
7+
const signOut: Firebase['signOut'] = () => auth().signOut()
8+
9+
const signInAnonymously: Firebase['signInAnonymously'] = async () => {
10+
return (await auth().signInAnonymously()).user
11+
}
12+
13+
const onIdTokenChanged: Firebase['onIdTokenChanged'] = (callback) => {
14+
return auth().onIdTokenChanged(callback)
15+
}
16+
17+
export { getIsSignedIn, signOut, signInAnonymously, onIdTokenChanged }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type * as firebase from 'firebase/auth'
2+
3+
export type Firebase = {
4+
getIsSignedIn: () => boolean
5+
signInAnonymously: () => Promise<Pick<firebase.User, 'uid'>>
6+
signOut: () => Promise<void>
7+
onIdTokenChanged: (
8+
callback: (user: { uid: string } | null) => void
9+
) => () => void
10+
}

packages/app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"dripsy": "^3.6.0",
77
"expo-linking": "^3.0.0",
8+
"firebase": "^9.11.0",
89
"moti": "0.19.0-alpha.6",
910
"react-native-reanimated": "2.9.1",
1011
"solito": "latest"

0 commit comments

Comments
 (0)