Create useAuth composable
This commit is contained in:
@@ -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 };
|
||||||
|
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user