As we mentioned previously, jsonwebtoken can be used to generate JWTs on the server side. You can use this module synchronously or asynchronously in the following simplified steps:
- Install jsonwebtoken via npm:
$ npm i jsonwebtoken
- Import and sign a token on the server side:
import jwt from 'jsonwebtoken'
var token = jwt.sign({ name: 'john' }, 'secret', { expiresIn: '1h' })
- Asynchronously verify the token coming from the client side:
try {
var verified = jwt.verify(token, 'secret')
} catch(err) {
// handle error
}
If you want to find out more information about this module, please visit https://github.com/brianloveswords/node-jws.
So, now you have a basic understanding of session-based and token-based authentication, we will guide you on how to apply them in server-side and client-side apps that use Koa and Nuxt. In this chapter, we will use token-based authentication to create two authentication options...