Options
The top level of the auth options in nuxt.config.js looks like this:
{
auth: {
endpoints: object,
headers: object,
token: object,
refreshToken: object,
redirect: object,
cookie: object,
middleware: object,
rewriteRedirects: boolean,
routes: object,
debug: boolean,
plugins: string[],
useGlobalFetch: boolean,
useI18n: boolean,
}
}headers
An object containing the headers to be sent with $fetch requests.
This option is useful when you need to send headers with every request, for example, to send an client id, or to set the content type.
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'max-age=604800',
}endpoints
An object containing information about the endpoints for authentication.
baseUrl
endpoints: {
baseUrl: process.env.NUXT_PUBLIC_API_BASE_URL,
}The base URL of the API used for authentication.
login
endpoints: {
login: {
url: '/login',
method: 'POST',
property: '',
headers: undefined,
}
}An object containing information about the login API.
url: Path of the login API.method: Method of the login API.property: The name of the property containing the response data from the API.headers: The headers to be sent with the login request.
For example, if your login API has a response object like this:
{
"data": {
"tokens": {
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
}Then you need to set property to data.tokens.
logout
endpoints: {
logout: {
url: '/logout',
method: 'DELETE',
headers: undefined
}
}An object containing information about the logout API.
url: Path of the logout API.method: Method of the logout API.headers: The headers to be sent with the logout request.
refresh
endpoints: {
refresh: {
url: '/refresh_tokens',
method: 'POST',
property: '',
headers: undefined
}
}An object containing information about the refresh token API.
url: Path of the refresh token API.method: Method of the refresh token API.property: The name of the property containing the response data from the API.headers: The headers to be sent with the refresh token request.
For example, if your refresh tokens API have a response object like this:
{
"data": {
"tokens": {
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
}Then you need to set property to data.tokens.
user
endpoints: {
user: {
url: '/me',
method: 'GET',
property: '',
headers: undefined
}
}An object containing information about the fetch user info API.
url: Path of the fetch user info API.method: Method of the fetch user info API.property: The name of the property containing the response data from the API.headers: The headers to be sent with the fetch user info request.
For example, if your fetch user info API has a response object like this:
{
"data": {
"id": 1,
"name": "John Doe",
"email": "user@example.com"
}
}Then you need to set property to data.
token
token: {
headerName: 'Authorization',
type: 'Bearer',
property: 'token',
maxAge: 365 * 24 * 60 * 60, // 1 year
}headerNameThe name of the header containing the token.typeThe type of token.propertyThe name of the token key in the response object from the login and refresh token API. In the example above,propertyhas a value oftoken.maxAgeThe maximum age of the token in seconds. If the token is a JWT, the module will use theexpclaim to calculate the expiration time. If the token is not a JWT, the module will use thismaxAgevalue or thecookie.maxAgevalue to calculate the expiration time.
refreshToken
refreshToken: {
type: 'param',
paramName: 'token',
property: 'refresh_token',
maxAge: 365 * 24 * 60 * 60, // 1 year
}typeis'param'by default, which means the refresh token will be sent in the request body as a parameter. If you want to attach the refresh token to the request headers, set a different value for the token type (e.g.Bearer).paramNameThe name of the parameter/header containing the refresh token in the request body/headers.propertyThe name of the property containing the refresh token in the response object from the login and refresh token API. In the example above,propertyhas a value ofrefresh_token.maxAgeThe maximum age of the refresh token in seconds. If the refresh token is a JWT, the module will use theexpclaim to calculate the expiration time. If the refresh token is not a JWT, the module will use thismaxAgevalue or thecookie.maxAgevalue to calculate the expiration time.
TIP
This module requires the login and refresh token API to return the same property containing the refresh token.
cookie
cookie: {
ssl: false,
maxAge: 365 * 24 * 60 * 60, // 1 year
domain: '',
path: '/',
}sslSettrueto enableSecureflag for cookies (recommended if possible).maxAgeThe maximum age of the cookie in seconds. The default value is 1 year. Make sure the value is larger than themaxAgeof both access token and refresh token.domainThe domain of the cookie. If not set, the cookie will be set to the domain of the current page.pathThe path of the cookie. The default value is/.
middleware
middleware: {
global: true,
}globalset tofalseto disable the built-in middlewareauththat handles authentication between pages.
redirect
redirect: {
login: '/login',
logout: '/',
home: '/',
}loginThe path to redirect to when the user is not logged in but trying to access pages that have metaauth = true.logoutThe path to redirect to after the user logs out.homeThe path to redirect to after the user logs in. This is also the path to redirect to when the user is logged in but trying to access pages that have metaauth = 'guest'. Note thatrewriteRedirectswill override this path.
rewriteRedirects
rewriteRedirects: boolean = true;Set true to rewrite the home redirect path to the path of the page that the user was trying to access before being redirected to the login page.
routes
Register authentication pages including login and logout. By default, these pages are located at /login and /logout. You can re-register the path for these pages here.
login
routes: {
login: {
name: 'login',
path: '/login',
file: resolve(__dirname, './pages/login.vue'),
}
}nameThe name of the route.fileThe path to the page component file.pathThe path on the URL.
logout
routes: {
logout: {
name: 'logout',
path: '/logout',
file: resolve(__dirname, './pages/logout.vue'),
}
}nameThe name of the route.fileThe path to the page component file.pathThe path on the URL.
debug
debug: boolean = false;Set true to enable debug mode. This will log the module's internal state to the console.
plugins
plugins: string[] = [];An array of paths to the plugins that you want to use with this module.
useGlobalFetch
useGlobalFetch: boolean = true;By default, this module sets up a global fetch function to use with the $fetch composable. Set useGlobalFetch = false to disable this feature. You still can use the $fetch instance of the module through $auth.$fetch().
const $auth = useNuxtApp();
const { $fetch } = $auth;
$fetch('https://example.com/api/users');useI18n
useI18n: boolean = false;Set useI18n = true if you have integrated the nuxtjs/i18n module in your project. When you do this, the paths for redirects will be created through the useLocalePath and useLocaleRoute compositions of i18n, helping to localize the paths when redirecting.