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

connect-authentication

v0.1.0

Published

Simple express credential/authentication middleware

Downloads

11

Readme

Authentication Middleware Build Status

NPM

Codecov block

A simple (opinionated) connect-style authentication middleware.

This middleware directly use jws package to implement a simpler version of JsonWebToken to authenticate the users via cookie or the Authentication header.

Readers can read the source code to get the idea and replace the token encode/decode mechanism with jsonwebtoken, or use this package directly. This package does not follow the standard of jsonwebtoken. However, it is well tested in it owns implementation.

Usage sample

const express = require('express')
const connectAuthentication = require('connect-authentication').default
const cookieParser = require('cookie-parser')
const asyncMiddleware = require('middleware-async').default
const bodyParser = require('body-parser')

const user = {id: '1', first: 'hello', last: 'world', username: 'admin'}
const encode = u => u.id
const decode = id => id === '1' && user
const app = express()
app.use(
		cookieParser('cookie-secret'),
		connectAuthentication(encode, decode, 'jws-secret')
)
app.get('/', (req, res) => res.status(200).send('hello world!'))
app.post('/login',
		bodyParser.json(),
		asyncMiddleware(async (req, res) => {
				const {body: {username, password}} = req
				if (username === 'admin' && password === 'password') {
						const token = await req.login(user)
						res.status(200).json(token)
				} else res.status(401).json({error: 'wrong credential'})
		})
)
app.get('/me', (req, res) => {
		if (req.user) res.status(200).json(req.user)
		else res.status(403).json({error: 'please login'})
})
app.get('/logout', asyncMiddleware(async (req, res) => {
		await req.logout()
		res.send('logout success')
}))
app.listen(3000, () => console.log('Server is listening at port 3000'))

API Reference

Interface of the default export

export default function connectAuthentication<IUser, IPayload>(
		encode: (user: IUser) => CanAwait<IPayload>,
		decode: (payload: IPayload) => CanAwait<IUser | undefined>,
		secret: string | Buffer,
		{
				ttl = '1 week',
				alg = 'HS256',
				encoding = 'utf8',
				cookieKey = 'jwt',
				isTokenRevoked,
				revokeToken,
				cookieOptions = {
						httpOnly: true,
						sameSite: 'lax',
						secure: true,
						signed: false,
				},
		}: {
				ttl?: number | string
				alg?: Algorithm
				encoding?: string
				cookieKey?: string | false
				isTokenRevoked?: (token: string) => CanAwait<boolean>
				revokeToken?: (token: string, expire: Date) => CanAwait<void>
				cookieOptions?: CookieOptions
		} = {}
): RequestHandler