This commit is contained in:
Mamalizz
2025-01-19 19:21:47 +03:30
26 changed files with 1657 additions and 89 deletions
@@ -2,6 +2,7 @@
import { useQuery } from "@tanstack/vue-query";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
import { useAuth } from "~/composables/api/auth/useAuth";
// types
+11 -1
View File
@@ -3,6 +3,7 @@ export const useAuth = () => {
// state
const token = useCookie("token");
const refreshToken = useCookie("refresh-token");
// method
@@ -10,11 +11,20 @@ export const useAuth = () => {
token.value = newToken;
};
const updateRefreshToken = (newToken: string) => {
refreshToken.value = newToken;
};
const logout = (reload ?: boolean) => {
token.value = undefined;
refreshToken.value = undefined;
if (reload) window.location.reload();
};
return { token, updateToken, logout };
// computed
const isLoggedIn = computed(() => !!token.value);
return { token, refreshToken, updateRefreshToken, updateToken, logout, isLoggedIn };
};
@@ -0,0 +1,36 @@
// imports
import { useMutation } from "@tanstack/vue-query";
import { API_ENDPOINTS } from "~/constants";
// types
export type RefreshAuthRequest = {
refresh: string,
};
export type RefreshAuthResponse = {
access: string,
refresh: string,
};
const useRefreshAuth = () => {
// state
const { $axios: axios } = useNuxtApp();
// methods
const handleRefreshAuth = async (variables: RefreshAuthRequest) => {
const { data } = await axios.post<RefreshAuthResponse>(`${API_ENDPOINTS.auth.refresh}/`, variables);
return data;
};
return useMutation({
mutationFn: (variables: RefreshAuthRequest) => handleRefreshAuth(variables)
});
};
export default useRefreshAuth;
@@ -0,0 +1,30 @@
// imports
import { useMutation } from "@tanstack/vue-query";
import { API_ENDPOINTS } from "~/constants";
// types
export type VerifyRequest = {
token: string,
};
const useVerify = () => {
// state
const { $axios: axios } = useNuxtApp();
// methods
const handleVerify = async (variables: VerifyRequest) => {
const { data } = await axios.post(`${API_ENDPOINTS.auth.verify}`, variables);
return data;
};
return useMutation({
mutationFn: (variables: VerifyRequest) => handleVerify(variables)
});
};
export default useVerify;
+4 -1
View File
@@ -2,6 +2,7 @@
import { useInfiniteQuery } from "@tanstack/vue-query";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
import { useAuth } from "~/composables/api/auth/useAuth";
// types
@@ -16,6 +17,8 @@ const useGetBranch = (
const { $axios: axios } = useNuxtApp();
const { isLoggedIn } = useAuth();
// method
const handleGetChat = async ({ productId, limit, offset }: {
@@ -37,7 +40,7 @@ const useGetBranch = (
};
return useInfiniteQuery({
enabled,
enabled: isLoggedIn,
queryKey: [QUERY_KEYS.chat],
initialPageParam: {
limit: 10,