npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

Iโ€™ve always been into building performant and accessible sites, but lately Iโ€™ve been taking it extremely seriously. So much so that Iโ€™ve been building a tool to help me optimize and monitor the sites that I build to make sure that Iโ€™m making an attempt to offer the best experience to those who visit them. If youโ€™re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, ๐Ÿ‘‹, Iโ€™m Ryan Hefnerย  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If youโ€™re interested in other things Iโ€™m working on, follow me on Twitter or check out the open source projects Iโ€™ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soonโ€“ish.

Open Software & Tools

This site wouldnโ€™t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you ๐Ÿ™

ยฉ 2024 โ€“ย Pkg Stats / Ryan Hefner

@airsoko/auth

v0.0.4

Published

## ๐Ÿ“‘ Table of Contents

Downloads

28

Readme

Airsoko auth package

๐Ÿ“‘ Table of Contents

Overview

The npm package provides essential utilities for handling authentication within the Airsoko application.

Installation

npm install @airsoko/auth

Prerequisites

  • Node.js (version 16 or higher)
  • npm (or yarn)

Uninstalling

If you wish to uninstall Airsoko next, use:

npm uninstall  @airsoko/auth

API Reference

Example

Step 1: Wrap the Application with AuthProvider

To enable authentication within your React application, you need to wrap it with the AuthProvider component provided by the Macive Auth package. This component sets up the necessary authentication context, allowing your components to access authentication-related information.

Here's an example of how to use the AuthProvider:

import { AuthProvider } from "@airsoko/auth;

<AuthProvider
  domain='my-shop.airsoko.com'
  clientId="YOUR_AUTH_CLIENT_ID"
  redirectUri='https://my-shop.airsoko.com'
  audience="CUSTOMER"
>
  {/* Your application components */}
</AuthProvider>

Parameters:

  • domain: The Airsoko domain associated with your application. .

  • clientId: Your Airsoko client ID. Replace "YOUR_AUTH0_CLIENT_ID" with the actual client ID for your application.

  • redirectUri: The URI to which the user will be redirected after authentication. It is usually set to the application's domain or https://my-shop.airsoko.com/my-auth-dwstination'.

  • audience: The audience parameter for Auth0, typically representing the target audience for authentication. In this example, it is set to "CUSTOMER".

Step 2: Authentication Hook use with useAuth

The useAuth hook provides a convenient way to integrate authentication functionalities into your React application. This hook allows you to access user information, check authentication status, and perform actions based on the authentication state.

Usage Example: Private Route Component

Here's an example of how you can use the useAuth hook to create a private route component using Next.js:

import { useAuth } from "@airsoko/auth";
import Link from "next/link";
import React from "react";

type AuthPropsType = {
  permissions?: string[];
};

interface PrivateRouteProps {
  authProps: AuthPropsType;
  children: React.ReactNode;
}

const PrivateRoute: React.FC<PrivateRouteProps> = ({ children, authProps }) => {
  const { user, goLogin, sessionLoading, isAuthenticated } = useAuth();
  const isUser = user?.id;

  React.useEffect(() => {
    if (!sessionLoading) {
      if (!isAuthenticated) {
        if (!isUser) goLogin();
      } else {
        // Additional logic for authenticated users, if needed.
      }
    }
  }, [isUser, sessionLoading]);

  // If the user is authenticated and has the required permissions.
  // Replace with your own permission checking logic if needed.
  if (isUser) {
    // Render the protected content.
    return <>{children}</>;
  }

  // Session is being fetched, or no user.
  // If no user, useEffect() will redirect to the login page.
  return <GlobalLoader />;
};

Functions

1. goLogin Function - Redirect to Login

The goLogin function is designed to redirect users to the login page. This function can be useful in scenarios where you want to ensure users are authenticated before accessing certain parts of your application.

import { useAuth } from "@airsoko/auth";

const YourComponent: React.FC = () => {
  const { goLogin } = useAuth();

  const handleLoginButtonClick = () => {
    goLogin();
  };

  return (
    <div>
      <p>Welcome to your application!</p>
      <button onClick={handleLoginButtonClick}>Login</button>
    </div>
  );
};

2. signIn Function - Sign In and Redirect

The signIn function is designed to sign in the user to the application using a provided token and redirect them to a specific destination. This function is useful when you want to handle custom authentication logic and redirect users after successful sign-in.

import { useAuth } from "@airsoko/auth";

const YourComponent: React.FC = () => {
  const { signIn } = useAuth();

  const handleSignInButtonClick = async () => {
    try {
      // Perform your authentication logic to obtain a token.
      const authToken = "YOUR_OBTAINED_TOKEN";

      // Use the signIn function to sign in the user and redirect.
      await signIn(authToken);

      // After successful sign-in, the user will be redirected.
    } catch (error) {
      console.error("Authentication failed:", error);
      // Handle authentication failure.
    }
  };

  return (
    <div>
      <p>Welcome to your application!</p>
      <button onClick={handleSignInButtonClick}>Sign In</button>
    </div>
  );
};

3. logout Function - Log Out User

The logout function is designed to log out the user from the application. This function can be used to initiate the logout process, which may include clearing session data and redirecting the user to a specified destination.

import { useAuth } from "@airsoko/auth";

const YourComponent: React.FC = () => {
  const { logout } = useAuth();

  const handleLogoutButtonClick = async () => {
    try {
      // Use the logout function to log out the user.
      await logout();

      // After successful logout, you can perform additional actions or redirect the user.
      console.log("User successfully logged out.");
    } catch (error) {
      console.error("Logout failed:", error);
      // Handle logout failure.
    }
  };

  return (
    <div>
      <p>Welcome to your application!</p>
      <button onClick={handleLogoutButtonClick}>Logout</button>
    </div>
  );
};

๐Ÿค Contributing

Contributions to improve this package are welcome. Please adhere to the project's coding standards and commit guidelines.

License

MIT License

โš’๏ธ Built With

  • @types/node

  • typescript


    ๐ŸŒŸ This README was generated with ๐Ÿ’– by Airsoko