Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Commit 39ab7be

Browse files
committed
refining documentation.
1 parent 448dc11 commit 39ab7be

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# redux-http-basic-auth-example
1+
# Redux HTTP Basic Authentication
22

33
When an app communicates with a HTTP API, which enforces some form of authentication, the app typically follows these steps:
44

@@ -9,11 +9,9 @@ When an app communicates with a HTTP API, which enforces some form of authentica
99
5. If the token/ hash does not work during any of the subsequent API requests (401 - Unauthorized), we'll need to invalidate the hash/ token and prompt the user to log in again.
1010
6. Or, on failure (401 - Unauthorized): We display an error message to the user, prompting them re-enter their credentials.
1111

12-
Based on the work flow defined above we start our app by displaying a login form, _step 2_ kicks in when the user taps the login button... Dispatching the `login` action creator:
13-
1412
# Logging In #
1513

16-
Let's jump into some code:
14+
Based on the work flow defined above we start our app by displaying a login form, _step 2_ kicks in when the user taps the login button... Dispatching the `login` action creator, let's jump into some code...
1715

1816
```
1917
/// actions/user.js
@@ -45,9 +43,10 @@ export function login(username, password) {
4543
})
4644
.then(
4745
data => {
46+
// data = { authenticated: true, user: 'admin' }
4847
// We pass the `authentication hash` down to the reducer so that it
4948
// can be used in subsequent API requests.
50-
// data = { authenticated: true, user: 'admin' }
49+
5150
dispatch(loginSuccess(hash, data.user))
5251
},
5352
(response, data) => dispatch(loginFailure(data.error || 'Log in failed'))
@@ -63,7 +62,7 @@ The first thing we do is dispatch an action creator:
6362
```
6463
dispatch(loginRequest())
6564
```
66-
Which results in our state letting us know that the user `isLoggingIn`. We use this to display an activity indicator (_spinning wheel_), and to disable the log in button in our log in view.
65+
Which results in our store letting us know that the user `isLoggingIn`. We use this to display an activity indicator (_spinning wheel, "Loading...", etc._), and to disable the log in button in our log in view.
6766

6867
Next we base64 encode our credentials, and setup a fetch request.
6968
```
@@ -79,18 +78,18 @@ Everything went well, so we...
7978
```
8079
dispatch(loginSuccess(hash, data.user))
8180
```
82-
Our `LOGIN_SUCCESS` action results in the `hash` been stored in our state, which will be used in every subsequent request.
81+
Our `LOGIN_SUCCESS` action results in us having an authentication `hash` in our store, which we'll use in subsequent requests.
8382

8483
If something went wrong then we want to let the user know...
8584
```
8685
dispatch(loginFailure(data.error || 'Log in failed')
8786
```
8887

89-
_The `loginSuccess`, `loginFailure`, and `loginRequest` action creators are fairly generic and don't really warrant code samples. (See `actions/user.js`)_
88+
_The `loginSuccess`, `loginFailure`, and `loginRequest` action creators are fairly generic and don't really warrant code samples. (See `[actions/user.js](https://github.com/peterp/redux-http-basic-auth-example/blob/master/actions/user.js)`)_
9089

9190
### Reducer ###
9291

93-
Our reducer is fairly typical, simply updating our store for each action it receives.
92+
Our reducer is also typical:
9493

9594
```
9695
/// reducers/user.js
@@ -126,8 +125,9 @@ function user(state = {
126125

127126
# Subsequent API requests #
128127

129-
We now have an authentication hash in our store, which we can use in subsequent action creators on requests.
128+
Now that we have an authentication hash in our store we can use it in subsequent action creators, passing it in to requests.
130129

130+
In our example below we're fetching a list of friends for our authenticated user:
131131
```
132132
/// actions/friends.js
133133
export function fetchFriends() {
@@ -169,6 +169,7 @@ export function fetchFriends() {
169169

170170
You'll find that most API requests typically dispatches the same 3 actions as above: `API_REQUEST`, `API_SUCCESS`, and `API_FAILURE`, and as such the majority of the request/ response code can be pushed into [middleware](https://github.com/reactjs/redux/issues/99#issuecomment-112198579).
171171

172+
We fetch the hash authentication token from the store and setup the request.
172173
```
173174
const hash = getState().user.hash
174175
return fetch(`https://httpbin.org/get/friends/`, {
@@ -177,7 +178,8 @@ return fetch(`https://httpbin.org/get/friends/`, {
177178
}
178179
})
179180
```
180-
We fetch the hash authentication token from the store and setup the request. If our API requests with a 401 unauthorized status code then we've got to remove our hash from the store, and present the user with a log in view.
181+
182+
If the API response with a 401 unauthorized status code then we've got to remove our hash from the store, and present the user with a log in view again.
181183
```
182184
// did our request fail because our auth credentials aren't working?
183185
if (response.status == 401) {

0 commit comments

Comments
 (0)