From ba3499512e4aef4f527c99964ba223cb00fa6c9a Mon Sep 17 00:00:00 2001 From: marzban-dev Date: Mon, 13 Jan 2025 21:14:10 +0330 Subject: [PATCH] Create useAuth composable --- frontend/composables/useAuth.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 frontend/composables/useAuth.ts diff --git a/frontend/composables/useAuth.ts b/frontend/composables/useAuth.ts new file mode 100644 index 0000000..84310d8 --- /dev/null +++ b/frontend/composables/useAuth.ts @@ -0,0 +1,31 @@ +import { useCookies } from "@vueuse/integrations/useCookies"; + +export const useAuth = () => { + + // state + + const cookies = useCookies(); + + const token = ref(""); + + // method + + const updateToken = (newToken: string) => { + cookies.set("token", newToken); + }; + + const logout = () => { + cookies.remove("token"); + }; + + // watch + + watch(() => cookies.get("token"), (newValue) => { + token.value = newValue; + }, { + immediate: true + }); + + return { token, updateToken, logout }; + +}; \ No newline at end of file