ref: change auth setting in Login Screen
This commit is contained in:
parent
9d31ea245c
commit
12c90ff120
26 changed files with 1149 additions and 99 deletions
|
|
@ -1 +1,4 @@
|
|||
export const BASE_URL_AUTHENTICATION = "/AUTHENTICATION-SERVICE/rhi-bpc/"
|
||||
export const BASE_URL_ADMINISTRATION = '/ADMINISTRATION-SERVICE/rhi-bpc/v2/'
|
||||
export const BASE_URL_CUSTOMER_VALIDATION = '/CUSTOMER-SERVICE/rhi-bpc/v2/customer-type-validation-method/'
|
||||
export const BASE_URL_CUSTOMER_TYPEDNI = '/CUSTOMER-SERVICE/rhi-bpc/v2/type-customer-dni/'
|
||||
|
|
|
|||
|
|
@ -3,11 +3,23 @@ import i18n from "@package/utils/language/i18n"
|
|||
|
||||
const BASE_URL = process.env.NEXT_PUBLIC_API_URL
|
||||
|
||||
/**
|
||||
* Fetches data from a specified endpoint and returns a `ReturnService` object.
|
||||
*
|
||||
* @template T - The type of the data expected from the API response.
|
||||
* @param {string} BASE_URL_MODULE - The base URL module to be appended to the base URL.
|
||||
* @param {string} endpoint - The specific API endpoint to fetch data from.
|
||||
* @param {RequestInit} [options={}] - Optional configuration for the fetch request.
|
||||
* @returns {Promise<ReturnService<T>>} A promise that resolves to a `ReturnService` object containing the fetched data.
|
||||
*
|
||||
* @throws {Error} If the fetch request fails or the response is not ok.
|
||||
*/
|
||||
async function FetchData<T>(
|
||||
BASE_URL_MODULE: string,
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
): Promise<ReturnService<T>> {
|
||||
|
||||
const url = `${BASE_URL}${BASE_URL_MODULE}${endpoint}`
|
||||
const text = i18n.t("errorProcessingAPI") as string
|
||||
try {
|
||||
|
|
@ -29,4 +41,4 @@ async function FetchData<T>(
|
|||
}
|
||||
}
|
||||
|
||||
export { FetchData }
|
||||
export { FetchData }
|
||||
|
|
@ -4,6 +4,14 @@ export class ReturnService<T> {
|
|||
errorCode: number
|
||||
object: T
|
||||
|
||||
/**
|
||||
* Constructs an instance of the ReturnService class.
|
||||
*
|
||||
* @param object - The object of type T to be returned.
|
||||
* @param correct - A boolean indicating if the operation was correct. Defaults to true.
|
||||
* @param message - A message providing additional information. Defaults to an empty string.
|
||||
* @param errorCode - An error code associated with the operation. Defaults to 0.
|
||||
*/
|
||||
constructor(object: T, correct = true, message = "", errorCode = 0) {
|
||||
this.correct = correct
|
||||
this.message = message
|
||||
|
|
|
|||
19
package/config/api/Admin/auth/GetToken/page.tsx
Normal file
19
package/config/api/Admin/auth/GetToken/page.tsx
Normal 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)
|
||||
}
|
||||
}
|
||||
68
package/config/api/Admin/auth/Login/page.tsx
Normal file
68
package/config/api/Admin/auth/Login/page.tsx
Normal 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 };
|
||||
66
package/config/api/Admin/auth/ResetPassword/page.tsx
Normal file
66
package/config/api/Admin/auth/ResetPassword/page.tsx
Normal 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 }
|
||||
75
package/config/api/Admin/user/GetTypeUsers/page.tsx
Normal file
75
package/config/api/Admin/user/GetTypeUsers/page.tsx
Normal 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 }
|
||||
78
package/config/api/Admin/user/GetUsers/page.tsx
Normal file
78
package/config/api/Admin/user/GetUsers/page.tsx
Normal 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 };
|
||||
72
package/config/api/Admin/user/SaveUser/page.tsx
Normal file
72
package/config/api/Admin/user/SaveUser/page.tsx
Normal 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 }
|
||||
33
package/config/api/Auth/page.tsx
Normal file
33
package/config/api/Auth/page.tsx
Normal 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 }
|
||||
Loading…
Add table
Add a link
Reference in a new issue