- an open standard defining a compact and self-contained way for securely transmitting information between parties as a JSON object.
- stateless sessions (load balancers, separated front and back-end)
- easy logout
- save on SQL queries to check user or permissions every request
- server endpoint to create a token on auth
- client store token in localStorage (or cookie)
- client sends token back with every request
- server decodes token and lets request through
- server can use info in payload to make decision on authorization
- they are not for use in the client, only the API needs the info
- you should have some other way of checking permissions of the user
- front end AND api should both be checking for permissions
- Example
{
header: {
typ: "JWT",
alg: "HS256"
},
payload: {
whatever: 2,
something: 'hello'
},
signature: HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret
)
}
token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