Module CSRF for NUXT
Install the module
# NPM
npm install --save @privyid/nuxt-csrf
# Yarn
yarn add @privyid/nuxt-csrf
include in nuxt.config.js
// nuxt.config.js
{
modules: [
// Simple usage
'@privyid/nuxt-csrf',
// With options
['@privyid/nuxt-csrf', { /* module options */ }]
]
}
const token = this.$csrfToken()
const token = context.app.$csrfToken()
If you using @nuxtjs/axios, this module must be registered before @nuxtjs/axios axios module.
// nuxt.config.js
{
modules: [
'@privyid/nuxt-csrf', // <-- must be placed before @nuxtjs/axios
'@nuxtjs/axios',
]
}
If you want axios automatically send CSRF Token in every request, you can add this into your axios's interceptors. If you not familiar with it before, goto this.
// ~/plugins/axios.js
export default function ({ $axios, app, redirect }) {
$axios.onRequest(config => {
const token = app.$csrfToken()
if (!config.headers['X-CSRF-Token'] && token)
config.headers['X-CSRF-Token'] = token
return config
})
}
Distributed under the MIT License. See LICENSE for more information.