Skip to content

Commit 3d11443

Browse files
committed
refresh token before requests
1 parent 01273a0 commit 3d11443

File tree

19 files changed

+83
-35
lines changed

19 files changed

+83
-35
lines changed

.yarn/install-state.gz

-10.6 KB
Binary file not shown.

app.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</div>
1212
<div v-else class="flx --flxColumn --flx-start-stretch --gap-0">
1313
<div
14-
v-if="SESSION.user && !isAdmin && APP.instance?.banner?.message"
14+
v-if="SESSION.token && !isAdmin && APP.instance?.banner?.message"
1515
class="x-banner flx --flxColumn --flx-center --gap-0 --width-100 --maxWidth --mX"
1616
>
1717
<div
@@ -61,7 +61,7 @@
6161
</li>
6262
<DropdownUser />
6363
<DropdownAdmin />
64-
<li v-if="!SESSION.user && route.path != '/ingresar'">
64+
<li v-if="!SESSION.token && route.path != '/ingresar'">
6565
<XamuActionLink to="/ingresar">
6666
<XamuIconFa
6767
name="circle-user"
@@ -135,7 +135,7 @@
135135
</p>
136136
</div>
137137
<div
138-
v-else-if="!SESSION.user && route.path != '/ingresar'"
138+
v-else-if="!SESSION.token && route.path != '/ingresar'"
139139
class="txt --txtAlign-center --gap-10"
140140
>
141141
<h4>Modo lectura</h4>

components/PaginatedTable.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</div>
1111
<XamuPaginationContent
1212
v-slot="{ content }"
13-
v-bind="{ page, url, noContentMessage, preventAutoload, theme }"
13+
v-bind="{ page, url, noContentMessage, preventAutoload, theme, client }"
1414
with-route
1515
:defaults="{ page: true, ...defaults }"
1616
class="flx --flxColumn --flx-start-end"
@@ -76,6 +76,7 @@
7676
noContentMessage?: string;
7777
tableProps?: Omit<iTableProps<TMi>, "nodes" | "refresh">;
7878
theme?: tThemeModifier | tThemeTuple;
79+
client?: boolean;
7980
}
8081
8182
/**

components/dropdown/Admin.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<XamuDropdown
3-
v-if="SESSION.user && SESSION.canAdmin"
3+
v-if="SESSION.token && SESSION.canAdmin"
44
:position="['bottom', 'right']"
55
invert-theme
66
>
@@ -22,7 +22,7 @@
2222
</template>
2323
<template #default="{ invertedTheme }">
2424
<nav
25-
v-if="SESSION.canDevelop || SESSION.user"
25+
v-if="SESSION.canDevelop || SESSION.token"
2626
class="list flx --flxColumn --gap-20 --minWidth-180 --maxWidth-100 --txtColor"
2727
>
2828
<ul v-if="SESSION.canDevelop" class="list-group --gap-5">

components/dropdown/Enrollment.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<XamuDropdown v-if="SESSION.user && enrolledCount" :position="['bottom', 'left']" invert-theme>
2+
<XamuDropdown v-if="SESSION.token && enrolledCount" :position="['bottom', 'left']" invert-theme>
33
<template #toggle="{ setModel, model }">
44
<li>
55
<XamuActionButtonToggle

components/dropdown/User.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<XamuDropdown v-if="SESSION.user" :position="['bottom', 'right']" invert-theme>
2+
<XamuDropdown v-if="SESSION.token" :position="['bottom', 'right']" invert-theme>
33
<template #toggle="{ setModel, model }">
44
<li>
55
<XamuActionLink

composables/utils.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,38 @@ export function useMinMilis(minutes: number) {
4646
return minutes * 60 * 1000;
4747
}
4848

49-
export function useFetchQuery<R>(
49+
/**
50+
* Fetch wrapper
51+
*
52+
* Refresh auth token before each request
53+
* @see https://stackoverflow.com/questions/47803495/error-firebase-id-token-has-expired
54+
*/
55+
export async function useFetchQuery<R>(
5056
url: string,
5157
{
5258
options,
5359
...query
5460
}: Record<string, any> & { options?: Omit<NitroFetchOptions<NitroFetchRequest>, "query"> } = {}
5561
) {
5662
const SESSION = useSessionStore();
63+
const { $clientAuth } = useNuxtApp();
64+
const { cache } = useRuntimeConfig().public;
65+
let authorization = SESSION.token || "";
66+
67+
// Refresh token, before server request
68+
if (import.meta.client && SESSION.user) {
69+
const newToken = await $clientAuth?.currentUser?.getIdToken();
70+
71+
authorization = newToken || authorization;
72+
73+
SESSION.setUser(SESSION.user, authorization);
74+
}
5775

5876
return $fetch<R>(url, {
59-
cache: "no-cache",
6077
credentials: "same-origin",
6178
...options,
6279
query,
63-
headers: { authorization: SESSION.token || "", ...options?.headers },
80+
headers: { authorization, "Cache-Control": cache.frequent, ...options?.headers },
6481
});
6582
}
6683

@@ -70,6 +87,8 @@ export const useLogger: tLogger = async (...args): Promise<void> => {
7087
if (!logData) return;
7188

7289
try {
90+
const { useDocumentCreate } = await import("~/composables/firestore");
91+
7392
useDocumentCreate<LogRef>("logs", logData);
7493
} catch (err) {
7594
console.error("Error logging to db", err);

error.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<span>Reintentar</span>
1919
</XamuActionLink>
2020
<XamuActionLink
21-
v-if="SESSION.user"
21+
v-if="SESSION.token"
2222
aria-label="Cerrar sesion y reintentar"
2323
@click="
2424
clearError();

middleware/guest-only.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
export default defineNuxtRouteMiddleware(async () => {
77
const SESSION = useSessionStore();
88

9-
if (!SESSION.user) return;
9+
if (!SESSION.token) return;
1010

1111
// User is authenticated
1212
return navigateTo("/", { redirectCode: 302 });

nuxt.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "node:path";
33

44
import locale from "@open-xamu-co/ui-common-helpers/es";
55

6+
import packageJson from "./package.json" assert { type: "json" };
67
import {
78
debugCSS,
89
debugNuxt,
@@ -92,6 +93,7 @@ export default defineNuxtConfig({
9293
compressPublicAssets: true,
9394
},
9495
vite: {
96+
server: { fs: { strict: "resolutions" in packageJson && !debugNuxt } },
9597
resolve: { preserveSymlinks: true },
9698
css: {
9799
postcss: require("@open-xamu-co/ui-styles/postcss")[

0 commit comments

Comments
 (0)