Skip to content

Commit 0066e81

Browse files
committed
Improve authentication flow docs
1 parent ddfbe48 commit 0066e81

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,40 @@ Most apps require that a user authenticate in some way to have access to data as
1717

1818
This is the behavior that we want from the authentication flow: when users sign in, we want to throw away the state of the authentication flow and unmount all of the screens related to authentication, and when we press the hardware back button we expect to not be able to go back to the authentication flow.
1919

20-
## Conditionally define our screens
20+
## How it will work
2121

22-
In our navigator, we can conditionally render appropriate screens. For our case, let's say we have 3 screens:
22+
We can define different screens based on some condition. For example, if the user is signed in, we can define `Home`, `Profile`, `Settings` etc. If the user is not signed in, we can define `SignIn` and `SignUp` screens.
23+
24+
For example:
25+
26+
```js
27+
isSignedIn ? (
28+
<>
29+
<Stack.Screen name="Home" component={HomeScreen} />
30+
<Stack.Screen name="Profile" component={ProfileScreen} />
31+
<Stack.Screen name="Settings" component={SettingsScreen} />
32+
</>
33+
) : (
34+
<>
35+
<Stack.Screen name="SignIn" component={SignInScreen} />
36+
<Stack.Screen name="SignUp" component={SignUpScreen} />
37+
</>
38+
)
39+
```
40+
41+
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.
42+
43+
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.
44+
45+
The magic happens when the value of the `isSignedIn` variable changes. Let's say, initially `isSignedIn` is `false`. This means, either `SignIn` or `SignUp` screens are shown. After the user signs in, the value of `isSignedIn` will change to `true`. React Navigation will see that the `SignIn` and `SignUp` screens are no longer defined and so it will remove them. Then it'll show the `Home` screen automatically because that's the first screen defined when `isSignedIn` is `true`.
46+
47+
This takes advantage of a new feature in React Navigation: being able to dynamically define and alter the screen definitions of a navigator based on props or state. The example shows stack navigator, but you can use the same approach with any navigator.
48+
49+
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.
50+
51+
## Define our screens
52+
53+
In our navigator, we can conditionally define appropriate screens. For our case, let's say we have 3 screens:
2354

2455
- `SplashScreen` - This will show a splash or loading screen when we're restoring the token.
2556
- `SignInScreen` - This is the screen we show if the user isn't signed in already (we couldn't find a token).
@@ -62,8 +93,6 @@ The main thing to notice is that we're conditionally defining screens based on t
6293
- `SignIn` screen is only defined if `userToken` is `null` (user is not signed in)
6394
- `Home` screen is only defined if `userToken` is non-null (user is signed in)
6495

65-
This takes advantage of a new feature in React Navigation: being able to dynamically define and alter the screen definitions of a navigator based on props or state. The example shows stack navigator, but you can use the same approach with any navigator.
66-
6796
Here, we're conditionally defining one screen for each case. But you could also define multiple screens. For example, you probably want to define password reset, signup, etc screens as well when the user isn't signed in. Similarly for the screens accessible after sign in, you probably have more than one screen. We can use `React.Fragment` to define multiple screens:
6897

6998
```js
@@ -81,10 +110,10 @@ state.userToken == null ? (
81110
);
82111
```
83112

84-
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 logged in are "protected" and cannot be navigated to by other means if the user is not logged in.
85-
86113
## Implement the logic for restoring the token
87114

115+
> Note: The following is just an example of how you might implement the logic for authentication in your app. You don't need to follow it as is.
116+
88117
From the previous snippet, we can see that we need 3 state variables:
89118

90119
- `isLoading` - We set this to `true` when we're trying to check if we already have a token saved in `AsyncStorage`

0 commit comments

Comments
 (0)