Skip to content

Commit fd92b38

Browse files
Benoit NgoNgob
authored andcommitted
fix(logout): Refreshing /me instead of removing it. That allow to quitly redirect user without displaying toasters
1 parent 5394a6e commit fd92b38

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

apps/front/src/app.vue

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@ const route = useRoute();
2020
2121
const { isAuthenticated, isMePending, authUrl } = storeToRefs(authStore);
2222
// Doing this here instead than in the middleware allow reactivity on the auth user
23-
watchEffect(async () => {
24-
if (isMePending.value) {
25-
return;
23+
watchEffect(
24+
async () => {
25+
if (isMePending.value) {
26+
return;
27+
}
28+
const shouldRedirectToLogin =
29+
!isAuthenticated.value &&
30+
authUrl.value &&
31+
route.fullPath !== authUrl.value;
32+
if (shouldRedirectToLogin) {
33+
return navigateTo(authUrl.value, { external: true });
34+
}
35+
const shouldRedirectToHomepage =
36+
isAuthenticated.value && route.fullPath === authUrl.value;
37+
if (shouldRedirectToHomepage) {
38+
return navigateTo("/");
39+
}
40+
},
41+
{
42+
flush: "pre",
2643
}
27-
const shouldRedirectToLogin =
28-
!isAuthenticated.value && authUrl.value && route.fullPath !== authUrl.value;
29-
if (shouldRedirectToLogin) {
30-
return navigateTo(authUrl.value, { external: true });
31-
}
32-
const shouldRedirectToHomepage =
33-
isAuthenticated.value && route.fullPath === authUrl.value;
34-
if (shouldRedirectToHomepage) {
35-
return navigateTo("/");
36-
}
37-
});
44+
);
3845
</script>

apps/front/src/components/form/RegisterPasswordInput.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
defineOptions({
5858
inheritAttrs: false,
5959
});
60-
const props = defineProps<{
60+
defineProps<{
6161
inputId: string;
6262
}>();
6363
</script>

apps/front/src/components/layout/AppHeader.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<div class="col-2 col-offset-8"></div>
44
<div class="col-2">
55
<p
6-
v-if="username"
6+
v-if="authStore.me?.email"
77
v-t="{
88
path: 'components.layout.appHeader.welcome',
9-
args: { username },
9+
args: { username: authStore.me.email },
1010
}"
1111
></p>
1212
</div>
@@ -16,6 +16,4 @@
1616
import { useAuthUser } from "~/store/auth";
1717
1818
const authStore = useAuthUser();
19-
20-
const username = authStore.me.email || "";
2119
</script>

apps/front/src/store/auth.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ const logout = (fetcher: AppFetch<any>) => {
2121
};
2222

2323
export const useAuthUser = defineStore("auth-store", () => {
24-
const me = ref();
24+
const me: Ref<null | User> = ref(null);
2525
const { error, resetError, setError } = useBasicError();
2626
const isMePending = ref(false);
2727
const authUrl = ref("/auth/login");
2828
const refresh = async ($appFetch: AppFetch<User | undefined>) => {
2929
resetError();
3030
isMePending.value = true;
3131
try {
32-
const res = await $appFetch("auth/me");
32+
const res = await $appFetch("auth/me", {
33+
// This override the default behavior of onResponse interceptor from $appFetch
34+
// Because when logout, the next /me gonna answer 401 but you dont want to display any errors
35+
onResponse: () => {},
36+
});
37+
if (!res) {
38+
throw createError("res expect a value");
39+
}
3340
me.value = res;
3441
} catch (exception: any) {
3542
isMePending.value = false;
@@ -48,7 +55,7 @@ export const useAuthUser = defineStore("auth-store", () => {
4855
};
4956
const logoutUser = async (fetch: AppFetch<any>) => {
5057
await logout(fetch);
51-
resetAuth();
58+
await refresh(fetch);
5259
};
5360

5461
return {

0 commit comments

Comments
 (0)