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 🙏

© 2025 – Pkg Stats / Ryan Hefner

passport-koa

v1.0.1

Published

koa framework plugin for passport.js

Readme

passport-koa

koa framework plugin for passport.js

Usage

const Koa = require('koa');
const Router = require('koa-router');
const passportKoa = require('passport-koa');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mysql = require('mysql2/promise');
const session = require('koa-session');
const pool = mysql.createPool({
  "host": "127.0.0.1",
  "port": 3306,
  "user": "root",
  "password": "passpord",
  "database": "your database name",
  "namedPlaceholders": true
});

const CONFIG = {
  key: 'koa:sess', 
  maxAge: 86400000,
  autoCommit: true,
  overwrite: true,
  httpOnly: true,
  signed: true,
  rolling: false,
  renew: false,
};

const queryOne = async (sql, params) => {
  let connection = null;
  try {
    connection = await pool.getConnection();
    const [rows] = await connection.query(sql, params);
    return rows[0];
  } catch (error) {
    console.error(sql);
    console.error(error);
  } finally {
    if (connection) {
      connection.release();
    }
  }
  return 'error';
};

const app = new Koa();
app.keys= ['lwt'];
const router = new Router();

// use passport-koa
passport.framework(passportKoa);

// use passport as always
passport.use('local', new LocalStrategy({ passReqToCallback: true, }, async function (reqest, username, password, done) {
  try {
    const result = await queryOne(`SELECT id FROM account WHERE name=:username`, {
      username,
    });
    console.log(result)
    if (result) {
      done(null, result)
    } else {
      done(null, false);
    }
  } catch (err) {
    await done(err);
  } 
}))

passport.serializeUser(function(user, done) {
  done(null, user);
});

passport.deserializeUser(function(user, done) {
  done(null, user);
});

router.get('/', passport.authenticate('local', {
  successRedirect: '/success',
  failureRedirect: '/failure',
}));

router.get('/success', async (ctx, next) => {
  console.log(ctx.session.passport.user);
  ctx.body = 'success';
  await next();
});

router.get('/failure', async (ctx, next) => {
  console.log(ctx.session.passport.user);
  ctx.body = 'failure';
  ctx.logout();
  await next();
})

router.get('/middle', passport.authenticate('local'), async (ctx, next) => {
  console.log(ctx.req.user);
  console.log(ctx.user);
  console.log(ctx.isAuthenticated());
  ctx.body = 'middle';
  await next();
})

router.get('/changename', passport.authenticate('local'), async (ctx, next) => {
  console.log(ctx.req.current);
  console.log(ctx.current);
  ctx.body = 'changename';
  await next();
})

router.get('/login', async (ctx, next) => {
  const user = { username: 'lwt', id: 1 };
  ctx.login(user, function(err) {
    if (err) { return next(err); }
    return ctx.redirect('/users');
  });
})

router.get('/users', async(ctx, next) => {
  console.log(ctx.session.passport.user);
  ctx.body = 'user';
  await next();
})

app.use(session(CONFIG, app));

app.use(passport.initialize({
  // use ctx.current to access user info
  // userProperty: 'current'
}))

app
  .use(router.routes())
  .use(router.allowedMethods());

app.listen(3000, () => {
  console.log('listen 3000')
});

only difference between passport.js usage is passport.framework(passport-koa);