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

@webiny/app-security

v5.41.0

Published

[![](https://img.shields.io/npm/dw/@webiny/app-security.svg)](https://www.npmjs.com/package/@webiny/app-security) [![](https://img.shields.io/npm/v/@webiny/app-security.svg)](https://www.npmjs.com/package/@webiny/app-security) [![code style: prettier](ht

Downloads

5,388

Readme

@webiny/app-security

code style: prettier PRs Welcome

Exposes a simple SecurityProvider React provider component and enables you to quickly retrieve the currently signed-in user via the useSecurity React hook.

Install

npm install --save @webiny/app-security

Or if you prefer yarn:

yarn add @webiny/app-security

Quick Example

First, make sure you mount the SecurityProvider React provider component in your application's entrypoint component, for example the App component:

import React from "react";
import { Routes } from "@webiny/app/components/Routes";
import { BrowserRouter } from "@webiny/react-router";
import { SecurityProvider } from "@webiny/app-security";
import Authenticator from "./components/Authenticator";

export const App = () => (
    <>
        {/*
        <SecurityProvider> is a generic provider of identity information. 3rd party identity providers (like Cognito,
        Okta, Auth0) will handle the authentication, and set the information about the user into this provider,
        so other parts of the system have a centralized place to fetch user information from.
    */}
        <SecurityProvider>
            {/* This is the component that might trigger the initial authentication
        process and set the retrieved user data into the Security provider.*/}
            <Authenticator>
                <BrowserRouter basename={process.env.PUBLIC_URL}>
                    <Routes />
                </BrowserRouter>
            </Authenticator>
        </SecurityProvider>
    </>
);

A simple Authenticator React component (uses Amazon Cognito and AWS Amplify's Auth class):

import React, { useEffect } from "react";
import { Auth } from "@aws-amplify/auth";
import { useSecurity, SecurityIdentity } from "@webiny/app-security";

// Apart from the React component, we also configure the Auth class here.
Auth.configure({
    region: process.env.REACT_APP_USER_POOL_REGION,
    userPoolId: process.env.REACT_APP_USER_POOL_ID,
    userPoolWebClientId: process.env.REACT_APP_USER_POOL_WEB_CLIENT_ID,
    oauth: {
        domain: process.env.REACT_APP_USER_POOL_DOMAIN,
        redirectSignIn: `${location.origin}?signIn`,
        redirectSignOut: `${location.origin}?signOut`,
        responseType: "token"
    }
});

interface AuthenticatorProps {
    children: React.ReactNode;
}
// The `Authenticator` component.
const Authenticator = (props: AuthenticatorProps) => {
    const { setIdentity } = useSecurity();

    useEffect(() => {
        // Get the currently signed-in user.
        Auth.currentSession().then(response => {
            const user = response.getIdToken().payload;
            setIdentity(
                new SecurityIdentity({
                    login: user.email,
                    firstName: user.given_name,
                    lastName: user.family_name,
                    logout: () => {
                        Auth.signOut();
                        setIdentity(null);
                    }
                })
            );
        }).catch(() => { /* Do nothing. */ });
    }, []);

    return <>{props.children}</>;
};

export default Authenticator;

Finally, use the useSecurity React hook in any of your components:

import React from "react";
import { useSecurity } from "@webiny/app-security";

const MyComponent = () => {
  const { identity } = useSecurity();

  if (identity) {
    return <>Logged in.</>;
  }

  return <>Not logged in.</>;
};

export default MyComponent;