-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
123 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
export default defineNuxtConfig({ | ||
devtools: { enabled: true }, | ||
modules: ['../src/module'], | ||
|
||
auth: { | ||
baseURL: 'http://localhost:3000/api/auth', | ||
baseURL: '/api/auth', | ||
provider: { | ||
type: 'authjs', | ||
}, | ||
globalAppMiddleware: true, | ||
globalAppMiddleware: { | ||
allow404WithoutAuth: false, | ||
isEnabled: true, | ||
}, | ||
}, | ||
|
||
compatibilityDate: '2024-07-25', | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
import { options } from './auth/[...]' | ||
import { getServerSession, getServerToken } from '#auth' | ||
import { getServerSession } from '#auth' | ||
|
||
export default defineEventHandler(async (event) => { | ||
const session = await getServerSession(event) | ||
const token = await getServerToken(event, options) | ||
|
||
return { | ||
session, | ||
token, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,76 @@ | ||
import authMiddleware from '../middleware/auth' | ||
import { addRouteMiddleware, defineNuxtPlugin, useAuth, useRuntimeConfig } from '#imports' | ||
|
||
export default defineNuxtPlugin(async (nuxtApp) => { | ||
// 1. 初始化变量,调用 getSession 获取用户权限数据 | ||
const config = useRuntimeConfig().public.auth | ||
const { data, lastRefreshedAt, getSession } = useAuth() | ||
export default defineNuxtPlugin({ | ||
name: '@roshan-labs/auth', | ||
enforce: 'pre', | ||
async setup(nuxtApp) { | ||
// 1. 初始化变量,调用 getSession 获取用户权限数据 | ||
const config = useRuntimeConfig().public.auth | ||
const { data, lastRefreshedAt, getSession } = useAuth() | ||
|
||
if (data.value === null) { | ||
await getSession() | ||
} | ||
if (data.value === null) { | ||
await getSession() | ||
} | ||
|
||
// 2. 配置 session 刷新规则 | ||
let refetchIntervalTimer: number | null = null | ||
const { enableRefreshPeriodically, enableRefreshOnWindowFocus } = config.session | ||
// 2. 配置 session 刷新规则 | ||
let refetchIntervalTimer: number | null = null | ||
const { enableRefreshPeriodically, enableRefreshOnWindowFocus } = config.session | ||
|
||
// 如果配置 enableRefreshOnWindowFocus 为 true,则在每次 window 激活的时候重新获取 session | ||
const onVisibilitychange = () => { | ||
if (document.visibilityState === 'visible') { | ||
getSession() | ||
// 如果配置 enableRefreshOnWindowFocus 为 true,则在每次 window 激活的时候重新获取 session | ||
const onVisibilitychange = () => { | ||
if (document.visibilityState === 'visible') { | ||
getSession() | ||
} | ||
} | ||
} | ||
|
||
nuxtApp.hook('app:mounted', () => { | ||
if (enableRefreshOnWindowFocus) { | ||
document.addEventListener('visibilitychange', onVisibilitychange, false) | ||
} | ||
nuxtApp.hook('app:mounted', () => { | ||
if (enableRefreshOnWindowFocus) { | ||
document.addEventListener('visibilitychange', onVisibilitychange, false) | ||
} | ||
|
||
// 设置定时刷新 session | ||
if (enableRefreshPeriodically !== false) { | ||
const intervalTime: number = | ||
typeof enableRefreshPeriodically === 'boolean' ? 1000 : enableRefreshPeriodically | ||
// 设置定时刷新 session | ||
if (enableRefreshPeriodically !== false) { | ||
const intervalTime: number = | ||
typeof enableRefreshPeriodically === 'boolean' ? 1000 : enableRefreshPeriodically | ||
|
||
refetchIntervalTimer = window.setInterval(() => { | ||
if (data.value) { | ||
getSession() | ||
} | ||
}, intervalTime) | ||
} | ||
}) | ||
refetchIntervalTimer = window.setInterval(() => { | ||
if (data.value) { | ||
getSession() | ||
} | ||
}, intervalTime) | ||
} | ||
}) | ||
|
||
// 在应用卸载时清理 session 相关事件 | ||
const _unmount = nuxtApp.vueApp.unmount | ||
// 在应用卸载时清理 session 相关事件 | ||
const _unmount = nuxtApp.vueApp.unmount | ||
|
||
nuxtApp.vueApp.unmount = () => { | ||
// 清除 visibilitychange 事件 | ||
if (enableRefreshOnWindowFocus) { | ||
document.removeEventListener('visibilitychange', onVisibilitychange) | ||
} | ||
nuxtApp.vueApp.unmount = () => { | ||
// 清除 visibilitychange 事件 | ||
if (enableRefreshOnWindowFocus) { | ||
document.removeEventListener('visibilitychange', onVisibilitychange) | ||
} | ||
|
||
// 清除刷新 session 定时器 | ||
if (refetchIntervalTimer !== null) { | ||
window.clearInterval(refetchIntervalTimer) | ||
} | ||
// 清除刷新 session 定时器 | ||
if (refetchIntervalTimer !== null) { | ||
window.clearInterval(refetchIntervalTimer) | ||
} | ||
|
||
// 清除 session | ||
data.value = null | ||
lastRefreshedAt.value = null | ||
// 清除 session | ||
data.value = null | ||
lastRefreshedAt.value = null | ||
|
||
_unmount() | ||
} | ||
_unmount() | ||
} | ||
|
||
// 3. 按配置判断是否注册全局路由中间件 | ||
const { globalAppMiddleware } = config | ||
// 3. 按配置判断是否注册全局路由中间件 | ||
const { globalAppMiddleware } = config | ||
|
||
if ( | ||
(typeof globalAppMiddleware === 'boolean' && globalAppMiddleware === true) || | ||
globalAppMiddleware.isEnabled | ||
) { | ||
addRouteMiddleware('auth', authMiddleware, { global: true }) | ||
} | ||
if ( | ||
(typeof globalAppMiddleware === 'boolean' && globalAppMiddleware === true) || | ||
globalAppMiddleware.isEnabled | ||
) { | ||
addRouteMiddleware('auth', authMiddleware, { global: true }) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters