کار اولیه
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref, computed } from "vue";
|
||||
import { useStorage } from "@vueuse/core";
|
||||
|
||||
export const useAuthStore = defineStore("authStore", () => {
|
||||
// ===== state =====
|
||||
const user = ref<any>({});
|
||||
const isAuthenticated = ref(false);
|
||||
const isRealUser = ref(false);
|
||||
const errors = ref<any>(null);
|
||||
const loading = ref(false);
|
||||
|
||||
// ===== getters =====
|
||||
const isLoggedIn = computed(() => isAuthenticated.value && !!user.value?.token);
|
||||
const userName = computed(() => {
|
||||
if (!user.value) return "";
|
||||
return `${user.value.first_name || ""} ${user.value.last_name || ""}`.trim() || user.value.username || "";
|
||||
});
|
||||
|
||||
// ===== actions =====
|
||||
function setUser(response: any) {
|
||||
const payload = response?.data ?? response ?? {};
|
||||
|
||||
const userData = {
|
||||
id: payload.user_id ?? payload.id ?? null,
|
||||
level: payload.user_level ?? payload.level ?? null,
|
||||
token: payload.token ?? null,
|
||||
expire: payload.expire ?? null,
|
||||
refresh_token: payload.refresh_token ?? null,
|
||||
first_name: payload.first_name ?? payload.user_data?.first_name ?? "",
|
||||
last_name: payload.last_name ?? payload.user_data?.last_name ?? "",
|
||||
avatar: payload.avatar ?? payload.user_data?.avatar ?? "",
|
||||
username: payload.username ?? payload.user_data?.username ?? "",
|
||||
email: payload.email ?? payload.user_data?.email ?? "",
|
||||
mobile: payload.mobile ?? payload.user_data?.mobile ?? "",
|
||||
...(payload.user_data || {}),
|
||||
};
|
||||
|
||||
isAuthenticated.value = true;
|
||||
isRealUser.value = true;
|
||||
user.value = userData;
|
||||
errors.value = null;
|
||||
loading.value = false;
|
||||
|
||||
try {
|
||||
const tokenStorage = useStorage("id_token", "");
|
||||
tokenStorage.value = userData.token || "";
|
||||
|
||||
const userStorage = useStorage("user", {});
|
||||
userStorage.value = userData;
|
||||
|
||||
const userIdStorage = useStorage("user_id", "");
|
||||
userIdStorage.value = String(userData.id || "");
|
||||
|
||||
const nuxtApp = useNuxtApp();
|
||||
if (nuxtApp.$http && userData.token) {
|
||||
nuxtApp.$http.setToken(userData.token);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Could not write to storage:", e);
|
||||
}
|
||||
}
|
||||
|
||||
async function login(credentials: { username: string; password: string; captcha?: string }) {
|
||||
loading.value = true;
|
||||
errors.value = null;
|
||||
|
||||
try {
|
||||
const nuxtApp = useNuxtApp();
|
||||
const { $http } = nuxtApp;
|
||||
|
||||
const response = await $http.postRequest("/auth/login", credentials);
|
||||
|
||||
if (response?.data || response?.token) {
|
||||
setUser(response);
|
||||
return { success: true, data: response };
|
||||
}
|
||||
|
||||
throw new Error(response?.message || "ورود ناموفق بود");
|
||||
} catch (error: any) {
|
||||
errors.value = error?.message || "خطا در ارتباط با سرور";
|
||||
loading.value = false;
|
||||
return { success: false, error: errors.value };
|
||||
}
|
||||
}
|
||||
|
||||
async function getCaptcha() {
|
||||
try {
|
||||
const nuxtApp = useNuxtApp();
|
||||
const { $http } = nuxtApp;
|
||||
return await $http.getRequest("/auth/captcha");
|
||||
} catch (error) {
|
||||
console.error("Captcha Error:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function register(credentials: any) {
|
||||
loading.value = true;
|
||||
errors.value = null;
|
||||
|
||||
try {
|
||||
const nuxtApp = useNuxtApp();
|
||||
const { $http } = nuxtApp;
|
||||
|
||||
const response = await $http.postRequest("/auth/register", credentials);
|
||||
|
||||
if (response?.data || response?.token) {
|
||||
setUser(response);
|
||||
return { success: true, data: response };
|
||||
}
|
||||
|
||||
throw new Error(response?.message || "ثبت نام ناموفق بود");
|
||||
} catch (error: any) {
|
||||
errors.value = error?.message || "خطا در ثبت نام";
|
||||
loading.value = false;
|
||||
return { success: false, error: errors.value };
|
||||
}
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
try {
|
||||
const nuxtApp = useNuxtApp();
|
||||
const { $http } = nuxtApp;
|
||||
|
||||
if (isAuthenticated.value) {
|
||||
await $http.postRequest("/auth/logout", {});
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Logout API error:", error);
|
||||
} finally {
|
||||
userReset();
|
||||
}
|
||||
}
|
||||
|
||||
function userReset() {
|
||||
user.value = {};
|
||||
isAuthenticated.value = false;
|
||||
isRealUser.value = false;
|
||||
errors.value = null;
|
||||
loading.value = false;
|
||||
|
||||
try {
|
||||
const tokenStorage = useStorage("id_token", "");
|
||||
tokenStorage.value = "";
|
||||
|
||||
const userStorage = useStorage("user", {});
|
||||
userStorage.value = {};
|
||||
|
||||
const userIdStorage = useStorage("user_id", "");
|
||||
userIdStorage.value = "";
|
||||
|
||||
const nuxtApp = useNuxtApp();
|
||||
if (nuxtApp.$http) {
|
||||
nuxtApp.$http.clearToken();
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Could not clear storage:", e);
|
||||
}
|
||||
}
|
||||
|
||||
const restoreSession = () => {
|
||||
try {
|
||||
const userStorage = useStorage("user", {});
|
||||
const storedUser = userStorage.value as any;
|
||||
|
||||
if (storedUser && storedUser.token) {
|
||||
user.value = storedUser;
|
||||
isAuthenticated.value = true;
|
||||
isRealUser.value = !!storedUser.id;
|
||||
|
||||
const nuxtApp = useNuxtApp();
|
||||
if (nuxtApp.$http && storedUser.token) {
|
||||
nuxtApp.$http.setToken(storedUser.token);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Could not restore session:", e);
|
||||
}
|
||||
};
|
||||
|
||||
if (import.meta.client) {
|
||||
restoreSession();
|
||||
}
|
||||
|
||||
return {
|
||||
user,
|
||||
isAuthenticated,
|
||||
isRealUser,
|
||||
errors,
|
||||
loading,
|
||||
isLoggedIn,
|
||||
userName,
|
||||
setUser,
|
||||
login,
|
||||
getCaptcha,
|
||||
register,
|
||||
logout,
|
||||
userReset,
|
||||
restoreSession,
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
|
||||
export const useCommonStore = defineStore("commonStore", () => {
|
||||
const sidebarOpen = ref(false);
|
||||
|
||||
function toggleSidebar() {
|
||||
sidebarOpen.value = !sidebarOpen.value;
|
||||
}
|
||||
|
||||
function closeSidebar() {
|
||||
sidebarOpen.value = false;
|
||||
}
|
||||
|
||||
function openSidebar() {
|
||||
sidebarOpen.value = true;
|
||||
}
|
||||
|
||||
return {
|
||||
sidebarOpen,
|
||||
toggleSidebar,
|
||||
closeSidebar,
|
||||
openSidebar,
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
import { useAuthStore } from "./authStore";
|
||||
import { useStorage } from "@vueuse/core";
|
||||
|
||||
export const getUserPermission = defineStore("permissionStore", () => {
|
||||
const permissions = ref<any[]>([]);
|
||||
const loading = ref(false);
|
||||
|
||||
const hasPagePermission = (to_fullPath: string) => {
|
||||
let path = to_fullPath;
|
||||
const index = path.indexOf("?");
|
||||
if (index !== -1) path = path.substring(0, index);
|
||||
|
||||
const items = path.split("/");
|
||||
const page_key = (items[1] || "") + "_" + (items[2] || "");
|
||||
|
||||
return permissions.value.some((p) => p.section_tag === page_key);
|
||||
};
|
||||
|
||||
const hasActionPermission = (to_fullPath: string, action_tag: string) => {
|
||||
let path = to_fullPath;
|
||||
const index = path.indexOf("?");
|
||||
if (index !== -1) path = path.substring(0, index);
|
||||
|
||||
const items = path.split("/");
|
||||
const page_key = (items[1] || "") + "_" + (items[2] || "");
|
||||
|
||||
return permissions.value.some(
|
||||
(p) => p.section_tag === page_key && p.action_tag === action_tag
|
||||
);
|
||||
};
|
||||
|
||||
const fetchUserPermissions = async (projectId?: number) => {
|
||||
const { $http } = useNuxtApp();
|
||||
const authStore = useAuthStore();
|
||||
|
||||
if (!authStore.isAuthenticated && import.meta.client && !localStorage.getItem("user")) {
|
||||
return;
|
||||
}
|
||||
|
||||
loading.value = true;
|
||||
|
||||
try {
|
||||
const data = {
|
||||
project_id: projectId || 50,
|
||||
project_only: 1,
|
||||
};
|
||||
|
||||
const response = await $http.postRequest("/permissions/user-permissions", data);
|
||||
permissions.value = response?.data || response || [];
|
||||
|
||||
if (import.meta.client) {
|
||||
const permitStorage = useStorage("permit", []);
|
||||
permitStorage.value = permissions.value;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Could not fetch user permissions:", error);
|
||||
permissions.value = [];
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const reset = () => {
|
||||
permissions.value = [];
|
||||
loading.value = false;
|
||||
};
|
||||
|
||||
return {
|
||||
permissions,
|
||||
loading,
|
||||
hasPagePermission,
|
||||
hasActionPermission,
|
||||
fetchUserPermissions,
|
||||
reset,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user