mirror of
https://github.com/hay-kot/mealie.git
synced 2025-07-12 08:07:14 -07:00
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Docker Nightly Production / Backend Server Tests (push) Waiting to run
Docker Nightly Production / Frontend Tests (push) Waiting to run
Docker Nightly Production / Build Package (push) Waiting to run
Docker Nightly Production / Build Tagged Release (push) Blocked by required conditions
Docker Nightly Production / Notify Discord (push) Blocked by required conditions
Release Drafter / ✏️ Draft release (push) Waiting to run
Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com> Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import { ref, watch, computed } from "vue";
|
|
import type { UserOut } from "~/lib/api/types/user";
|
|
|
|
export const useMealieAuth = function () {
|
|
const auth = useAuth();
|
|
const { setToken } = useAuthState();
|
|
const { $axios } = useNuxtApp();
|
|
|
|
// 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;
|
|
}
|
|
else {
|
|
lastUser.value = null;
|
|
}
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
|
|
// 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);
|
|
refreshCookie(useRuntimeConfig().public.AUTH_TOKEN);
|
|
}
|
|
|
|
async function oauthSignIn() {
|
|
const params = new URLSearchParams(window.location.search);
|
|
const { data: token } = await $axios.get<{ access_token: string; token_type: "bearer" }>("/api/auth/oauth/callback", { params });
|
|
setToken(token.access_token);
|
|
await auth.getSession();
|
|
}
|
|
|
|
return {
|
|
user,
|
|
loggedIn,
|
|
signIn,
|
|
signOut: auth.signOut,
|
|
signUp: auth.signUp,
|
|
refresh: auth.refresh,
|
|
oauthSignIn,
|
|
};
|
|
};
|