Skip to content

Commit 4c1f4d3

Browse files
author
Sam Vitare
authored
Update part4d.md
Added more clarification.
1 parent d34df2b commit 4c1f4d3

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/content/4/en/part4d.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The principles of token-based authentication are depicted in the following seque
1717

1818
- User starts by logging in using a login form implemented with React
1919
- We will add the login form to the frontend in [part 5](/en/part5)
20-
- This causes the React code to send the username and the password to the server address <i>/api/login</i> as a HTTP POST request.
20+
- This causes the React code to send the username and the password to the server address <i>/api/login</i> as an HTTP POST request.
2121
- If the username and the password are correct, the server generates a <i>token</i> that somehow identifies the logged-in user.
2222
- The token is signed digitally, making it impossible to falsify (with cryptographic means)
2323
- The backend responds with a status code indicating the operation was successful and returns the token with the response.
@@ -31,7 +31,7 @@ Let's first implement the functionality for logging in. Install the [jsonwebtoke
3131
npm install jsonwebtoken
3232
```
3333

34-
The code for login functionality goes to the file controllers/login.js.
34+
The code for login functionality goes to the file <i>controllers/login.js</i>.
3535

3636
```js
3737
const jwt = require('jsonwebtoken')
@@ -69,14 +69,34 @@ module.exports = loginRouter
6969
```
7070

7171
The code starts by searching for the user from the database by the <i>username</i> attached to the request.
72+
73+
```js
74+
const user = await User.findOne({ username })
75+
```
76+
7277
Next, it checks the <i>password</i>, also attached to the request.
78+
79+
```js
80+
const passwordCorrect = user === null
81+
? false
82+
: await bcrypt.compare(password, user.passwordHash)
83+
```
84+
7385
Because the passwords themselves are not saved to the database, but <i>hashes</i> calculated from the passwords, the _bcrypt.compare_ method is used to check if the password is correct:
7486

7587
```js
76-
await bcrypt.compare(body.password, user.passwordHash)
88+
await bcrypt.compare(password, user.passwordHash)
7789
```
7890

79-
If the user is not found, or the password is incorrect, the request is responded to with the status code [401 unauthorized](https://www.rfc-editor.org/rfc/rfc9110.html#name-401-unauthorized). The reason for the failure is explained in the response body.
91+
If the user is not found, or the password is incorrect, the request is responded with the status code [401 unauthorized](https://www.rfc-editor.org/rfc/rfc9110.html#name-401-unauthorized). The reason for the failure is explained in the response body.
92+
93+
```js
94+
if (!(user && passwordCorrect)) {
95+
return response.status(401).json({
96+
error: 'invalid username or password'
97+
})
98+
}
99+
```
80100

81101
If the password is correct, a token is created with the method _jwt.sign_. The token contains the username and the user id in a digitally signed form.
82102

@@ -95,6 +115,12 @@ The value for the environment variable must be set in the <i>.env</i> file.
95115

96116
A successful request is responded to with the status code <i>200 OK</i>. The generated token and the username of the user are sent back in the response body.
97117

118+
```js
119+
response
120+
.status(200)
121+
.send({ token, username: user.username, name: user.name })
122+
```
123+
98124
Now the code for login just has to be added to the application by adding the new router to <i>app.js</i>.
99125

100126
```js
@@ -143,7 +169,7 @@ In practice, this means that if the token is, for example, the string <i>eyJhbGc
143169
Bearer eyJhbGciOiJIUzI1NiIsInR5c2VybmFtZSI6Im1sdXVra2FpIiwiaW
144170
</pre>
145171

146-
Creating new notes will change like so:
172+
Creating new notes will change like so (<i>controllers/notes.js</i>):
147173

148174
```js
149175
const jwt = require('jsonwebtoken') //highlight-line

0 commit comments

Comments
 (0)