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-5.x/auth-flow.md
+35-6Lines changed: 35 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,40 @@ Most apps require that a user authenticate in some way to have access to data as
17
17
18
18
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.
19
19
20
-
## Conditionally define our screens
20
+
## How it will work
21
21
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.
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:
23
54
24
55
-`SplashScreen` - This will show a splash or loading screen when we're restoring the token.
25
56
-`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
62
93
-`SignIn` screen is only defined if `userToken` is `null` (user is not signed in)
63
94
-`Home` screen is only defined if `userToken` is non-null (user is signed in)
64
95
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
-
67
96
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:
68
97
69
98
```js
@@ -81,10 +110,10 @@ state.userToken == null ? (
81
110
);
82
111
```
83
112
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
-
86
113
## Implement the logic for restoring the token
87
114
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
+
88
117
From the previous snippet, we can see that we need 3 state variables:
89
118
90
119
-`isLoading` - We set this to `true` when we're trying to check if we already have a token saved in `AsyncStorage`
0 commit comments