The documentation says
resetOnError
Default: false
If enabled, user will be automatically logged out if any error happens. (For example when token expired)
Is this working? I have been checking the code and I don't see any code related to it
Could someone respond? It seems an interesting option but I tested and resetOnError: true has no effect.
I thought that option would automatically call $aut.logout() when the token is expired, to logout the user on the front-end side and send him to home or login page for example.
For the time being, I implemented a plugin to intercept response that has errors:
In ~/plugins/axios.js:
export default function ({$axios, app}) {
$axios.interceptors.response.use(
function (response) {
return response;
},
function (error) {
const code = parseInt(error.response && error.response.status);
if ([401, 403].includes(code)) {
app.$auth.logout();
}
return Promise.reject(error);
}
);
}
Then in ~/nuxt.config.js:
plugins: [
'~/plugins/axios'
],
@nitescent resetOnError is reimplemented. Please install directly from GitHub dev branch.
@ishitatsuyuki It's not working on my side. Or it's unclear for me how to use the option. Would you have an example?
What I did is disable my plugin (see my previous post) and set auth {resetOnError: true} in nuxt.config.js then I login and make an other request wihch has a response from the server with status code 401 but nothing happens.
I used some log in @nuxtjs/auth/lib/core/auth.js to see what's happening. 'log1' and 'log2' are printed but 'log3' is not printing when server returns a 401.
async init () {
console.debug('log1');
// Reset on error
if (this.options.resetOnError) {
console.debug('log2');
this.onError((...args) => {
console.debug('log3');
if (typeof (this.options.resetOnError) !== 'function' || this.options.resetOnError(...args)) {
this.reset()
}
})
}
Why no updates on this issue?
nuxt ^2.4.5
nuxtjs/auth ^4.5.3
Still not working.
I'd also love for this to be sorted out.
[email protected]
@nuxtjs/[email protected]
hello, i see that resetOnError works, but, it work only after 2 page refreshes. After 1st auth error it shows 401 error page and sets unlogged cookies, so after 2nd refresh page loades without problems...
I have the same behaviour like @andreynazarov3
Yes, the option leads to a different result and the token is removed after 2 page reloads... but that's not the expected behaviour.
Currently I don't know how to implement the requirement without workaround.
The issue is labeled as question, but it looks like a bug.
I have the same behaviour like @andreynazarov3
Yes, the option leads to a different result and the token is removed after 2 page reloads... but that's not the expected behaviour.
Currently I don't know how to implement the requirement without workaround.The issue is labeled as question, but it looks like a bug.
seems this property works only if redirect on logout is set:
redirect: {
// redirect here after logout
logout: 'index'
}
Thx, but that seems not to be enough. This is my config:
auth: {
strategies: {
local: {
endpoints: {
login: { url: '/api/login_check', method: 'post', propertyName: 'token' },
// logout: { url: '/api/auth/logout', method: 'post' },
user: { url: '/api/user', method: 'get', propertyName: '' }
}
}
},
redirect: {
login: '/login',
home: '/',
logout: '/'
},
resetOnError: true
}
BTW: "index" would not work
yeah, i use 'index' because i have localize routes before redirect.
i notice one difference with my config that i have logout endpoint to false:
strategies: {
local: {
endpoints: {
login: {
url: '/shop-api/login_check',
method: 'post',
propertyName: 'token'
},
logout: false,
user: {
url: '/shop-api/me',
method: 'get',
propertyName: false
}
}
Unfortunately that makes also no difference.
Currently not working :(
not working here :cry:
guys, i found what workaround i used for it
in plugins/axios.js i set special case for 401 error like
// plugins/axios.js
export default function(context) {
const { $axios, app } = context;
$axios.interceptors.response.use(
function(response) {
return response;
},
function(error) {
const code = parseInt(error.response && error.response.status);
if ([401, 403].includes(code)) {
app.$auth.logout();
}
return Promise.reject(error);
}
);
}
still having this error.
Tried setting resetOnError: true and nothing happened.
According to the document, it accepts function as well, so tried:
// nuxt.config.js
{
auth: {
// other configs...
resetOnError() {
console.log('here 123')
return true
},
},
}
When 401 happens, nothing is printed in the console.
Searching the source code and found its used in https://github.com/nuxt-community/auth-module/blob/dev/src/core/auth.ts#L35 but I tried setting up auth plugin to test the onError like this:
// plugins/auth.js
export default ({ $auth }) => {
console.log('auth plugin')
$auth.onError((error, name, endpoint) => {
console.log('')
console.log('Auth error')
console.log('error', error)
console.log('name', name)
console.log('endpoint', endpoint)
})
}
Can see auth plugin printed in console but when 401 happens, nothing is printed.
Seems like the problem is from the onError ?!?
Most helpful comment
Could someone respond? It seems an interesting option but I tested and
resetOnError: truehas no effect.I thought that option would automatically call $aut.logout() when the token is expired, to logout the user on the front-end side and send him to home or login page for example.
For the time being, I implemented a plugin to intercept response that has errors:
In ~/plugins/axios.js:
Then in ~/nuxt.config.js: