You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/auth-flow.md
+37-38
Original file line number
Diff line number
Diff line change
@@ -35,39 +35,6 @@ We can configure different screens to be available based on some condition. For
35
35
<TabsgroupId="config"queryString="config">
36
36
<TabItemvalue="static"label="Static"default>
37
37
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*asReactfrom'react';
51
-
52
-
constSignInContext=React.createContext();
53
-
```
54
-
55
-
Then we can implement the `useIsSignedIn` and `useIsSignedOut` hooks as follows:
56
-
57
-
```js
58
-
functionuseIsSignedIn() {
59
-
constisSignedIn=React.useContext(SignInContext);
60
-
return isSignedIn;
61
-
}
62
-
63
-
functionuseIsSignedOut() {
64
-
constisSignedIn=React.useContext(SignInContext);
65
-
return!isSignedIn;
66
-
}
67
-
```
68
-
69
-
We'll discuss how to expose the context value later.
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.
213
182
214
183
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
219
188
220
189
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.
221
190
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*asReactfrom'react';
204
+
205
+
constSignInContext=React.createContext();
206
+
```
207
+
208
+
Then we can implement the `useIsSignedIn` and `useIsSignedOut` hooks as follows:
209
+
210
+
```js
211
+
functionuseIsSignedIn() {
212
+
constisSignedIn=React.useContext(SignInContext);
213
+
return isSignedIn;
214
+
}
215
+
216
+
functionuseIsSignedOut() {
217
+
constisSignedIn=React.useContext(SignInContext);
218
+
return!isSignedIn;
219
+
}
220
+
```
221
+
222
+
We'll discuss how to expose the context value later.
0 commit comments