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

@server/auth

v0.2.1

Published

Server.js plugin to handle authentication with Github and Twitter

Downloads

31

Readme

Server Auth

An authentication plugin for server.js. So far it handles Github and Twitter login.

import server from "server";
import auth from "@server/auth";

server.plugins.push(auth);

server(ctx => `Hello ${ctx.user ? ctx.user.name : "Anonymous"}`);

Getting Started

We are going to see how to setup Github Login. First install server and this plugin:

npm i server @server/auth

Setup your app in Github and fill in the secrets in your .env file:

GITHUB_ID=
GITHUB_SECRET=

Include the plugin in your main app alongside with a small route with some helper just to see the data:

import server from "server";
import auth from "@server/auth";

server.plugins.push(auth);

const home = ctx => {
  console.log(ctx.user);
  return `Hello ${ctx.user ? ctx.user.name : "Anonymous"}`;
};

server(get("/", home));

Finally start your server with node ., and visit http://localhost:3000/login/github to login into your app. The homepage will show your name, and the terminal all of your info.

Database Integration

The default shown in the Getting Started is the setup just for login. When you have a DB you need to write some extra methods to connect Auth to your specific database data and to the session/cookies. This is achieved with these (all of them are async):

  • findUser(profile) => user: given the profile retrieved from the social network, this function will return the user from your database. You can make the match however you prefer; through the profile.email, or profile.username, or profile.id depending on your needs and whether you have one or multiple providers.
  • createUser(profile) => user: given the profile retrieved from the social network, it creates a user in the database and returns this newly created user.
  • serialize(user) => id: given the user from your database, extract a uniquely identifying ID. This is usually something like the user.id, user._id with Mongoose, or similar. That ID will be stored in the session store for later usage.
  • deserialize(id) => user: given the id extracted from the previous serialization method, this method finds the full user from your database.
import server from "server";
import auth from "@server/auth";

const { get } = server.router;

// Add it to the list of plugins
server.plugins.push(auth);

// Define these 3-4 methods to access your local DB:
const findUser = profile => { ... };
const createUser = profile => { ... };
const serialize = user => user.id;
const deserialize = id => { ... };

// Initialize server.js as usual, passing the options for auth
server(
  { auth: { findUser, createUser, serialize, deserialize } },
  get('/', ctx => `Hello ${ctx.user ? ctx.user.name : 'Anonymous'}`),
);