2024-06-09 20:04:46 +00:00
|
|
|
import { IIdentity_Response_DTO, ILogin_DTO, ILogin_Response_DTO } from "@shared/contexts";
|
2024-06-06 11:05:54 +00:00
|
|
|
import secureLocalStorage from "react-secure-storage";
|
|
|
|
|
import { IAuthActions } from "../hooks";
|
|
|
|
|
import { createAxiosInstance } from "./axiosInstance";
|
|
|
|
|
|
|
|
|
|
export const createAxiosAuthActions = (
|
|
|
|
|
apiUrl: string,
|
|
|
|
|
httpClient = createAxiosInstance()
|
|
|
|
|
): IAuthActions => ({
|
|
|
|
|
login: async ({ email, password }: ILogin_DTO) => {
|
|
|
|
|
// eslint-disable-next-line no-useless-catch
|
|
|
|
|
try {
|
2024-06-09 20:04:46 +00:00
|
|
|
const result = await httpClient.request<ILogin_Response_DTO>({
|
2024-06-06 11:05:54 +00:00
|
|
|
url: `${apiUrl}/auth/login`,
|
|
|
|
|
method: "POST",
|
|
|
|
|
data: {
|
|
|
|
|
email,
|
|
|
|
|
password,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-09 20:04:46 +00:00
|
|
|
const { data } = result;
|
|
|
|
|
secureLocalStorage.setItem("uecko.auth", data);
|
2024-06-06 11:05:54 +00:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
data,
|
2024-07-01 17:12:15 +00:00
|
|
|
redirectTo: "/",
|
2024-06-06 11:05:54 +00:00
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
error: {
|
|
|
|
|
message: "Login failed",
|
|
|
|
|
name: "Invalid email or password",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-06-09 20:04:46 +00:00
|
|
|
|
2024-06-06 11:05:54 +00:00
|
|
|
logout: () => {
|
2024-06-09 20:04:46 +00:00
|
|
|
secureLocalStorage.clear();
|
|
|
|
|
|
2024-06-06 11:05:54 +00:00
|
|
|
return Promise.resolve({
|
|
|
|
|
success: true,
|
|
|
|
|
redirectTo: "/login",
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-06-09 20:04:46 +00:00
|
|
|
|
2024-06-06 11:05:54 +00:00
|
|
|
check: () => {
|
2024-06-09 20:04:46 +00:00
|
|
|
const profile = secureLocalStorage.getItem("uecko.auth") as ILogin_Response_DTO;
|
|
|
|
|
|
2024-06-06 11:05:54 +00:00
|
|
|
return Promise.resolve(
|
2024-06-09 20:04:46 +00:00
|
|
|
profile?.token
|
2024-06-06 11:05:54 +00:00
|
|
|
? {
|
|
|
|
|
authenticated: true,
|
|
|
|
|
}
|
|
|
|
|
: { authenticated: false, redirectTo: "/login" }
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-06-09 20:04:46 +00:00
|
|
|
|
|
|
|
|
getIdentity: async () => {
|
2024-06-29 19:39:25 +00:00
|
|
|
const errorResult = {
|
|
|
|
|
message: "Identification failed",
|
|
|
|
|
name: "Invalid profile or identification",
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-09 20:04:46 +00:00
|
|
|
try {
|
|
|
|
|
const result = await httpClient.request<IIdentity_Response_DTO>({
|
|
|
|
|
url: `${apiUrl}/auth/identity`,
|
|
|
|
|
method: "GET",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { data } = result;
|
|
|
|
|
const profile = secureLocalStorage.getItem("uecko.auth") as ILogin_Response_DTO;
|
|
|
|
|
|
|
|
|
|
if (profile?.id === data?.id) {
|
|
|
|
|
secureLocalStorage.setItem("uecko.profile", data);
|
2024-06-29 19:39:25 +00:00
|
|
|
return Promise.resolve(data);
|
2024-06-09 20:04:46 +00:00
|
|
|
}
|
2024-06-29 19:39:25 +00:00
|
|
|
return Promise.reject(errorResult);
|
2024-06-09 20:04:46 +00:00
|
|
|
} catch (error) {
|
2024-06-29 19:39:25 +00:00
|
|
|
return Promise.reject(errorResult);
|
2024-06-09 20:04:46 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2024-06-06 11:05:54 +00:00
|
|
|
onError: (error: any) => {
|
|
|
|
|
console.error(error);
|
2024-06-09 20:04:46 +00:00
|
|
|
secureLocalStorage.clear();
|
2024-06-06 11:05:54 +00:00
|
|
|
return Promise.resolve({
|
|
|
|
|
error,
|
|
|
|
|
logout: true,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|