Skip to content

Commit f470f2d

Browse files
Benoit NgoNgob
Benoit Ngo
authored andcommitted
feat(auth): Adding logout
1 parent 094bae6 commit f470f2d

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

apps/back/config/packages/security.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ security:
1717
security: true
1818
lazy: true
1919
provider: app_user_provider
20+
logout:
21+
path: app_logout
2022
json_login:
2123
check_path: api_login
2224
# custom_authenticators:

apps/back/config/routes.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#index:
22
# path: /
33
# controller: App\Controller\DefaultController::index
4+
app_logout:
5+
path: "%app.api.url.prefix%/auth/logout"
6+
methods: POST

apps/front/src/app.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<template>
22
<div>
33
<NuxtErrorBoundary @error="mHandleError">
4-
<NuxtLoadingIndicator color="linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(0,18,71,1) 60%, rgba(0,236,174,1) 100%)"/>
4+
<NuxtLoadingIndicator
5+
color="linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(0,18,71,1) 60%, rgba(0,236,174,1) 100%)"
6+
/>
57
<NuxtLayout v-if="!isMePending || isAuthenticated">
68
<NuxtPage />
79
</NuxtLayout>

apps/front/src/components/layout/menu/AppMenu.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
<TieredMenu :model="items" class="h-screen sticky w-full"> </TieredMenu>
33
</template>
44
<script setup lang="ts">
5+
import useAuthUser from "~/store/auth";
6+
57
const { t } = useI18n();
8+
const authStore = useAuthUser();
9+
const { $appFetch } = useNuxtApp();
610
const items = computed(() => [
711
{
812
label: t("components.layout.menu.appMenu.users"),
@@ -30,6 +34,10 @@ const items = computed(() => [
3034
{
3135
label: t("components.layout.menu.appMenu.quit"),
3236
icon: "pi pi-fw pi-power-off",
37+
command: async () => {
38+
await authStore.logoutUser($appFetch);
39+
await navigateTo("/", { external: true });
40+
},
3341
},
3442
]);
3543
</script>

apps/front/src/store/auth.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ const login = <T>(fetcher: AppFetch<T>, username: string, password: string) => {
1414
});
1515
};
1616

17+
const logout = (fetcher: AppFetch<any>) => {
18+
return fetcher("/auth/logout", {
19+
method: POST,
20+
}) as Promise<null>;
21+
};
22+
1723
export const useAuthUser = defineStore("auth-store", () => {
1824
const me = ref();
1925
const { error, resetError, setError } = useBasicError();
@@ -37,6 +43,12 @@ export const useAuthUser = defineStore("auth-store", () => {
3743
isMePending.value = false;
3844
};
3945

46+
const resetAuth = () => (me.value = null);
47+
const logoutUser = async (fetch: AppFetch<any>) => {
48+
await logout(fetch);
49+
resetAuth();
50+
};
51+
4052
return {
4153
me,
4254
meError: error,
@@ -46,9 +58,9 @@ export const useAuthUser = defineStore("auth-store", () => {
4658
isAuthenticated: computed(() => !!me.value),
4759

4860
isAuthUser: (user: User) => me.value?.id === user.id,
49-
resetAuth: () => (me.value = null),
61+
resetAuth,
5062
refresh,
51-
63+
logoutUser,
5264
async authenticateUser(
5365
username: string,
5466
password: string,

0 commit comments

Comments
 (0)