ref: change auth setting in Login Screen

This commit is contained in:
Luis Felipe Cadavid 2024-09-24 17:35:35 -05:00
parent 9d31ea245c
commit 12c90ff120
26 changed files with 1149 additions and 99 deletions

View file

@ -0,0 +1,19 @@
import { auth } from "@package/config/api/Auth/page"
import { API_PASS_SERVICE, API_USER_TOKEN_SERVICE } from "@package/utils/Constants"
/**
* Retrieves an authentication token using the provided user token service and password service.
*
* @returns {Promise<any>} The authentication token.
* @throws {Error} If `API_USER_TOKEN_SERVICE` or `API_PASS_SERVICE` is undefined.
*/
export async function GetToken() {
try {
if (!API_USER_TOKEN_SERVICE || !API_PASS_SERVICE) {
throw new Error("API_USER_TOKEN_SERVICE or API_PASS_SERVICE is undefined");
}
return await auth(API_USER_TOKEN_SERVICE, API_PASS_SERVICE)
} catch (error) {
console.error('error' + error)
}
}

View file

@ -0,0 +1,68 @@
import { GetToken } from '@package/config/api/Admin/auth/GetToken/page';
import { BASE_URL_ADMINISTRATION } from '@package/config/ApiPath';
import { FetchData } from '@package/config/FetchData';
import { ReturnService } from '@package/config/ReturnService';
import i18n from '@package/utils/language/i18n';
import { deleteKeyApi, deleteUser, encrypt, setKeyApi, setUser } from '@package/utils/Utilities';
interface LoginResponse {
correct?: boolean;
success: boolean;
message: string;
errorCode?: number;
}
/**
* Authenticates a user by their username and password.
*
* @param {string} ae_user - The username of the user.
* @param {string} ae_password - The password of the user.
* @returns {Promise<LoginResponse>} A promise that resolves to a LoginResponse object indicating the success or failure of the login attempt.
*
* @throws Will return an error response if the token retrieval fails, the credentials are incorrect, or an unexpected error occurs.
*/
async function Login(ae_user: string, ae_password: string): Promise<LoginResponse> {
try {
const token = await GetToken();
if (!token) {
return { success: false, message: i18n.t('errorTokenAPI'), errorCode: 102 };
}
ae_password = encrypt(ae_password);
const options: RequestInit = {
method: 'post',
mode: 'cors' as RequestMode,
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
lng: i18n.language,
},
body: JSON.stringify({
email: ae_user,
password: ae_password,
}),
};
const data: ReturnService<any> = await FetchData(BASE_URL_ADMINISTRATION, 'user/login', options);
if (data.correct && data.object) {
setKeyApi(token);
setUser(data.object.object);
return data.object;
} else {
deleteKeyApi();
deleteUser();
return { success: false, message: data.message || i18n.t('msj_Incorrect_credentials'), errorCode: data.errorCode };
}
} catch (error) {
deleteKeyApi();
deleteUser();
return { success: false, message: i18n.t('errorUnexpected'), errorCode: 500 };
}
}
export { Login };

View file

@ -0,0 +1,66 @@
import { GetToken } from "@package/config/api/Admin/auth/GetToken/page"
import { BASE_URL_ADMINISTRATION } from "@package/config/ApiPath";
import { FetchData } from "@package/config/FetchData";
import { ReturnService } from "@package/config/ReturnService";
import i18n from "@package/utils/language/i18n";
import { encrypt } from "@package/utils/Utilities";
const BASE_URL_LANGUAGE = i18n.language
interface AeOptions {
id?: string;
idSubsidiary?: number;
odlPassword?: string;
password?: string;
}
/**
* Resets the password for a user.
*
* @param {AeOptions} ae_options - The options for the reset password request.
* @returns {Promise<ReturnService>} A promise that resolves to a ReturnService object indicating the result of the operation.
*
* @throws {Error} If there is an issue with fetching the token or making the API request.
*
* The function performs the following steps:
* 1. Retrieves an authentication token using the `GetToken` function.
* 2. If the token is empty, returns a `ReturnService` object indicating an error.
* 3. Constructs the request options, including headers and body, for the API call.
* 4. Makes a POST request to the `user/resetPassword` endpoint of the administration API.
* 5. If the response indicates success, returns the response object as a `ReturnService` object.
* 6. Otherwise, returns the original response data.
*/
async function ResetPassword(ae_options: AeOptions) {
const token = await GetToken()
if (token === '') {
return new ReturnService('', false, i18n.t('errorTokenAPI'), 102)
}
const options = {
method: 'post',
mode: 'cors' as RequestMode,
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
lng: BASE_URL_LANGUAGE
},
body: JSON.stringify({
id: ae_options.id ?? '',
idSubsidiary: ae_options.idSubsidiary ?? 0,
odlPassword: encrypt(ae_options.odlPassword ?? '') ?? '',
password: encrypt(ae_options.password ?? '') ?? ''
})
}
const data = await FetchData(BASE_URL_ADMINISTRATION, 'user/resetPassword', options)
let result = data;
if (data.correct) {
result = data.object as ReturnService<unknown>
}
return result
}
export { ResetPassword }

