feat(account): implement initial skin loading in profile
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref, computed } from "vue";
|
||||
import { registerUser as apiRegisterUser } from "@/api/auth";
|
||||
import { getUserProfile as apiGetUserProfile } from "@/api/user";
|
||||
import type { RegisterRequest } from "@/types";
|
||||
import router from "@/router";
|
||||
import apiClient from "@/api/axios";
|
||||
@@ -9,6 +10,7 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
// State
|
||||
const user = ref<User | null>(null);
|
||||
const token = ref<string | null>(localStorage.getItem("authToken"));
|
||||
const skinUrl = ref<string | null>(null);
|
||||
const isLoading = ref(false);
|
||||
const error = ref<string | null>(null);
|
||||
|
||||
@@ -16,11 +18,34 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
const isAuthenticated = computed(() => !!user.value && !!token.value);
|
||||
|
||||
// Actions
|
||||
async function fetchUserProfile() {
|
||||
if (!user.value) return;
|
||||
|
||||
try {
|
||||
const response = await apiGetUserProfile(user.value.uuid);
|
||||
if (response.data && response.data.properties) {
|
||||
const textureProp = response.data.properties.find((p) => p.name === "textures");
|
||||
if (textureProp) {
|
||||
// Декодируем Base64-строку и парсим JSON
|
||||
const textureData = JSON.parse(atob(textureProp.value));
|
||||
if (textureData.textures?.SKIN?.url) {
|
||||
skinUrl.value = textureData.textures.SKIN.url;
|
||||
} else {
|
||||
skinUrl.value = null; // У пользователя нет скина
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to fetch user profile:", e);
|
||||
skinUrl.value = null;
|
||||
}
|
||||
}
|
||||
function setAuthData(userData: User, authToken: string) {
|
||||
user.value = userData;
|
||||
token.value = authToken;
|
||||
localStorage.setItem("authToken", authToken);
|
||||
apiClient.defaults.headers.common["Authorization"] = `Bearer ${authToken}`;
|
||||
fetchUserProfile();
|
||||
}
|
||||
|
||||
async function handleLogin(credentials: LoginRequest) {
|
||||
@@ -61,15 +86,24 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
function handleLogout() {
|
||||
user.value = null;
|
||||
token.value = null;
|
||||
skinUrl.value = null;
|
||||
localStorage.removeItem("authToken");
|
||||
delete apiClient.defaults.headers.common["Authorization"];
|
||||
router.push({ name: "login" });
|
||||
}
|
||||
|
||||
async function checkAuth() {
|
||||
if (token.value) {
|
||||
if (user.value) {
|
||||
await fetchUserProfile();
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
// State
|
||||
user,
|
||||
token,
|
||||
skinUrl,
|
||||
isLoading,
|
||||
error,
|
||||
// Getters
|
||||
@@ -78,5 +112,7 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
handleLogin,
|
||||
handleRegister,
|
||||
handleLogout,
|
||||
fetchUserProfile,
|
||||
checkAuth,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user