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

gatsby-plugin-netlify-admin

v0.1.1

Published

A gatsby plugin for easily creating admin page via netlify auth

Downloads

30

Readme

gatsby-plugin-netlify-admin

Gatsby + Netlify Identity = Static Site with Identification

Why?

You love Gatsby. You love Netlify. So you host your Gatsby site on Netlify. You love static site. You also want your static site has identity feature.

Here is it. Powered your Gatsby site with Netlify Identity without pain.

Features

  • Minimal configuration to set up Netlify Identity
  • Protected pages with client side identity validation
  • Support client side routing

Demo

Go to https://gatsby-plugin-netlify-admin.netlify.app/

Login as:

How to use

0. Enable the Netlify identity feature

Please follow the Authenticate users with Netlify Identity runbook to enable the Netlify identity in your dev site.

Since the plugin is using netlify-identity-widget under the hood, in dev environment, please follow

1. Install the package

yarn add gatsby-plugin-netlify-admin

2. Create the admin folder under /src

3. Add the plugin to gatsby-config.js

Minimal set up

{
    resolve: 'gatsby-plugin-netlify-admin',
    options: {
        adminPath: `${__dirname}/src/admin`
    }
}

4. Add some pages under /admin folder

Let's say you'd like to have an index admin page and another login page.

.
├── admin
    ├── index.js
    └── login.js

admin/index.js

import React from 'react';

const Admin = (props) => {
    return <div>Admin</div>
}

export default Admin;

admin/login.js

import React from "react"

const Login = props => {
  return <button onClick={props.showNetlifyLoginModal}>Login</button>
}

export default Login

5. Spin up gatsby

gatsby develop

Assume your page is host under http://localhost:8000.

Follow this flow:

> Navigate to http://localhost:8000/admin.
> Will be reidrected to http://localhost:8000/login since you've not logged in
> Click `Login` and the Netlify login modal appears. Log in with correct identity
> Navigate to http://localhost:8000/admin
> You can see the admin page

6. That's it

The setup is simple. The only thing you need to do is creating a folder to host the admin pages and you can reuse the original gatsby usage.

Usage

gatsby-config

All options

{
    resolve: 'gatsby-plugin-netlify-admin',
    options: {
        adminPath: `${__dirname}/src/admin`,
        adminUri: 'admin',
        loginUri: '/admin/login/',
        excludeUri: ['/admin/signup'],
    }
}

| option | type | default | details | |-------------|--------|----------------|-----------------------------------------------------------------------------------------| | adminPath* | string | | The field is required. The folder hosts the admin pages | | adminUri | string | admin | the root admin URI. It means pages will be host under /<admin>/... | | loginUri | string | /admin/login | This is the default redirection page if users are trying to access the auth first pages | | excludeUri | Array | ["/admin/login"] | Pages are eligble to all users |

pages

All pages under the adminPath folder will be injected with Netlify login info.

You can directly access and use these props in your component:

| props name | type | details | | ------------------ | -------- | -------------------------------------------- | | netlifyLogin | Function | The function triggers Netlify login popup | | netlifyLogout | Function | The function signs out current user | | netlifyAdminStatus | Object | The status of current user. values are: {user: Object, isLoggedIn: boolean, isModalOpen: boolean, error: string} |

For example, you can take these props by:

admin/login.js

import React from "react"
import {navigate} from 'gatsby';

const Login = props => {
  if (props.netlifyAdminStatus && props.netlifyAdminStatus.isLoggedIn) {
		navigate('/admin');
    return null;
  }
  return (
      <button onClick={props.netlifyLogin}>Login</button>
  )
}

export default Login

More features

Client side routing

Admin pages usually happen to have client side generated pages. This plugin also makes it easy to configure.

We will utilize the Gatsby client routing to do the trick.

Update your admin/index.js file:

import React from "react"
import { Router } from "@reach/router"
import UserPage from "../components/UserPage"
const App = () => {
  return (
      <Router>
        <UserPage path="/admin/user/:id" component={UserPage} />
      </Router>
  )
}

Then /admin/user/:id will be generated by client side routing. Plus, this route will also be protected by identification since /admin/user/* is not in excludeUri.


Example: https://github.com/ctxhou/gatsby-plugin-netlify-admin/blob/master/example

Reference