Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

abp-react

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abp-react

ABP-REACT is a versatile React library that provides convenient tools and utilities for integrating with the ABP (ASP.NET Boilerplate) framework in your React applications.

latest
Source
npmnpm
Version
1.0.70
Version published
Weekly downloads
32
433.33%
Maintainers
1
Weekly downloads
 
Created
Source

Abp-React

ABP-REACT is a versatile React library that provides convenient tools and utilities for integrating with the ABP (ASP.NET Boilerplate) framework in your React applications. This library simplifies common tasks such as authentication, localization, and accessing ABP-related functionalities.

Installation

npm install abp-react

Usage

1. Importing the library

import { abp, AbpWrapper, initialiseApp } from 'abp-react';
import { useAbp, useAbpUser } from 'abp-react';
import { login, clearAuthCookies } from 'abp-react';
import { L } from 'abp-react';

2. Initializing the App

A. With AbpWrapper wrap your main application component with AbpWrapper to enable ABP integration. Pass a fallback function to be seen while loading the configuration from ASP.NET Boilerplate.

function App() {
  return (
    <AbpWrapper baseUrl="your base url" fallback={<Loading />}>
      {/* Your application components */}
    </AbpWrapper>
  );
}

Optionally you can specify a "tenantId" to override the default one.

B. Without AbpWrapper If you're not using the AbpWrapper component, initialize your application with the configuration response from the ABP backend.

const abpConfigResponse = initialiseApp(abpConfigResponse); // Fetch configuration from ABP backend;

Hooks

useAbp

Access ABP instance and its functionalities using the useAbp hook.

function MyComponent() {
  const abpInstance = useAbp();
  const { abp } = abpInstance;

  // Use abpInstance methods and properties
}

useAbpUser

Retrieve information about the current user with the useAbpUser hook. Should only be used inside "AbpWrapper" component

function UserProfile() {
  const user = useAbpUser<MyUserType>();

  // Access user information
}

For simplicity you can create an abstraction over the useAbpUser hook. If no user type is provided useAbpUser will return ANY.

4. Authentication

Login

Perform user login using the login method. Perform the login operation and pass "expireInSeconds", "accessToken", "encryptedAccessToken"

async function loginUser(username, password) {
  let loginResponse = ...

  try {
    await login(loginResponse);
    // Successful login logic
  } catch (error) {
    // Handle login error
  }
}

Clear Auth Cookies

Clear authentication cookies using clearAuthCookies.

function logoutUser() {
  clearAuthCookies();
  // Additional logout logic
}

5. Localization

Access localization resources using the L function.

function MyLocalizedComponent() {
  const localizedText = L('key', 'your source name here');

  // Use localizedText in your component
}

We recommend overriding the "L" function so it would be easier to use.

function customL(key) {
  const sourceName = process.env.SOURCE_NAME;

  return L(key, sourceName);
}

Contribution

Feel free to contribute to the abp-react library. Fork the repository, make your changes, and submit a pull request. Your contributions are highly appreciated!

Keywords

abp

FAQs

Package last updated on 18 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts