NUXT的替代验证模块
该模块是作为 @nuxtjs/auth的替代方法,除非这仅适用于NUXT3,仅没有向后兼容的支持。
@nuxt-alt/auth和@nuxt-alt/http依赖性添加到您的项目yarn add @nuxt-alt/auth @nuxt-alt/http@nuxt-alt/auth和@pinia/nuxt添加到nuxt.config.ts的modules部分注意:您不需要指定@nuxt-alt/http ,它将自动添加,但是如果要手动添加它,请确保它在Auth模块下方(如果您使用的(如果您使用)在代理模块上方)。它也不需要PINIA,默认情况下将使用NUXT的useState 。
export default defineNuxtConfig ( {
modules : [
'@nuxt-alt/auth'
] ,
auth : {
/* module options */
}
} ) ; 阅读文档
该模块现在使用“@nuxt-alt/http”来函数,该模块扩展了Ohmyfetch。请注意,如果您使用data发布数据,则现在需要使用body因为这是ohmyfetch使用的。如果您打算使用SSR,请考虑使用@nuxt-alt/proxy模块。
可以使用useAuth()组合来访问auth方法。
大多数选项直接从 @nuxtjs/auth模块中获取。此外,还有一些额外的选项。
globalMiddlewareBooleanfalse启用/禁用中间件可以在全球使用。
enableMiddlewareBooleantrue启用/禁用内置中间件。
stores.state.namespaceStringauth这是用于NUXT USESTATE的名称空间。
stores.pinia.enabledBooleanfalse启用此选项使用PINIA商店,BEY默认情况下,这是禁用的,而NUXT的useState则使用。
stores.pinia.namespaceStringauth这是用于Pinia商店的名称空间。
stores.local.enabledBooleantrue启用此选项可以使用LocalStorage Store。
stores.local.prefixStringauth.这设置了Localstorage前缀。
stores.session.enabledBooleantrue启用此选项使用SessionStorage Store。
stores.session.prefixStringauth.类似于LocalStorage选项,这是会话存储的前缀。
stores.cookie.enabledBooleantrue启用此选项可以使用cookie存储。
stores.cookie.prefixStringauth.类似于LocalStorage选项,这是Cookie存储的前缀。
stores.cookie.optionsObject{ path: '/' }默认的cookie存储选项。
redirectStrategyquery | storagestorage您要使用的重定向策略的类型,用于重定向的localStorage的storage使用,使用路由查询参数query 。
tokenValidationIntervalBoolean | Numberfalse这是实验性的。如果设置为true,则默认间隔为1000ms,否则将时间设置为毫秒。这是试图验证令牌到期的尝试的频率。
resetOnResponseErrorBoolean | Functionfalse启用后,当任何响应中有401个错误时,它将重置。您可以将其转变为自己处理此问题的函数:
auth: {
//... module options
resetOnResponseError : ( error , auth , scheme ) => {
if ( error . response . status === 401 ) {
scheme . reset ?. ( )
auth . redirect ( 'login' )
}
} ,
} 用户信息可以像打字稿一样编辑:
declare module '@nuxt-alt/auth' {
interface UserInfo {
email : string
name : string
}
} 除了身份令牌;
默认情况下, $auth.strategy getter使用的Scheme类型没有token或refreshToken属性类型。为此,添加了$auth.refreshStrategy和$auth.tokenStrategy getter。他们都做同样的事情,这只是用于类型提示。
OAuth2现在有了客户窗口验证,这要归功于此拉的请求:nuxt-community/auth-module#1746
属性已更改为:
clientWindowBooleanfalse启用/禁用使用弹出式验证的使用。
clientWidthNumber400客户端窗口的宽度。
clientHieghtNumber600客户端窗口的宽度。
可在NUXT中使用的可用别名
#auth/runtime#auth/utils#auth/providers