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

use-discord

v1.1.5

Published

A React hook for logging in with Discord

Downloads

16

Readme

use-discord

A React context hook to log in using a Discord identity without a server-side component.

The code will refresh login and role status every hour. I use a service worker to handle the redirect_uri. I'll work on getting that added as something you can import and stick in your fetch handler.

Installation

npm i use-discord

Use

In your app's root somewhere:

    <DiscordProvider
      client_id={'969261945310031913'}
      guilds={['783410937902006374', '551763693378076683']}
    >
      <TheRestOfYourApp />
    </DiscordProvider>

In a component hook:

const MyComponent = () => {
  const discord = useDiscord();
  return <Menu>
    {
      discord.isLoggedIn
        ? <MenuItem onClick={discord.logout}>Log out of Discord</MenuItem>
        : <MenuItem onClick={discord.login}>
            <Typography>Log in to Discord</Typography>
          </MenuItem>
    }
  </Menu>
}

In your service worker:

const { onMessageHandler, fetchHandler } = require('use-discord');

addEventListener('fetch', (event) => {
  if (fetchHandler(event)) return;

  ...
});
addEventListener('message', onMessageHandler);

Provider options:

client_id

The client ID you get when registering your "app" with discord.

guilds

An array of strings that represent all the guilds you wish to look for roles on.

scopes

An array of strings that represent the scopes that this hook will request. Default: ['identify', 'guilds.members.read']

redirect_uri

Where discord will send the user during authorization flow. Default: ${location.origin}/discord

Available actions

login

Initiate the login flow.

logout

Log the user out in this app. This will reset the state as well.

Available state

avatar

String - A url that points to an avatar of the user. For example:

'https://cdn.discordapp.com/avatars/8574465478869584/7437496069584a98685e96968.png?size=32'

isLoggedIn

Boolean - true if the user is logged in, otherwise false.

roles

Dictionary - A map of guild to array of roles that a uer has. For example:

{
  '12325234234234234': ['324234232523412341241', '23423512342345234123']
}

uid

String - The user id in discord. For example:

'235436523432652'

username

String - The username for the user. For example:

'ftreesmilo'