View file

@ -0,0 +1,75 @@
import { BASE_URL_ADMINISTRATION } from "@package/config/ApiPath"
import { FetchData } from "@package/config/FetchData"
import { ReturnService } from "@package/config/ReturnService"
import { GRID_SIZE } from "@package/utils/Constants"
import i18n from "@package/utils/language/i18n"
import { getKeyApi } from "@package/utils/Utilities"
const BASE_URL_LANGUAGE = i18n.language
interface AeObject {
id?: string;
name?: string;
idCompany?: string;
idSubsidiary?: number;
isActive?: boolean;
size?: number;
page?: number;
}
/**
* Fetches a list of user types from the administration API.
*
* @param {AeObject} ae_object - The object containing parameters for the request.
* @param {string} ae_object.id - The ID of the user type (optional).
* @param {string} ae_object.name - The name of the user type (optional).
* @param {string} ae_object.idCompany - The ID of the company (optional).
* @param {string} ae_object.idSubsidiary - The ID of the subsidiary (optional).
* @param {boolean} ae_object.isActive - The active status of the user type (optional, defaults to true).
* @param {number} ae_object.size - The number of items per page (optional, defaults to GRID_SIZE).
* @param {number} ae_object.page - The page number (optional, defaults to 0).
* @returns {Promise<ReturnService>} A promise that resolves to a ReturnService object containing the result of the API call.
*/
async function GetTypeUsers(ae_object: AeObject): Promise<ReturnService<any>> {
const token = getKeyApi()
if (token === '') {
return new ReturnService('', false, i18n.t('errorTokenAPI'), 102)
}
if (ae_object.size == null || ae_object.size <= 0) {
ae_object.size = GRID_SIZE
}
if (ae_object.page == null || ae_object.page <= 0) {
ae_object.page = 0
}
const options = {
method: 'post',
mode: 'cors' as RequestMode,
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
lng: BASE_URL_LANGUAGE
},
body: JSON.stringify({
id: ae_object.id ?? '',
name: ae_object.name ?? '',
idCompany: ae_object.idCompany ?? '',
idSubsidiary: ae_object.idSubsidiary ?? 0,
isActive: ae_object.isActive ?? true,
size: ae_object.size,
page: ae_object.page
})
}
const data = await FetchData(BASE_URL_ADMINISTRATION, 'type-user/all', options)
let result = data;
if (data.correct) {
result = data.object as ReturnService<unknown>
}
return result
}
export { GetTypeUsers }

View file

