1.0.31 • Published 7 months ago

@rfrsh/auth-library v1.0.31

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

@rfrsh/auth-library

@rfrsh/auth-library is a powerful authentication service package designed to provide secure authentication and user management functionalities. It includes classes for handling user authentication, user data storage, and user context management.

Installation

npm install @rfrsh/auth-library

OR

yarn add @rfrsh/auth-library

Usage

To use the @rfrsh/auth-library package in your project, import the necessary classes and set up the required configurations. Here's a basic example of how to use the package:

AuthController

To use the @rfrsh/auth-library package in your project, you can create an instance of AuthController and interact with it to perform various authentication-related operations. Here's an example of how to use the AuthController class:

// Import the necessary modules
import AuthController, { WalletContextState } from '@rfrsh/auth-library';

// Initialize the AuthController with the URL of the authentication service
const authController = new AuthController("https://auth-service.rfrsh.io");

// Function to handle login using the connected Solana wallet
const handleLogin = async (wallet: WalletContextState, isTx: boolean = false) => {
  try {
    // Perform the login operation using the AuthController
    await authController.login(wallet, solanaConnection, isTx);

    // Login successful, perform actions accordingly
    console.log("Login successful!");
    console.log("User:", authController.user);
    console.log("Wallets:", authController.wallets);
  } catch (error) {
    // Handle login errors
    console.error("Login failed:", error.message);
  }
};

// Function to handle logout
const handleLogout = async () => {
  try {
    // Perform the logout operation using the AuthController
    await authController.logout();

    // Logout successful, perform actions accordingly
    console.log("Logged out successfully!");
  } catch (error) {
    // Handle logout errors
    console.error("Logout failed:", error.message);
  }
};

// Function to handle adding a new wallet to the user's account
const handleAddWallet = async (wallet: WalletContextState, isTx: boolean = false) => {
  try {
    // Perform the addWallet operation using the AuthController
    await authController.addWallet(wallet, isTx);

    // Wallet addition successful, perform actions accordingly
    console.log("Wallet added successfully!");
    console.log("Updated Wallets:", authController.wallets);
  } catch (error) {
    // Handle addWallet errors
    console.error("Failed to add wallet:", error.message);
  }
};

// Function to handle removing a wallet from the user's account
const handleRemoveWallet = async (walletAddress: string) => {
  try {
    // Perform the removeWallet operation using the AuthController
    await authController.removeWallet(walletAddress);

    // Wallet removal successful, perform actions accordingly
    console.log("Wallet removed successfully!");
    console.log("Updated Wallets:", authController.wallets);
  } catch (error) {
    // Handle removeWallet errors
    console.error("Failed to remove wallet:", error.message);
  }
};

AppService

To interact with the application data, you can use the AppService class. You can fetch app details from the database, get the endpoint URL, access token secret, and fetch user data associated with the app. Here's an example of how to use the AppService class:

import { AppService } from '@rfrsh/auth-library';

// Fetch the app service instance with a custom API endpoint.
const appService = AppService.fetch('https://app-service.example.com');

// Fetch an application from the database using its API key and create a new App instance.
const app = await appService.fetchApp('your-api-key');

// Get the endpoint URL for the app.
const endpoint = app.endpoint;

// Get the Access Token Secret for the app.
const accessTokenSecret = app.accessTokenSecret;

// Fetch a user associated with the app from the database or the cache.
const user = await app.user('user123', true); // Set to true to refetch the user from the database.

User

The UserService class allows you to interact with user data and context management. You can fetch the user's context data from the Redis cache storage and set a new context for the user. Here's an example of how to use the User class:

// Get the user's ID.
const userID = user.id;

// Get the user's information.
const userInfo = user.info;

// Fetch the user's context from the Redis cache storage.
const context = await user.getContext();

// Set the user's context in the Redis cache storage.
const updatedContext = await user.setContext({ theme: 'dark' });

Store and MemoryStore

You can use the Store and MemoryStore classes to store data in memory or create a custom data store. Here's an example of how to use the Store and MemoryStore classes:

import { Store, MemoryStore } from '@rfrsh/auth-library';

// Create a new custom data store extending the Store class.
class CustomStore extends Store<number> {
  // Implement the required methods: get, set, and delete.
}

// Use the default in-memory data store implementation.
const memoryStore = new MemoryStore<string>();

// Set a value in the data store.
memoryStore.set('key1', 'value1');

// Get a value from the data store.
const value = memoryStore.get('key1'); // Returns 'value1'

// Delete a value from the data store.
memoryStore.delete('key1');

Features

  • User Registration: Register new users with their details and associate a Solana wallet with their account.

  • User Login: Allow existing users to log in using their connected Solana wallet.

  • Access Token Management: Obtain and refresh access tokens for authenticated users.

  • User Context: Manage user context data stored in the Redis cache storage.

  • Wallet Management: Add or remove Solana wallets associated with user accounts.

API Documentation

For detailed API documentation and usage instructions, please refer to the API Documentation for this package.

License

This package is released under the MIT License.

Contributions

Contributions to the @rfrsh/auth-library package are welcome! If you find any bugs or want to suggest new features, feel free to open an issue or submit a pull request on the GitHub repository. Your feedback and contributions will help make this package even better!

1.0.31

7 months ago

1.0.30

7 months ago

1.0.29

7 months ago

1.0.28

7 months ago

1.0.27

7 months ago

1.0.26

7 months ago

1.0.25

7 months ago

1.0.24

7 months ago

1.0.23

8 months ago

1.0.22

8 months ago

1.0.21

8 months ago

1.0.20

8 months ago

1.0.19

9 months ago

1.0.18

9 months ago

1.0.17

9 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.13

9 months ago

1.0.12

9 months ago

1.0.11

9 months ago

1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago