Skip to content

Commit

Permalink
feat: add server shutdown error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LorexIQ committed Nov 22, 2023
1 parent 31ae95b commit dcbb9c3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
7 changes: 6 additions & 1 deletion playground/pages/error.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<script setup lang="ts">
import {useLocalAuth} from "#imports";
const auth = useLocalAuth();
</script>

<template>
error
<div>
error
{{auth}}
</div>
</template>

<style scoped>
Expand Down
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
4 changes: 3 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ interface ModuleOptionsPages {
* */
defaultRedirect?: string;
/* A page for catching a server shutdown when it is impossible to
* get session data due to a timeout. Default: '/error'
* get session data due to a timeout. Default: undefined
* Example #1: redirect to '/error'. value > '/error/
* Example #2: redirect is disabled. value > undefined
* */
serverIsDown?: string;
/* Protecting all pages from guests. Default: false
Expand Down
1 change: 1 addition & 0 deletions src/runtime/composables/useLocalAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async function getMe<T extends UseLocalAuthResponse = {}>(): Promise<T> {
return meData;
} catch (e: any) {
if (e.statusCode) {
await signOut();
throw new LocalAuthError(`getMe > [${e.statusCode}] > ${JSON.stringify(e.response._data)}`);
} else {
throw new LocalAuthError(`getMe > ${e.message}`);
Expand Down
31 changes: 22 additions & 9 deletions src/runtime/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type {ModuleOptions} from "../module";
import type {UseLocalAuthFetchConfig, UseLocalAuthResponse} from "./types";
import {callWithNuxt, useNuxtApp, useRuntimeConfig} from "#app";
import {
callWithNuxt,
useNuxtApp,
useRuntimeConfig,
navigateTo
} from "#app";
import useLocalAuthState from "./composables/useLocalAuthState";
import useUtils from "./composables/useUtils";

Expand All @@ -24,14 +29,22 @@ export async function fetch<T extends UseLocalAuthResponse>(
withToken: false,
..._config,
}
const { state: { token, origin} } = await getContext();
const { options, state: { token, origin, meta} } = await getContext();

return await $fetch(
`${origin}/${trimStartWithSymbol(endpoint.path, '/')}`,
{
method: endpoint.method,
body: config.body,
headers: config.withToken ? { 'Authorization': token.value! } : {}
try {
return await $fetch(
`${origin}/${trimStartWithSymbol(endpoint.path, '/')}`,
{
method: endpoint.method,
body: config.body,
headers: config.withToken ? { 'Authorization': token.value! } : {}
}
);
} catch (e: any) {
if (!e.statusCode && options.pages.serverIsDown) {
meta.value.status = 'timeout';
navigateTo(options.pages.serverIsDown);
}
);
throw Error(e);
}
}
5 changes: 4 additions & 1 deletion src/runtime/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ export default defineNuxtRouteMiddleware(async to => {
if (metaAuth === false) return;

const isAuthorized = meta.value.status === 'authorized';
const isTimeout = meta.value.status === 'timeout';
const isUnauthorizedOnly = typeof metaAuth === 'object' ? metaAuth.unauthorizedOnly : false;
const isAuthorizedOnly = typeof metaAuth === 'object' ? metaAuth.authorizedOnly : false;

if (isTimeout && options.pages.serverIsDown && to.path !== options.pages.serverIsDown) {
return navigateTo(`/${trimStartWithSymbol(options.pages.serverIsDown!, '/')}`);
}
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 && !isUnauthorizedOnly) {
return navigateTo(`/${trimStartWithSymbol(options.pages.auth!, '/')}`);
}
Expand Down

0 comments on commit dcbb9c3

Please sign in to comment.