31 lines
539 B
TypeScript
31 lines
539 B
TypeScript
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 };
|
|
|
|
}; |