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: src/content/4/en/part4d.md
+31-5Lines changed: 31 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ The principles of token-based authentication are depicted in the following seque
17
17
18
18
- User starts by logging in using a login form implemented with React
19
19
- 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.
21
21
- If the username and the password are correct, the server generates a <i>token</i> that somehow identifies the logged-in user.
22
22
- The token is signed digitally, making it impossible to falsify (with cryptographic means)
23
23
- 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
31
31
npm install jsonwebtoken
32
32
```
33
33
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>.
35
35
36
36
```js
37
37
constjwt=require('jsonwebtoken')
@@ -69,14 +69,34 @@ module.exports = loginRouter
69
69
```
70
70
71
71
The code starts by searching for the user from the database by the <i>username</i> attached to the request.
72
+
73
+
```js
74
+
constuser=awaitUser.findOne({ username })
75
+
```
76
+
72
77
Next, it checks the <i>password</i>, also attached to the request.
78
+
79
+
```js
80
+
constpasswordCorrect= user ===null
81
+
?false
82
+
:awaitbcrypt.compare(password, user.passwordHash)
83
+
```
84
+
73
85
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:
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
+
returnresponse.status(401).json({
96
+
error:'invalid username or password'
97
+
})
98
+
}
99
+
```
80
100
81
101
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.
82
102
@@ -95,6 +115,12 @@ The value for the environment variable must be set in the <i>.env</i> file.
95
115
96
116
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.
0 commit comments