Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-passport

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-passport

Passport middleware for Koa

  • 5.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is koa-passport?

koa-passport is a Koa middleware that integrates Passport.js for authentication. It allows you to use various authentication strategies, manage user sessions, and protect routes in a Koa application.

What are koa-passport's main functionalities?

Authentication Strategy

This code demonstrates how to set up a local authentication strategy using koa-passport. It includes initializing the Koa app, setting up the LocalStrategy, and defining a login route.

const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const passport = require('koa-passport');
const LocalStrategy = require('passport-local').Strategy;

const app = new Koa();
const router = new Router();

passport.use(new LocalStrategy((username, password, done) => {
  // Replace with your own logic
  if (username === 'user' && password === 'pass') {
    return done(null, { id: 1, username: 'user' });
  } else {
    return done(null, false);
  }
}));

app.use(bodyParser());
app.use(passport.initialize());
app.use(passport.session());

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

app.use(router.routes());
app.listen(3000);

Session Management

This code demonstrates how to set up session management with koa-passport. It includes initializing session middleware and defining serializeUser and deserializeUser functions.

const session = require('koa-session');

app.keys = ['your-session-secret'];
app.use(session({}, app));

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  // Replace with your own logic
  done(null, { id: 1, username: 'user' });
});

Protecting Routes

This code demonstrates how to protect routes using koa-passport. It includes defining a protected route that requires authentication.

router.get('/protected', passport.authenticate('local', { session: false }), (ctx) => {
  ctx.body = 'This is a protected route';
});

Other packages similar to koa-passport

Keywords

FAQs

Package last updated on 19 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc