Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/expo router #101

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ apps/*/credentials.json
apps/*/build
packages/*/build

apps/*/dist

# Turborepo
.turbo

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ To run the repository locally, run these two commands:
- `$ pnpm install` - This installs all required Node libraries using [pnpm](https://pnpm.io/).
- `$ pnpm dev` - Starts the development servers for all **apps**.

In Expo SDK 49+, you can switch between Expo Go and an expo-dev-client development build by pressing "s" after expo start. This respository doesn't require the expo-dev-client, but because it is installed expo start defaults to it. If you want to use the expo-dev-client with plugins, you must first build and install the development build on your device or simulator by runing `pnpm ios -d` or `pnpm android -d`.

### Commands

Because this monorepo uses [Turborepo](https://turbo.build/repo), you don't need to run additional commands to set things up. Whenever you run `$ pnpm build`, it will build all **packages** if they aren't built yet. In this monorepo we use a few commands or pipelines:
Expand Down
22 changes: 0 additions & 22 deletions apps/mobile/App.tsx

This file was deleted.

9 changes: 7 additions & 2 deletions apps/mobile/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"expo": {
"name": "expo-monorepo",
"slug": "expo-monorepo",
"scheme": "expo-monorepo",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
Expand Down Expand Up @@ -29,7 +30,11 @@
}
},
"web": {
"favicon": "./assets/favicon.png"
}
"favicon": "./assets/favicon.png",
"bundler": "metro"
},
"plugins": [
"expo-router"
]
}
}
11 changes: 11 additions & 0 deletions apps/mobile/app/(auth)/sign-in.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Text, View } from 'react-native';
import { useAuth } from '@acme/app-mobile/context/auth';

export default function SignIn() {
const { signIn } = useAuth();
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text onPress={() => signIn()}>Sign In</Text>
</View>
);
}
11 changes: 11 additions & 0 deletions apps/mobile/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Slot } from 'expo-router';
import { Provider } from '@acme/app-mobile/context/auth';

export default function Root() {
return (
// Setup the auth context and render our layout inside of it.
<Provider>
<Slot />
</Provider>
);
}
12 changes: 12 additions & 0 deletions apps/mobile/app/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Text, View } from 'react-native';

import { useAuth } from '@acme/app-mobile/context/auth';

export default function Index() {
const { signOut } = useAuth();
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text onPress={() => signOut()}>Sign Out</Text>
</View>
);
}
1 change: 1 addition & 0 deletions apps/mobile/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: ['expo-router/babel'],
};
};
47 changes: 47 additions & 0 deletions apps/mobile/context/auth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { router, useSegments } from 'expo-router';
import React from 'react';

const AuthContext = React.createContext(null);

// This hook can be used to access the user info.
export function useAuth() {
return React.useContext(AuthContext);
}

// This hook will protect the route access based on user authentication.
function useProtectedRoute(user) {
const segments = useSegments();

React.useEffect(() => {
const inAuthGroup = segments[0] === '(auth)';

if (
// If the user is not signed in and the initial segment is not anything in the auth group.
!user &&
!inAuthGroup
) {
// Redirect to the sign-in page.
router.replace('/sign-in');
} else if (user && inAuthGroup) {
// Redirect away from the sign-in page.
router.replace('/');
}
}, [user, segments]);
}

export function Provider(props) {
const [user, setAuth] = React.useState(null);

useProtectedRoute(user);

return (
<AuthContext.Provider
value={{
signIn: () => setAuth({}),
signOut: () => setAuth(null),
user,
}}>
{props.children}
</AuthContext.Provider>
);
}
11 changes: 3 additions & 8 deletions apps/mobile/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import { registerRootComponent } from 'expo';

import App from './App';

// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in the Expo client or in a native build,
// the environment is set up appropriately
registerRootComponent(App);
import 'expo-dev-client';
import 'react-native-gesture-handler';
import 'expo-router/entry';
16 changes: 12 additions & 4 deletions apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@
"scripts": {
"dev": "expo start",
"lint": "eslint --ext js,ts,tsx .",
"prebuild": "expo prebuild",
"build": "expo export --output-dir ./build --platform all",
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"android": "expo run:android",
"ios": "expo run:ios",
"web": "expo start --web",
"eas-build-pre-install": "npm install --global [email protected]",
"eas-build-post-install": "pnpm run -w build:mobile"
},
"dependencies": {
"@acme/feature-home": "*",
"expo": "^49.0.2",
"expo": "^49.0.8",
"expo-constants": "~14.4.2",
"expo-dev-client": "~2.4.5",
"expo-linking": "^5.0.2",
"expo-router": "2.0.4",
"expo-splash-screen": "~0.20.5",
"expo-status-bar": "~1.6.0",
"expo-updates": "~0.18.9",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.72.1",
"react-native": "0.72.3",
"react-native-gesture-handler": "~2.12.0",
"react-native-safe-area-context": "4.6.3",
"react-native-screens": "~3.22.0",
"react-native-web": "~0.19.6"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions apps/mobile/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"jsx": "react",
"strict": true
}
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
"test": "turbo test",
"build": "turbo build",
"build:mobile": "turbo build --filter=\"...{./apps/mobile}\"",
"build:web": "turbo build --filter=\"...{./apps/web}\""
"build:web": "turbo build --filter=\"...{./apps/web}\"",
"clean": "turbo clean:modules && turbo clean:apps && pnpm install",
"clean:modules": "rm -rf node_modules && rm -rf apps/*/node_modules && rm -rf packages/*/node_modules",
"clean:apps": "rm -rf apps/*/ios && rm -rf apps/*/android && rm -rf apps/*/.expo"
},
"devDependencies": {
"turbo": "^1.10.7",
"typescript": "^4.9.5"
"typescript": "^5.1.3"
},
"pnpm": {
"peerDependencyRules": {
Expand Down
2 changes: 0 additions & 2 deletions packages/feature-home/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
"babel-preset-expo": "~9.5.0",
"jest": "^29.4.3",
"jest-expo": "^49.0.0",
"react": "18.2.0",
"react-native": "0.72.1",
"react-test-renderer": "18.2.0",
"tsup": "^6.5.0"
},
Expand Down
2 changes: 0 additions & 2 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
"babel-preset-expo": "~9.5.0",
"jest": "^29.4.3",
"jest-expo": "^49.0.0",
"react": "18.2.0",
"react-native": "0.72.1",
"react-test-renderer": "18.2.0",
"tsup": "^6.5.0"
},
Expand Down
Loading