@ -0,0 +1,78 @@
import { BASE_URL_ADMINISTRATION } from "@package/config/ApiPath";
import { FetchData } from "@package/config/FetchData";
import { GRID_SIZE } from "@package/utils/Constants";
import i18n from "@package/utils/language/i18n";
import { getKeyApi } from "@package/utils/Utilities";
interface AeObject {
size: number | null;
page: number | null;
type?: string;
idSubsidiary?: number;
idCompany?: number;
name?: string;
email?: string;
isActive?: boolean | null;
}
interface GetUsersResponse {
correct: boolean;
message: string;
errorCode: number;
object?: any;
}
async function GetUsers(ae_object: AeObject): Promise<GetUsersResponse> {
const token = getKeyApi();
if (!token) {
return { correct: false, message: i18n.t('errorTokenAPI'), errorCode: 102, object: null };
}
if (ae_object.size === null || ae_object.size <= 0) {
ae_object.size = GRID_SIZE;
}
if (ae_object.page === null || ae_object.page <= 0) {
ae_object.page = 0;
}
const options = {
method: 'post',
mode: 'cors' as RequestMode,
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
lng: i18n.language,
},
body: JSON.stringify({
idType: ae_object.type ?? '',
idSubsidiary: ae_object.idSubsidiary ?? 0,
idCompany: ae_object.idCompany ?? 0,
name: ae_object.name ?? '',
mail: ae_object.email ?? '',
isActive: ae_object.isActive ?? null,
size: ae_object.size,
page: ae_object.page,
}),
};
// Llamada a FetchData
const response = await FetchData<any>(BASE_URL_ADMINISTRATION, 'user/all', options);
if (response.correct) {
return {
correct: true,
message: "OK",
errorCode: 0,
object: response.object,
};
} else {
return {
correct: false,
message: response.message,
errorCode: response.errorCode,
object: null,
};
}
}
export { GetUsers };

View file

@ -0,0 +1,72 @@
import { BASE_URL_ADMINISTRATION } from "@package/config/ApiPath";
import { FetchData } from "@package/config/FetchData";
import { ReturnService } from "@package/config/ReturnService";
import { DEFAULT_PASSWORD } from "@package/utils/Constants";
import i18n from "@package/utils/language/i18n";
import { encrypt, getKeyApi } from "@package/utils/Utilities";
const BASE_URL_LANGUAGE = i18n.language
interface UserObject {
id?: string;
idSubsidiary?: number;
idCompany?: number;
name?: string;
lastName?: string;
email?: string;
cellphone?: string;
password?: string;
type?: string;
isActive?: boolean;
isAdmin?: boolean;
}
async function SaveUser(ae_object: UserObject) {
const token = getKeyApi()
if (token === '') {
return new ReturnService('', false, i18n.t('errorTokenAPI'), 102)
}
if ((ae_object.id ?? '').trim().length <= 0 && (ae_object.password ?? '').trim().length <= 0) {
ae_object.password = DEFAULT_PASSWORD
}
if ((ae_object.password ?? '').trim().length > 0) {
ae_object.password = encrypt(ae_object.password ?? '')
}
const options = {
method: 'post',
mode: 'cors' as RequestMode,
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
lng: BASE_URL_LANGUAGE
},
body: JSON.stringify({
id: (ae_object.id ?? '').trim(),
idSubsidiary: ae_object.idSubsidiary ?? 0,
idCompany: ae_object.idCompany ?? 0,
name: ae_object.name ?? '',
lastName: ae_object.lastName ?? '',
email: ae_object.email ?? '',
cellphone: ae_object.cellphone ?? '',
password: ae_object.password ?? '',
idTypeUser: ae_object.type ?? '',
isActive: ae_object.isActive ?? false,
isAdmin: ae_object.isAdmin ?? false
})
}
const data = await FetchData(BASE_URL_ADMINISTRATION, 'user/save', options)
let result = data;
if (data.correct) {
result = data.object as ReturnService<unknown>
}
return result
}
export { SaveUser }

View file

@ -0,0 +1,33 @@
import { BASE_URL_AUTHENTICATION } from "@package/config/ApiPath"
import { FetchData } from "@package/config/FetchData"
async function auth(ae_user: string, ae_password: string): Promise<string> {
const options: RequestInit = {
method: "POST",
mode: 'cors',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: ae_user,
password: ae_password,
}),
}
const data = await FetchData<{ token: string }>(BASE_URL_AUTHENTICATION, "login", options)
console.log(data)
if (data.correct) {
if (data.object) {
return data.object.token
} else {
console.error("Error: data.object is null or undefined")
return "error en la autenticación"
}
} else {
console.error("Error en la autenticación:", data.message)
return "error en la autenticación, mensaje: " + data.message
}
}
export { auth }