Skip to content

Commit 5c884b3

Browse files
committed
Move the full code example into the How it will work section where it belongs
1 parent 0d93eab commit 5c884b3

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

versioned_docs/version-7.x/auth-flow.md

+37-38
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,6 @@ We can configure different screens to be available based on some condition. For
3535
<Tabs groupId="config" queryString="config">
3636
<TabItem value="static" label="Static" default>
3737

38-
To do this, we need a couple of things:
39-
40-
1. Define two hooks: `useIsSignedIn` and `useIsSignedOut`, which return a boolean value indicating whether the user is signed in or not.
41-
2. Use the `useIsSignedIn` and `useIsSignedOut` along with the [`if`](static-configuration.md#if) property to define the screens that are available based on the condition.
42-
43-
This tells React Navigation to show specific screens based on the signed in status. When the signed in status changes, React Navigation will automatically show the appropriate screen.
44-
45-
## Define the hooks
46-
47-
To implement the `useIsSignedIn` and `useIsSignedOut` hooks, we can start by creating a context to store the authentication state. Let's call it `SignInContext`:
48-
49-
```js
50-
import * as React from 'react';
51-
52-
const SignInContext = React.createContext();
53-
```
54-
55-
Then we can implement the `useIsSignedIn` and `useIsSignedOut` hooks as follows:
56-
57-
```js
58-
function useIsSignedIn() {
59-
const isSignedIn = React.useContext(SignInContext);
60-
return isSignedIn;
61-
}
62-
63-
function useIsSignedOut() {
64-
const isSignedIn = React.useContext(SignInContext);
65-
return !isSignedIn;
66-
}
67-
```
68-
69-
We'll discuss how to expose the context value later.
70-
7138
```js name="Customizing tabs appearance" snack
7239
import * as React from 'react';
7340
import { View } from 'react-native';
@@ -82,6 +49,7 @@ const useIsSignedOut = () => {
8249
return false;
8350
};
8451

52+
// codeblock-focus-start
8553
const signedInStack = createNativeStackNavigator({
8654
screens: {
8755
Home: HomeScreen,
@@ -97,7 +65,6 @@ const signedOutStack = createNativeStackNavigator({
9765
},
9866
});
9967

100-
// codeblock-focus-start
10168
const RootStack = createNativeStackNavigator({
10269
screens: {
10370
LoggedIn: {
@@ -146,9 +113,8 @@ function SignUpScreen() {
146113
```
147114

148115
</TabItem>
149-
<TabItem value="dynamic" label="Dynamic">
150116

151-
For example:
117+
<TabItem value="dynamic" label="Dynamic">
152118

153119
```js name="Customizing tabs appearance" snack
154120
import * as React from 'react';
@@ -209,6 +175,9 @@ function SignUpScreen() {
209175
}
210176
```
211177

178+
</TabItem>
179+
</Tabs>
180+
212181
When we define screens like this, when `isSignedIn` is `true`, React Navigation will only see the `Home`, `Profile` and `Settings` screens, and when it's `false`, React Navigation will see the `SignIn` and `SignUp` screens. This makes it impossible to navigate to the `Home`, `Profile` and `Settings` screens when the user is not signed in, and to `SignIn` and `SignUp` screens when the user is signed in.
213182

214183
This pattern has been in use by other routing libraries such as React Router for a long time, and is commonly known as "Protected routes". Here, our screens which need the user to be signed in are "protected" and cannot be navigated to by other means if the user is not signed in.
@@ -219,8 +188,38 @@ The example shows stack navigator, but you can use the same approach with any na
219188

220189
By conditionally defining different screens based on a variable, we can implement auth flow in a simple way that doesn't require additional logic to make sure that the correct screen is shown.
221190

222-
</TabItem>
223-
</Tabs>
191+
To do this, we need a couple of things:
192+
193+
1. Define two hooks: `useIsSignedIn` and `useIsSignedOut`, which return a boolean value indicating whether the user is signed in or not.
194+
2. Use the `useIsSignedIn` and `useIsSignedOut` along with the [`if`](static-configuration.md#if) property to define the screens that are available based on the condition.
195+
196+
This tells React Navigation to show specific screens based on the signed in status. When the signed in status changes, React Navigation will automatically show the appropriate screen.
197+
198+
## Define the hooks
199+
200+
To implement the `useIsSignedIn` and `useIsSignedOut` hooks, we can start by creating a context to store the authentication state. Let's call it `SignInContext`:
201+
202+
```js
203+
import * as React from 'react';
204+
205+
const SignInContext = React.createContext();
206+
```
207+
208+
Then we can implement the `useIsSignedIn` and `useIsSignedOut` hooks as follows:
209+
210+
```js
211+
function useIsSignedIn() {
212+
const isSignedIn = React.useContext(SignInContext);
213+
return isSignedIn;
214+
}
215+
216+
function useIsSignedOut() {
217+
const isSignedIn = React.useContext(SignInContext);
218+
return !isSignedIn;
219+
}
220+
```
221+
222+
We'll discuss how to expose the context value later.
224223

225224
## Define our screens
226225

0 commit comments

Comments
 (0)