mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-25 07:47:56 -07:00
Fix flickering user auth
This commit is contained in:
parent
01852198b5
commit
736fd8da9d
1 changed files with 17 additions and 9 deletions
|
@ -1,33 +1,41 @@
|
|||
import { ref, watch, computed } from "vue";
|
||||
import type { UserOut } from "~/lib/api/types/user";
|
||||
|
||||
const USER_CACHE_KEY = "mealie_user";
|
||||
|
||||
export const useMealieAuth = function () {
|
||||
const auth = useAuth();
|
||||
const { setToken } = useAuthState();
|
||||
const { $axios } = useNuxtApp();
|
||||
|
||||
// Try to restore user from cache
|
||||
const cachedUser = localStorage.getItem(USER_CACHE_KEY);
|
||||
const lastUser = ref<UserOut | null>(cachedUser ? JSON.parse(cachedUser) : null);
|
||||
// User Management
|
||||
const lastUser = ref<UserOut | null>(null);
|
||||
const user = computed(() => lastUser.value);
|
||||
|
||||
watch(
|
||||
() => auth.data.value,
|
||||
(val) => {
|
||||
if (val) {
|
||||
lastUser.value = val as UserOut;
|
||||
localStorage.setItem(USER_CACHE_KEY, JSON.stringify(val));
|
||||
}
|
||||
else {
|
||||
localStorage.removeItem(USER_CACHE_KEY);
|
||||
lastUser.value = null;
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
const user = computed(() => lastUser.value);
|
||||
const loggedIn = computed(() => auth.status.value === "authenticated");
|
||||
// Auth Status Management
|
||||
const lastAuthStatus = ref<string>(auth.status.value);
|
||||
const loggedIn = computed(() => lastAuthStatus.value === "authenticated");
|
||||
|
||||
watch(
|
||||
() => auth.status.value,
|
||||
(val) => {
|
||||
if (val !== "loading") {
|
||||
lastAuthStatus.value = val;
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
async function signIn(...params: Parameters<typeof auth.signIn>) {
|
||||
await auth.signIn(...params);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue