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

@first-line/firstline-react

v1.0.11

Published

React Library for Firstline IDP

Downloads

119

Readme

Firstline React

This library enables you to add authentication to your React app.

Helpful resources

  • Quick setup - our guide for quickly adding login, logout and user information to a React app using Firstline.
  • React sample app - a full-fledged React application integrated with Firstline.
  • Firstline docs - explore our docs site and learn more about Firstline.

Getting started

1. Setup Firstline Application & API

  1. Follow the Quick setup to configure a Firstline Application.
  2. Add a Firstline API as shown in Secure API.

Important: Don't forget to configure the Application URIs.

2. Installation

Using npm:

npm install @first-line/firstline-react

Using yarn:

yarn add @first-line/firstline-react

Hint: You can also try out our React sample app.

3. Wrap your React app with Firstline context.

Wrap your app with the Firstline context. Replace DOMAIN, API_IDENTIFIER and CLIENT_ID with the settings you configured in the setup step. You can also find them in the Application's and API's "Configure" tab in your dashboard.

// This file is often named index.jsx or _app.jsx
import { FirstlineContext } from "@first-line/firstline-react";

const App = () => {
  return (
    <FirstlineContext
      audience="API_IDENTIFIER"
      domain="DOMAIN"
      client_id="CLIENT_ID"
      redirect_uri={window.location.origin}
    >
    ...  {/* your existing components */}
    </FirstlineContext>
  );
};

4. Add login & logout to your application

Implement the following component in your frontend and you have a fully functional login/logout.

import { useFirstline } from "@first-line/firstline-react";

const MyComponent = () => {
  const { user, isAuthenticated, loginWithRedirect, logout } = useFirstline();
  
  return (
    <>
      {isAuthenticated ? (
        <div>
          <p>{user.email}</p>
          <button onClick={() => logout()}>Logout</button>
        </div>
      ) : (
        <button onClick={() => loginWithRedirect()}>Login</button>
      )}
    </>
  );
};

You can call useFirstline() from anywhere in your application to

  • log in
  • log out
  • check if the user is signed in
  • retrieve the logged in user

5. Make a secured backend call

Here is sample code on how to make an API request to a secured endpoint.

import { useState } from "react";
import { useFirstline } from "@first-line/firstline-react";

const Posts = () => {
  const { getAccessToken } = useFirstline();
  const [posts, setPosts] = useState([]);

  const loadPosts = async () => {
    const response = await fetch("http://localhost:8080/posts", {
      headers: {
        Authorization: `Bearer ${getAccessToken()}`,
      },
    });
    const data = await response.json();
    setPosts(data);
  };

  return (
    <div>
      <button onClick={loadPosts}>Load Posts</button>
      <ul>
        {posts.map(post => (
          <li key={post.id}>{post.text}</li>
        ))}
      </ul>
    </div>
  );
};

In this example, we assume that the API endpoint http://localhost:8080/posts exists.

Important: The user must be logged in when calling getAccessToken(). Therefore, use isAuthenticated.