Skip to content

Commit

Permalink
fix: jwt - expiresIn to ms
Browse files Browse the repository at this point in the history
  • Loading branch information
luizchaves committed Oct 12, 2024
1 parent c93c7d0 commit 3445897
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion public/codes/expressjs/invest-app-auth/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ router.post('/signin', async (req, res) => {
const token = jwt.sign(
{ userId },
process.env.JWT_SECRET,
{ expiresIn: 3600 } // 1h
{ expiresIn: 3600000 } // 1h
);

return res.json({ auth: true, token });
Expand Down
2 changes: 1 addition & 1 deletion public/codes/expressjs/invest-app-email/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ router.post(
const token = jwt.sign(
{ userId },
process.env.JWT_SECRET,
{ expiresIn: 3600 } // 1h
{ expiresIn: 3600000 } // 1h
);

return res.json({ auth: true, token });
Expand Down
2 changes: 1 addition & 1 deletion public/codes/expressjs/invest-app-upload/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ router.post(
const token = jwt.sign(
{ userId },
process.env.JWT_SECRET,
{ expiresIn: 3600 } // 1h
{ expiresIn: 3600000 } // 1h
);

return res.json({ auth: true, token });
Expand Down
2 changes: 1 addition & 1 deletion public/codes/expressjs/invest-app-validation/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ router.post(
const token = jwt.sign(
{ userId },
process.env.JWT_SECRET,
{ expiresIn: 3600 } // 1h
{ expiresIn: 3600000 } // 1h
);

return res.json({ auth: true, token });
Expand Down
7 changes: 5 additions & 2 deletions src/content/classnotes/expressjs/auth/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jwt.sign(
jwt.sign(
{ userId: 1 },
'secret',
{ expiresIn: 3600 } // 1h
{ expiresIn: 3600000 } // 1h
);
//=> eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImlhdCI6MTcyODc0Njc4NiwiZXhwIjoxNzI4NzUwMzg2fQ.V1eHtVYWaI5Rji8wd4onYIGqdTGlm6NAmUeIiw6G7Gw

Expand All @@ -66,7 +66,7 @@ jwt.sign(
jwt.sign(
{ userId: 1 },
'abc',
{ expiresIn: 3600 } // 1h
{ expiresIn: 3600000 } // { expiresIn: '1h' }
);
//=> eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImlhdCI6MTcyODc0ODQ5NywiZXhwIjoxNzI4NzUyMDk3fQ.0m_bFbC337WRu0bqlabUJ1hN-hNwOXVJUHKBbSdmz7s

Expand All @@ -83,6 +83,7 @@ Observações:

- `iat` (`Issued At` ou `Gerado em`) é o timestamp de quando o token foi gerado;
- A `signature` é gerado pela assinatura do `header.payload` + `privateKey` ([HMAC-SHA256 Hash Generator](https://www.devglan.com/online-tools/hmac-sha256-online));
- expiresIn: expressed in seconds or a string describing a time span [vercel/ms](https://github.com/vercel/ms).

Decode: jwt.verify(token, privateKey)

Expand Down Expand Up @@ -116,6 +117,8 @@ jwt.verify(
//=> { userId: 1, iat: 1728746786, exp: 1728750386 }
```

> Sabendo que o `privateKey` é `secret`, gere um novo token para o usuário de `id` igual a `2` com `1h` de expiração.
## Middleware

```text
Expand Down

0 comments on commit 3445897

Please sign in to comment.