Skip to content

Commit

Permalink
Fixing token system (#64)
Browse files Browse the repository at this point in the history
* removing local storage

* fix scopes

* added toasts
  • Loading branch information
BatuevIO authored Jan 24, 2025
1 parent 4b58132 commit f29eddd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
12 changes: 12 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
import { useProfileStore } from '@/store';
import { onMounted } from 'vue';
import ToastList from '@/components/ToastList.vue';
import { useToastStore } from './store/toastStore';
import { ToastType } from './models';
const profileStore = useProfileStore();
const toastStore = useToastStore();
onMounted(() => {
profileStore.clearLocalStorage();
profileStore.fromUrl();
if (!profileStore.isLoggedIn) {
toastStore.push({
title: 'Не получится оставить отзыв',
description: 'Вы не привязали ЛК МГУ к своему профилю или не вошли в профиль',
type: ToastType.Error,
});
}
console.log(profileStore.token);
});
</script>

Expand Down
6 changes: 3 additions & 3 deletions src/pages/ReviewPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ async function sendReview() {
type: ToastType.Warn,
description: 'Подождите немного перед тем, как отправить следующий отзыв',
});
} else if (profileStore.token === 'null') {
} else if (!profileStore.token) {
toastStore.push({
title: 'Вы не вошли в аккаунт',
title: 'Не получится оставить отзыв',
description: 'Вы не привязали ЛК МГУ к своему профилю или не вошли в профиль',
type: ToastType.Error,
description: 'Отзыв могут оставить только зарегистрированные пользователи',
});
} else {
toastStore.push({
Expand Down
37 changes: 14 additions & 23 deletions src/store/profileStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,30 @@ import { LocalStorage, LocalStorageItem } from '../models/LocalStorage';
import { setupAuth } from '@profcomff/api-uilib';

export const useProfileStore = defineStore('profile', () => {
const id = ref<number | null>(null);
const email = ref<string | null>(null);
const token = ref<string | undefined>(undefined);
const groups = ref<number[] | null>(null);
const indirectGroups = ref<number[] | null>(null);
const userScopes = ref<string[] | null>(null);
const sessionScopes = ref<string[] | null>(null);
const isLoggedIn = ref<boolean>(false);

const full_name = ref<string | null>(null);

const fromUrl = () => {
const url = new URL(document.location.toString());

const localToken = LocalStorage.get(LocalStorageItem.Token);
const localScopes = LocalStorage.getObject<string[]>(LocalStorageItem.TokenScopes);
const urlToken = url.searchParams.get('token');
const urlScopes = url.searchParams.get('scopes')?.split(',');

if (urlToken === null && urlScopes === undefined) {
token.value = localToken === 'null' ? undefined : (localToken ?? undefined);
sessionScopes.value = localScopes;
if (urlToken === null) {
token.value = undefined;
isLoggedIn.value = false;
sessionScopes.value = [];
} else {
token.value = urlToken === null ? undefined : urlToken;
sessionScopes.value = urlScopes || [];
LocalStorage.set(LocalStorageItem.Token, urlToken);
LocalStorage.set(LocalStorageItem.TokenScopes, urlScopes ?? []);
token.value = urlToken;
isLoggedIn.value = true;
sessionScopes.value = urlScopes ?? [];
}

setupAuth(token.value);

const currId = url.searchParams.get('user_id') ?? LocalStorage.get(LocalStorageItem.UserId) ?? undefined;
if (currId) {
id.value = Number(currId);
}
};

const isAdmin = () => {
Expand All @@ -48,18 +38,19 @@ export const useProfileStore = defineStore('profile', () => {
);
};

const clearLocalStorage = () => {
LocalStorage.delete(LocalStorageItem.Token, LocalStorageItem.TokenScopes, LocalStorageItem.UserId);
};

return {
id,
email,
token,
groups,
indirectGroups,
userScopes,
sessionScopes,
isLoggedIn,

full_name,

fromUrl,
isAdmin,
clearLocalStorage,
};
});

0 comments on commit f29eddd

Please sign in to comment.