mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
change in progress
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
|
||||
|
||||
|
||||
const ACCESS_TOKEN_STATE_KEY = 'access_token';
|
||||
const ACCESS_TOKEN_COOKIE_KEY = 'access_token';
|
||||
|
||||
const tokenCookie = useCookie(ACCESS_TOKEN_COOKIE_KEY, { expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30) });
|
||||
const token = ref<string | undefined>();
|
||||
|
||||
export function signHeaders(headers?: Record<string, string>) {
|
||||
const { token } = useAccessToken()
|
||||
@@ -15,26 +14,12 @@ export const authorizationHeaderComputed = computed(() => {
|
||||
return token.value ? 'Bearer ' + token.value : '';
|
||||
});
|
||||
|
||||
function setToken(value: string) {
|
||||
tokenCookie.value = value;
|
||||
token.value = value;
|
||||
}
|
||||
|
||||
export function useAccessToken() {
|
||||
|
||||
const tokenCookie = useCookie(ACCESS_TOKEN_COOKIE_KEY, { expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30) })
|
||||
|
||||
const token = useState<string | undefined | null>(ACCESS_TOKEN_STATE_KEY);
|
||||
const needLoad = useState<boolean>('needAccessTokenLoad', () => true);
|
||||
|
||||
|
||||
const readToken = () => {
|
||||
token.value = tokenCookie.value;
|
||||
needLoad.value = false;
|
||||
}
|
||||
const setToken = (newToken: string) => {
|
||||
tokenCookie.value = newToken;
|
||||
token.value = tokenCookie.value;
|
||||
needLoad.value = false;
|
||||
}
|
||||
|
||||
if (needLoad.value == true) readToken();
|
||||
|
||||
|
||||
return { token, readToken, setToken, needLoad }
|
||||
if (!token.value) token.value = tokenCookie.value as any;
|
||||
return { setToken, token }
|
||||
}
|
||||
@@ -1,80 +1,27 @@
|
||||
import type { InternalApi } from 'nitropack';
|
||||
import type { WatchSource, WatchStopHandle } from 'vue';
|
||||
|
||||
|
||||
type NitroFetchRequest = Exclude<keyof InternalApi, `/_${string}` | `/api/_${string}`> | (string & {});
|
||||
|
||||
export type CustomFetchOptions = {
|
||||
watchProps?: WatchSource[],
|
||||
lazy?: boolean,
|
||||
method?: string,
|
||||
getBody?: () => Record<string, any>,
|
||||
watchKey?: string
|
||||
export type CustomOptions = {
|
||||
useSnapshotDates?: boolean,
|
||||
useActivePid?: boolean,
|
||||
slice?: string,
|
||||
}
|
||||
|
||||
type OnResponseCallback<TData> = (data: Ref<TData | undefined>) => any
|
||||
type OnRequestCallback = () => any
|
||||
const { token } = useAccessToken();
|
||||
const { projectId } = useProject();
|
||||
const { safeSnapshotDates } = useSnapshot()
|
||||
|
||||
export function useComputedHeaders(customOptions?: CustomOptions) {
|
||||
const useSnapshotDates = customOptions?.useSnapshotDates || true;
|
||||
const useActivePid = customOptions?.useActivePid || true;
|
||||
|
||||
const watchStopHandles: Record<string, WatchStopHandle> = {}
|
||||
|
||||
export function useCustomFetch<T>(url: NitroFetchRequest, getHeaders: () => Record<string, string>, options?: CustomFetchOptions) {
|
||||
|
||||
const pending = ref<boolean>(false);
|
||||
const data = ref<T | undefined>();
|
||||
const error = ref<Error | undefined>();
|
||||
|
||||
let onResponseCallback: OnResponseCallback<T> = () => { }
|
||||
let onRequestCallback: OnRequestCallback = () => { }
|
||||
|
||||
const onResponse = (callback: OnResponseCallback<T>) => {
|
||||
onResponseCallback = callback;
|
||||
}
|
||||
|
||||
const onRequest = (callback: OnRequestCallback) => {
|
||||
onRequestCallback = callback;
|
||||
}
|
||||
|
||||
const execute = async () => {
|
||||
onRequestCallback();
|
||||
pending.value = true;
|
||||
error.value = undefined;
|
||||
try {
|
||||
|
||||
data.value = await $fetch<T>(url, {
|
||||
headers: getHeaders(),
|
||||
method: (options?.method || 'GET') as any,
|
||||
body: options?.getBody ? JSON.stringify(options.getBody()) : undefined
|
||||
});
|
||||
|
||||
onResponseCallback(data);
|
||||
} catch (err) {
|
||||
error.value = err as Error;
|
||||
} finally {
|
||||
pending.value = false;
|
||||
const headers = computed<Record<string, string>>(() => {
|
||||
return {
|
||||
'Authorization': `Bearer ${token.value}`,
|
||||
'x-pid': useActivePid ? (projectId.value ?? '') : '',
|
||||
'x-from': useSnapshotDates ? (safeSnapshotDates.value.from ?? '') : '',
|
||||
'x-to': useSnapshotDates ? (safeSnapshotDates.value.to ?? '') : '',
|
||||
'x-slice': customOptions?.slice ?? ''
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (options?.lazy !== true) {
|
||||
execute();
|
||||
}
|
||||
|
||||
if (options?.watchProps) {
|
||||
|
||||
const watchStop = watch(options.watchProps, () => {
|
||||
execute();
|
||||
});
|
||||
|
||||
const key = options?.watchKey || `${url}`;
|
||||
if (watchStopHandles[key]) watchStopHandles[key]();
|
||||
watchStopHandles[key] = watchStop;
|
||||
|
||||
console.log('Watchers:', Object.keys(watchStopHandles).length);
|
||||
|
||||
|
||||
}
|
||||
|
||||
const refresh = execute;
|
||||
|
||||
return { pending, execute, data, error, refresh, onResponse, onRequest };
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
@@ -1,30 +1,34 @@
|
||||
import type { AuthContext } from "~/server/middleware/01-authorization";
|
||||
|
||||
const LOGGED_USER_STATE_KEY = 'logged_user';
|
||||
|
||||
const loggedUser = ref<AuthContext | undefined>();
|
||||
|
||||
const setLoggedUser = (authContext?: AuthContext) => {
|
||||
loggedUser.value = authContext;
|
||||
};
|
||||
|
||||
|
||||
export function useLoggedUser() {
|
||||
const loggedUserState = useState<AuthContext | undefined>(LOGGED_USER_STATE_KEY);
|
||||
return loggedUserState;
|
||||
}
|
||||
|
||||
export function setLoggedUser(authContext?: AuthContext) {
|
||||
useLoggedUser().value = authContext;
|
||||
}
|
||||
|
||||
export const isAdminHidden = ref<boolean>(false);
|
||||
|
||||
export function useUserRoles() {
|
||||
|
||||
function getUserRoles() {
|
||||
const isPremium = computed(() => {
|
||||
const loggedUser = useLoggedUser();
|
||||
if (!loggedUser.value?.logged) return false;
|
||||
return loggedUser.value.user.roles.includes('PREMIUM');
|
||||
});
|
||||
const isAdmin = computed(() => {
|
||||
const loggedUser = useLoggedUser();
|
||||
if (!loggedUser.value?.logged) return false;
|
||||
return loggedUser.value.user.roles.includes('ADMIN');
|
||||
});
|
||||
return { isPremium, isAdmin }
|
||||
}
|
||||
|
||||
export const isAdminHidden = ref<boolean>(false);
|
||||
|
||||
export function useLoggedUser() {
|
||||
return {
|
||||
user: loggedUser,
|
||||
userRoles: getUserRoles(),
|
||||
setLoggedUser
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
53
dashboard/composables/useProject.ts
Normal file
53
dashboard/composables/useProject.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
|
||||
import type { TProject } from "@schema/ProjectSchema";
|
||||
import { ProjectSnapshotModel } from "@schema/ProjectSnapshot";
|
||||
|
||||
const { token } = useAccessToken();
|
||||
|
||||
const projectsRequest = useFetch<TProject[]>('/api/project/list', {
|
||||
headers: computed(() => {
|
||||
return {
|
||||
'Authorization': `Bearer ${token.value}`
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const projectList = computed(() => projectsRequest.data.value);
|
||||
const refreshProjectsList = () => projectsRequest.refresh();
|
||||
|
||||
const activeProjectId = ref<string | undefined>();
|
||||
|
||||
const setActiveProject = (project_id: string) => {
|
||||
activeProjectId.value = project_id;
|
||||
localStorage.setItem('active_pid', project_id);
|
||||
}
|
||||
|
||||
const project = computed(() => {
|
||||
if (!projectList.value) return;
|
||||
if (projectList.value.length == 0) return;
|
||||
if (activeProjectId.value) {
|
||||
const target = projectList.value.find(e => e._id.toString() == activeProjectId.value);
|
||||
if (target) return target;
|
||||
}
|
||||
const savedActive = localStorage.getItem('active_pid');
|
||||
if (savedActive) {
|
||||
const target = projectList.value.find(e => e._id.toString() == savedActive);
|
||||
if (target) {
|
||||
activeProjectId.value = savedActive;
|
||||
return target;
|
||||
}
|
||||
}
|
||||
activeProjectId.value = projectList.value[0]._id.toString();
|
||||
return projectList.value[0];
|
||||
})
|
||||
|
||||
export function useProject() {
|
||||
|
||||
const actions = {
|
||||
refreshProjectsList,
|
||||
setActiveProject
|
||||
}
|
||||
|
||||
return { project, projectList, actions, projectId: activeProjectId }
|
||||
}
|
||||
8
dashboard/composables/useRefreshKey.ts
Normal file
8
dashboard/composables/useRefreshKey.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
|
||||
const { project } = useProject()
|
||||
|
||||
export const refreshKey = computed(() => {
|
||||
if (!project.value) return 'null';
|
||||
return project.value._id.toString();
|
||||
})
|
||||
Reference in New Issue
Block a user