Alternative Auth module for Nuxt
This module is meant as an alternative to @nuxtjs/auth, except this is for nuxt3 only with no backwards compatibility support.
- Add
@nuxt-alt/auth
and@nuxt-alt/http
dependency to your project
yarn add @nuxt-alt/auth @nuxt-alt/http
- Add
@nuxt-alt/auth
and@pinia/nuxt
to themodules
section ofnuxt.config.ts
Note: you dont need to specify @nuxt-alt/http
, it will automatically be added but if you want to manually add it, make sure it is below the auth module (and above the proxy module if you are using it). It also doesn't need pinia
it will use nuxt's useState
by default.
export default defineNuxtConfig({
modules: [
'@nuxt-alt/auth'
],
auth: {
/* module options */
}
});
The module now uses '@nuxt-alt/http' to function, that module extends ohmyfetch. Please note that if you were using data
to post data, you now need to use body
since this is what ohmyfetch
uses. If you intend to use ssr, please consider using the @nuxt-alt/proxy
module.
A useAuth()
composable is availale to use to access the auth methods.
Most of the options are taken directly from the @nuxtjs/auth module. In addition there are some extra options available.
- Type:
Boolean
- Default:
false
Enables/disables the middleware to be used globally.
- Type:
Boolean
- Default:
true
Enables/disables the built-in middleware.
- Type:
String
- Default:
auth
This is the namespace to use for nuxt useState.
- Type:
Boolean
- Default:
false
Enable this option to use the pinia store, bey default this is disabled and nuxt's useState
is used instead.
- Type:
String
- Default:
auth
This is the namespace to use for the pinia store.
- Type:
Boolean
- Default:
true
Enable this option to use the localStorage store.
- Type:
String
- Default:
auth.
This sets the localStorage prefix.
- Type:
Boolean
- Default:
true
Enable this option to use the sessionStorage store.
- Type:
String
- Default:
auth.
Similar to the localstorage option, this is the prefix for session storage.
- Type:
Boolean
- Default:
true
Enable this option to use the cookie storage.
- Type:
String
- Default:
auth.
Similar to the localstorage option, this is the prefix for the cookie storage.
- Type:
Object
- Default:
{ path: '/' }
The default cookie storage options.
- Type:
query | storage
- Default:
storage
The type of redirection strategy you want to use, storage
utilizng localStorage for redirects, query
utilizing the route query parameters.
- Type:
Boolean | Number
- Default:
false
This is experimental. If set to true, default interval is 1000ms, otherwise set time in milliseconds. This is how often the module with attempt to validate the token for expiry.
- Type:
Boolean | Function
- Default:
false
When enabled it will reset when there's a 401 error in any of the responses. You are able to turn this into a function to handle this yourself:
auth: {
//... module options
resetOnResponseError: (error, auth, scheme) => {
if (error.response.status === 401) {
scheme.reset?.()
auth.redirect('login')
}
},
}
The user information can be edited like so for TypeScript:
declare module '@nuxt-alt/auth' {
interface UserInfo {
email: string
name: string
}
}
In addition to Auth Tokens;
By default the $auth.strategy
getter uses the Scheme
type which does not have token
or refreshToken
property types. To help with this, a $auth.refreshStrategy
and a $auth.tokenStrategy
getter have been added for typing. They all do the same thing, this is just meant for type hinting.
Oauth2 now has client window authentication thanks to this pull request: nuxt-community/auth-module#1746
Properties have been changed to:
- Type:
Boolean
- Default:
false
Enable/disable the use of a popup for client authentication.
- Type:
Number
- Default:
400
The width of the client window.
- Type:
Number
- Default:
600
The width of the client window.
Available aliases to use within nuxt
#auth/runtime
#auth/utils
#auth/providers