nuxt-alt / auth

An alternative module to @nuxtjs/auth
https://nuxt-alt-auth.vercel.app/
MIT License
94 stars 20 forks source link

v3.0.3 #83

Closed Denoder closed 7 months ago

Denoder commented 7 months ago

Changes & Additions

Request Handler

There are cases where you will need to handle auth errors pertaining to the request rather than the auth module itself. For instance, when utilizing the local scheme and you have a non-expiring token, and you revoke the token (in your server) (for whatever reason), the token will still be there in the store manager.

With that said a new option resetOnResponseError has been introduced. This can be a Boolean or a Function. Similar to resetOnError, but this one is for the request handler. As a function it uses 3 arguments: error, auth, and scheme it would look something like this in your Nuxt config:

 auth: {
     //... module options
     resetOnResponseError: (error, auth, scheme) => {
        if (error.response.status === 401) {
            scheme.reset?.()
            auth.redirect('login')
        }
    },
}

The option is disabled by default.

Module Options

Functions within the module options were getting removed due to Nuxt turning them into strings thereby remove functions in the process. This will fix that issue.

Module Options: Redirects

The redirect options can now be defined as functions that returns strings, the function has access to the auth instance and the localePath instance (but this one isnt typed properly so you can use the auth instance to get to Nuxt's context for typing):

 auth: {
     //... module options
     redirect: {
         login: (auth, trans) => trans('/login'),
         logout: (auth, trans) => auth.ctx.$localePath('/login'),
         callback: () => '/login',
         home: '/'
     },
}

By default all redirect options goes through localePath if it's available, if you would like to prevent this, turn the property into a function and just return the string.