new changes
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
export const useAuth = () => {
|
||||
|
||||
// state
|
||||
|
||||
const token = useCookie("token");
|
||||
const refreshToken = useCookie("refresh-token");
|
||||
|
||||
// method
|
||||
// methods
|
||||
|
||||
const updateToken = (newToken: string) => {
|
||||
token.value = newToken;
|
||||
@@ -15,7 +14,7 @@ export const useAuth = () => {
|
||||
refreshToken.value = newToken;
|
||||
};
|
||||
|
||||
const logout = (reload ?: boolean) => {
|
||||
const logout = (reload?: boolean) => {
|
||||
token.value = undefined;
|
||||
refreshToken.value = undefined;
|
||||
if (reload) window.location.reload();
|
||||
@@ -25,6 +24,12 @@ export const useAuth = () => {
|
||||
|
||||
const isLoggedIn = computed(() => !!token.value);
|
||||
|
||||
return { token, refreshToken, updateRefreshToken, updateToken, logout, isLoggedIn };
|
||||
|
||||
};
|
||||
return {
|
||||
token,
|
||||
refreshToken,
|
||||
updateRefreshToken,
|
||||
updateToken,
|
||||
logout,
|
||||
isLoggedIn,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -6,17 +6,15 @@ import { API_ENDPOINTS } from "~/constants";
|
||||
// types
|
||||
|
||||
export type RefreshAuthRequest = {
|
||||
refresh: string,
|
||||
refresh: string;
|
||||
};
|
||||
|
||||
export type RefreshAuthResponse = {
|
||||
access: string,
|
||||
refresh: string,
|
||||
access: string;
|
||||
refresh: string;
|
||||
};
|
||||
|
||||
|
||||
const useRefreshAuth = () => {
|
||||
|
||||
// state
|
||||
|
||||
const { $axios: axios } = useNuxtApp();
|
||||
@@ -24,12 +22,16 @@ const useRefreshAuth = () => {
|
||||
// methods
|
||||
|
||||
const handleRefreshAuth = async (variables: RefreshAuthRequest) => {
|
||||
const { data } = await axios.post<RefreshAuthResponse>(`${API_ENDPOINTS.auth.refresh}/`, variables);
|
||||
const { data } = await axios.post<RefreshAuthResponse>(
|
||||
`${API_ENDPOINTS.auth.refresh}`,
|
||||
variables
|
||||
);
|
||||
return data;
|
||||
};
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (variables: RefreshAuthRequest) => handleRefreshAuth(variables)
|
||||
mutationFn: (variables: RefreshAuthRequest) =>
|
||||
handleRefreshAuth(variables),
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user