Skip to content

Commit e288d18

Browse files
Merge pull request #2735 from wxh06/master
Fix misleading response and TypeScript errors in authentication docs
2 parents 2a72046 + e132e04 commit e288d18

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

content/security/authentication.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class AuthService {
135135
this.usersService = usersService;
136136
}
137137

138-
async signIn(username, pass) {
138+
async signIn(username: string, pass: string) {
139139
const user = await this.usersService.findOne(username);
140140
if (user?.password !== pass) {
141141
throw new UnauthorizedException();
@@ -237,7 +237,7 @@ export class AuthService {
237237
if (user?.password !== pass) {
238238
throw new UnauthorizedException();
239239
}
240-
const payload = { username: user.username, sub: user.userId };
240+
const payload = { sub: user.userId, username: user.username };
241241
return {
242242
access_token: await this.jwtService.signAsync(payload),
243243
};
@@ -348,7 +348,7 @@ Let's go ahead and test our routes using cURL again. You can test with any of th
348348
```bash
349349
$ # POST to /auth/login
350350
$ curl -X POST http://localhost:3000/auth/login -d '{"username": "john", "password": "changeme"}' -H "Content-Type: application/json"
351-
$ # result -> {"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."}
351+
{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."}
352352
$ # Note: above JWT truncated
353353
```
354354

@@ -445,15 +445,15 @@ Ensure the app is running, and test the routes using `cURL`.
445445
```bash
446446
$ # GET /profile
447447
$ curl http://localhost:3000/auth/profile
448-
$ # result -> {"statusCode":401,"message":"Unauthorized"}
448+
{"statusCode":401,"message":"Unauthorized"}
449449

450450
$ # POST /auth/login
451451
$ curl -X POST http://localhost:3000/auth/login -d '{"username": "john", "password": "changeme"}' -H "Content-Type: application/json"
452-
$ # result -> {"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2Vybm... }
452+
{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2Vybm..."}
453453

454454
$ # GET /profile using access_token returned from previous step as bearer code
455455
$ curl http://localhost:3000/auth/profile -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2Vybm..."
456-
$ # result -> {"userId":1,"username":"john"}
456+
{"sub":1,"username":"john","iat":...,"exp":...}
457457
```
458458

459459
Note that in the `AuthModule`, we configured the JWT to have an expiration of `60 seconds`. This is too short an expiration, and dealing with the details of token expiration and refresh is beyond the scope of this article. However, we chose that to demonstrate an important quality of JWTs. If you wait 60 seconds after authenticating before attempting a `GET /auth/profile` request, you'll receive a `401 Unauthorized` response. This is because `@nestjs/jwt` automatically checks the JWT for its expiration time, saving you the trouble of doing so in your application.

0 commit comments

Comments
 (0)