mirror of
https://github.com/bitinflow/nuxt-oauth.git
synced 2026-03-13 21:56:00 +00:00
first commit
This commit is contained in:
35
src/runtime/plugin.ts
Normal file
35
src/runtime/plugin.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import {addRouteMiddleware, defineNuxtPlugin, navigateTo} from '#app'
|
||||
import useAuth from "./composables/useAuth"
|
||||
|
||||
export default defineNuxtPlugin(() => {
|
||||
addRouteMiddleware('auth', async (to) => {
|
||||
const {user, authConfig, setBearer} = await useAuth()
|
||||
|
||||
if (to.path === authConfig.redirect.callback) {
|
||||
const params = new URLSearchParams(to.hash.substring(1))
|
||||
|
||||
if (params.has('access_token')) {
|
||||
const token = params.get('access_token') as string;
|
||||
const tokenType = params.get('token_type') as string;
|
||||
const expires = params.get('expires_in') as string;
|
||||
|
||||
await setBearer(token, tokenType, parseInt(expires));
|
||||
return navigateTo(authConfig.redirect.home)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (user.value === undefined) {
|
||||
return navigateTo(authConfig.redirect.login)
|
||||
}
|
||||
})
|
||||
|
||||
addRouteMiddleware('guest', async () => {
|
||||
const {user, authConfig} = await useAuth()
|
||||
|
||||
if (user.value !== undefined) {
|
||||
return navigateTo(authConfig.redirect.home)
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user