Features: 1) Add default value and path options for cookieAccess initialization in useRefresh; 2) Implement nextTick usage in useLogin and useRefresh for improved reactivity; 3) Enhance apollo.ts with cookieAccess object to ensure token consistency;
Fixes: 1) Reorder `router.push` in `useLogout` to properly clear cookies before redirection; 2) Resolve issues with inconsistent access token handling during Apollo header configuration; Extra: 1) Cleanup comments in `useRefresh
This commit is contained in:
parent
64730a1d4e
commit
cb8e4fb2ab
4 changed files with 20 additions and 7 deletions
|
|
@ -57,6 +57,8 @@ export function useLogin() {
|
|||
userStore.setUser(authData.user);
|
||||
cookieAccess.value = authData.accessToken;
|
||||
|
||||
await nextTick();
|
||||
|
||||
appStore.unsetActiveState();
|
||||
|
||||
useNotification({
|
||||
|
|
|
|||
|
|
@ -22,12 +22,12 @@ export function useLogout() {
|
|||
);
|
||||
|
||||
async function logout() {
|
||||
await router.push({path: '/'});
|
||||
|
||||
userStore.setUser(null);
|
||||
|
||||
cookieRefresh.value = '';
|
||||
cookieAccess.value = '';
|
||||
|
||||
await router.push({path: '/'});
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import {useUserBaseData} from "~/composables/user";
|
|||
export function useRefresh() {
|
||||
const { t } = useI18n();
|
||||
const userStore = useUserStore();
|
||||
const { COOKIES_REFRESH_TOKEN_KEY, COOKIES_LOCALE_KEY } = useAppConfig();
|
||||
const { COOKIES_REFRESH_TOKEN_KEY, COOKIES_LOCALE_KEY, COOKIES_ACCESS_TOKEN_KEY } = useAppConfig();
|
||||
const { checkAndRedirect } = useLocaleRedirect();
|
||||
|
||||
const { mutate, loading, error } = useMutation(REFRESH);
|
||||
|
|
@ -23,6 +23,13 @@ export function useRefresh() {
|
|||
path: '/'
|
||||
}
|
||||
);
|
||||
const cookieAccess = useCookie(
|
||||
COOKIES_ACCESS_TOKEN_KEY,
|
||||
{
|
||||
default: () => '',
|
||||
path: '/'
|
||||
}
|
||||
);
|
||||
const cookieLocale = useCookie(
|
||||
COOKIES_LOCALE_KEY,
|
||||
{
|
||||
|
|
@ -42,12 +49,13 @@ export function useRefresh() {
|
|||
}
|
||||
|
||||
userStore.setUser(data.user);
|
||||
cookieAccess.value = data.accessToken;
|
||||
|
||||
if (data.user.language !== cookieLocale.value) {
|
||||
await checkAndRedirect(data.user.language);
|
||||
}
|
||||
|
||||
cookieRefresh.value = data.refreshToken
|
||||
cookieRefresh.value = data.refreshToken;
|
||||
|
||||
// await useWishlist();
|
||||
// await useOrders({
|
||||
|
|
@ -55,6 +63,9 @@ export function useRefresh() {
|
|||
// status: "PENDING"
|
||||
// });
|
||||
// await usePromocodes();
|
||||
|
||||
await nextTick();
|
||||
|
||||
await useUserBaseData(data.user.email);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import { useAppConfig } from '~/composables/config';
|
|||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
const runtime = useRuntimeConfig();
|
||||
const localeCookie = useCookie(useAppConfig().COOKIES_LOCALE_KEY);
|
||||
const token = useCookie(useAppConfig().COOKIES_ACCESS_TOKEN_KEY).value || '';
|
||||
const token = useCookie(useAppConfig().COOKIES_ACCESS_TOKEN_KEY);
|
||||
const { $apollo } = nuxtApp as any;
|
||||
|
||||
const errorLink = onError((err) => {
|
||||
|
|
@ -21,8 +21,8 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||
'Accept-Language': localeCookie.value || 'en-gb'
|
||||
};
|
||||
|
||||
if (token) {
|
||||
hdrs['X-EVIBES-AUTH'] = `Bearer ${token}`;
|
||||
if (token.value) {
|
||||
hdrs['X-EVIBES-AUTH'] = `Bearer ${token.value}`;
|
||||
}
|
||||
|
||||
return { headers: hdrs };
|
||||
|
|
|
|||
Loading…
Reference in a new issue