1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

node js で JWT をエンコード・デコードしてみる

Last updated at Posted at 2022-12-09

コード例

const jwt = require('jsonwebtoken')

const payload = {
  id: 333,
  name: "Alice",
  email: "example@example.com"
}

const token = jwt.sign(payload,'secret_signature')
console.log(token)

var decoded = jwt.verify(token, 'secret_signature');
console.log(decoded)

結果例

実行するたびにトークンは変わる(ドットで区切られた最後のゾーンが変わる)がデコード内容は同一になる。
ただしiatの値も毎回違う。

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MzMzLCJuYW1lIjoiQWxpY2UiLCJlbWFpbCI6ImV4YW1wbGVAZXhhbXBsZS5jb20iLCJpYXQiOjE2NzA1OTk2NjB9.uC438ol9Ac4KZX-3Bj9OW2HQxY05dTXHUdxAmovFw3Y

{
  id: 333,
  name: 'Alice',
  email: 'example@example.com',
  iat: 1670599660
}
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MzMzLCJuYW1lIjoiQWxpY2UiLCJlbWFpbCI6ImV4YW1wbGVAZXhhbXBsZS5jb20iLCJpYXQiOjE2NzA1OTk2NzZ9.sfR46mweeDxBbpZ5Gkr21Dmi9fnX7Gu4A7hjixlyLfo
{
  id: 333,
  name: 'Alice',
  email: 'example@example.com',
  iat: 1670599676
}

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?