feat: update environment variables for development and production; add user store and authentication client
- Updated VITE_API_URL in .env.development to point to local server. - Retained VITE_API_URL in .env.production for production use. - Added user store for managing user profile and authentication state. - Created authentication client for handling user login and token management. - Introduced safeClient utility for making API requests with error handling. - Updated various components and views to utilize new user store and authentication logic. - Enhanced UI styles for better visual consistency across the application.
This commit is contained in:
1
src/store/index.ts
Normal file
1
src/store/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./user";
|
||||
39
src/store/user.ts
Normal file
39
src/store/user.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { UserProfileData } from "@/api/types";
|
||||
import { client, safeClient } from "@/api";
|
||||
|
||||
interface State {
|
||||
userProfile: UserProfileData | null;
|
||||
}
|
||||
|
||||
export const useUserStore = defineStore("user", () => {
|
||||
const token = useStorageAsync<string | null>("user-token", null);
|
||||
const state = reactive<State>({
|
||||
userProfile: null,
|
||||
});
|
||||
|
||||
const isAuthenticated = computed(() => !!token.value);
|
||||
|
||||
async function updateProfile() {
|
||||
const { data } = await safeClient(client.api.user.profile.get(), { silent: true });
|
||||
state.userProfile = data.value || null;
|
||||
}
|
||||
|
||||
function setToken(value: string) {
|
||||
localStorage.setItem("user-token", value);
|
||||
token.value = value;
|
||||
}
|
||||
|
||||
function signOut() {
|
||||
token.value = null;
|
||||
state.userProfile = null;
|
||||
}
|
||||
|
||||
return {
|
||||
...toRefs(state),
|
||||
token,
|
||||
isAuthenticated,
|
||||
signOut,
|
||||
setToken,
|
||||
updateProfile,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user