[关闭]
@duanyubin 2017-05-24T07:51:09.000000Z 字数 638 阅读 252

JSONWEBTOKEN

NODE


What can jsonwebtoken do

LOGIN

Composition

HEADER.PAYLOAD.VERIFY SIGNATURE

structure

ALGORITHM & TOKEN TYPE

PAYLOAD

DATA

VERIFY SIGNATURE

signature of two fields above

Advantage

No need for cookies

No need for sharing session

Securely

Easy to use

How to use

Client request

  1. POST /login HTTP/1.1
  2. ...
  3. name=ybudan&password=123456

Server verify and generate signature

  1. const person = await DB.model('person').find({ where: { email, password } })
  2. if (person) {
  3. ctx.body = await sign({ DATA }, 'SECRET KEY', {
  4. expiresIn: '7 days'
  5. })
  6. }

Client receive response and save token in localStorage

Everytime reopen the website, take token from localStorage and decode it

  1. const token = localStorage.getItem('token')
  2. const authToken = decode(token)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注