Skip to content

Commit

Permalink
feat: add global page protected
Browse files Browse the repository at this point in the history
  • Loading branch information
LorexIQ committed Nov 22, 2023
1 parent ae8359d commit e961187
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export default defineNuxtConfig({
refreshToken: {
enabled: true
},
pages: {
protectAllPages: true
},
endpoints: {
signIn: { path: '/auth/', method: 'POST' },
getMe: { path: 'users/me/', method: 'GET' },
Expand Down
11 changes: 11 additions & 0 deletions playground/pages/error.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script setup lang="ts">
</script>

<template>
error
</template>

<style scoped>
</style>
6 changes: 3 additions & 3 deletions playground/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function testAuth() {
}
definePageMeta({
localAuth: {
unauthorizedOnly: true
}
// localAuth: {
// unauthorizedOnly: true
// }
})
</script>

Expand Down
15 changes: 15 additions & 0 deletions playground/pages/test.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script setup lang="ts">
definePageMeta({
localAuth: {
authorizedOnly: true,
}
})
</script>

<template>
test
</template>

<style scoped>
</style>
7 changes: 6 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ interface ModuleOptionsPages {
* get session data due to a timeout. Default: '/error'
* */
serverIsDown?: string;
/* Protecting all pages from guests. Default: false
* */
protectAllPages?: boolean
}
export interface ModuleOptions {
/* Path to server. Default: ''
Expand Down Expand Up @@ -132,7 +135,9 @@ export default defineNuxtModule<ModuleOptions>({
},
pages: {
auth: '/login',
defaultRedirect: '/'
defaultRedirect: '/',
serverIsDown: '/error',
protectAllPages: false,
}
},
setup (options, nuxt) {
Expand Down
33 changes: 22 additions & 11 deletions src/runtime/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { defineNuxtRouteMiddleware, navigateTo, useRuntimeConfig } from '#imports';
import { defineNuxtRouteMiddleware, navigateTo } from '#imports';
import useUtils from "../composables/useUtils";
import { useLocalAuth } from "../composables/useLocalAuth";
import { getContext } from "../helpers";

type MiddlewareMeta = boolean | {
unauthorizedOnly?: boolean;
authorizedOnly?: boolean;
};

declare module '#app/../pages/runtime/composables' {
Expand All @@ -12,27 +13,37 @@ declare module '#app/../pages/runtime/composables' {
}
}

export default defineNuxtRouteMiddleware(to => {
export default defineNuxtRouteMiddleware(async to => {
const { options, state: { meta} } = await getContext();
const { trimStartWithSymbol, trimWithSymbol } = useUtils();

const metaAuth = typeof to.meta.localAuth === 'object' ?
{
unauthorizedOnly: true,
unauthorizedOnly: false,
authorizedOnly: false,
...to.meta.localAuth
} :
to.meta.localAuth;

if (
!metaAuth &&
!options.pages.protectAllPages ||
[trimWithSymbol(options.pages.serverIsDown!, '/')].includes(trimWithSymbol(to.path, '/'))
) return;
if (metaAuth === false) return;

const options = useRuntimeConfig().public.localAuth;
const { trimStartWithSymbol } = useUtils();
const { meta } = useLocalAuth();
const isAuthorized = meta.value.status === 'authorized';
const isUnauthorizedOnly = typeof metaAuth === 'object' ? metaAuth.unauthorizedOnly : false;
const isAuthorizedOnly = typeof metaAuth === 'object' ? metaAuth.authorizedOnly : false;

if (isAuthorized && to.path === options.pages.auth && isUnauthorizedOnly) {
return navigateTo(`/${trimStartWithSymbol(options.pages.defaultRedirect, '/')}`);
if (isAuthorized && to.path !== options.pages.defaultRedirect && isUnauthorizedOnly) {
return navigateTo(`/${trimStartWithSymbol(options.pages.defaultRedirect!, '/')}`);
}
if (!isAuthorized && to.path !== options.pages.defaultRedirect && isAuthorizedOnly) {
return navigateTo(`/${trimStartWithSymbol(options.pages.defaultRedirect!, '/')}`);
}

if (!isAuthorized && to.path !== options.pages.auth) {
return navigateTo(`/${trimStartWithSymbol(options.pages.auth, '/')}`);
if (!isAuthorized && to.path !== options.pages.auth && !isUnauthorizedOnly) {
return navigateTo(`/${trimStartWithSymbol(options.pages.auth!, '/')}`);
}
});

0 comments on commit e961187

Please sign in to comment.