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

netlify-gated-components

v1.0.11

Published

show/hide components based on login state using Netlify Identity in React

Downloads

25

Readme

GatedComponent

This is an NPM package that creates a React component for gated content functionality using Netlify Identity. It allows you to restrict access to certain parts of your application based on the user's authentication status.

Or more simply understood, put anything that needs authentication inside a <GatedComponent></GatedComponent> tag and it will only be rendered if the user is logged in.

If you want to render a login form when the user is not authenticated, you can pass a component as a prop to the noAccessContent prop.

Styling is not included in the package.

Installation

To install this package, run the following command:

# npm
npm install netlify-gated-components
# or yarn
yarn add netlify-gated-components
# or pnpm
pnpm add netlify-gated-components

Install dependencies

You also need to install the netlify-identity-widget package to use this component. You can install it by running the following command:

# npm
npm install netlify-identity-widget
# or yarn
yarn add netlify-identity-widget
# or pnpm
pnpm add netlify-identity-widget

# Install types for TypeScript
npm install @types/netlify-identity-widget
# or yarn
yarn add @types/netlify-identity-widget
# or pnpm
pnpm add @types/netlify-identity-widget

Import the package into your React component:

import GatedComponent from 'netlify-gated-components'

Usage

To use the GatedComponent component, you need to wrap the content you want to restrict access to inside the component. You can also specify a component to render when the user is not authenticated.

Note: To use this package, you need to pass the netlifyIdentity object as a prop to the GatedComponent component.

Props

  • netlifyIdentity (required): The Netlify Identity object.
  • children (required): The content to render when the user is authenticated. Takes a valid React component.
  • noAccessContent (optional): The component to render when the user is not authenticated.
  • reloadOnLogin (optional): A boolean value that determines whether the page should be reloaded when the user logs in and out. Default is true.

Example

'use client'
import React, { useState, useEffect } from 'react'
import netlifyIdentity from 'netlify-identity-widget'
import GatedComponent from 'netlify-gated-components'

function Component() {
  const [userName, setUserName] = useState('')

  useEffect(() => {
    setUserName(
      netlifyIdentity?.currentUser()?.user_metadata?.full_name || 'Anon'
    )
  }, [])

  const noAccessContent = (
    <div className="gated-content-no-access">
      <h4 className="gated-content-no-access-heading">
        🔓 You must be logged in to see this content.
      </h4>
      <div className="gated-content-no-access-button-group">
        <input
          type="button"
          value="Log in"
          onClick={() => netlifyIdentity.open('login')}
          className="gated-content-no-access-button"
          id="gated-content-no-access-login-button"
        />
        <input
          type="button"
          value="Sign up"
          onClick={() => netlifyIdentity.open('signup')}
          className="gated-content-no-access-button"
          id="gated-content-no-access-signup-button"
        />
      </div>
    </div>
  )

  const content = (
    <div className="gated-content">
      <h4 className="gated-content-heading">👋 Logged in as {userName}.</h4>
      <div className="gated-content-button-group">
        <input
          type="button"
          value="Log out"
          onClick={() => netlifyIdentity.logout()}
        />
      </div>
    </div>
  )

  return (
    <div>
      <GatedComponent
        netlifyIdentity={netlifyIdentity}
        noAccessContent={noAccessContent}
        reloadOnLogin={true}>
        {content}
      </GatedComponent>
    </div>
  )
}

export default Component

License

MIT Open Source.

Author

Contributing

Contributions are welcome! Feel free to submit a pull request or open an issue if you have any questions or suggestions.

https://github.com/neontomo/netlify-gated-components-npm