Introduction to koa-passport
koa-passport
is a Koa-specific middleware for authentication using Passport.js
. It provides a painless and clean way to implement authentication in Koa-based applications.
Getting Started
First, let’s install koa-passport
and its dependencies:
npm install koa-passport passport
Setting up Koa and Koa-passport
After installing the dependencies, we need to set up Koa and integrate koa-passport
with it:
const Koa = require('koa'); const session = require('koa-session'); const koaPassport = require('koa-passport'); const app = new Koa(); app.keys = ['your-session-secret']; app.use(session({}, app)); app.use(koaPassport.initialize()); app.use(koaPassport.session());
Passport Strategies
Passport.js has a wide range of strategies for different authentication mechanisms. Here is an example of configuring a local authentication strategy:
const LocalStrategy = require('passport-local').Strategy; koaPassport.use(new LocalStrategy((username, password, done) => { // Your verification logic const user = { id: 1, username: 'test' }; // Example user if (username === 'test' && password === 'password') { return done(null, user); } else { return done(null, false); } }));
Serialization and Deserialization
Passport requires serialization and deserialization of users for session management:
koaPassport.serializeUser((user, done) => { done(null, user.id); }); koaPassport.deserializeUser((id, done) => { const user = { id: 1, username: 'test' }; // Example user matching id done(null, user); });
Authentication Middleware
We then create authentication routes using the authenticate
method:
const Router = require('koa-router'); const router = new Router(); router.post('/login', koaPassport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' }));
You can also protect routes using koa-passport
:
router.get('/profile', (ctx, next) => { if (ctx.isAuthenticated()) { ctx.body = 'Profile page'; } else { ctx.redirect('/login'); } });
Complete Example App
Below is a complete example integrating all the above pieces:
const Koa = require('koa'); const session = require('koa-session'); const koaPassport = require('koa-passport'); const LocalStrategy = require('passport-local').Strategy; const Router = require('koa-router'); const app = new Koa(); app.keys = ['your-session-secret']; app.use(session({}, app)); app.use(koaPassport.initialize()); app.use(koaPassport.session()); koaPassport.serializeUser((user, done) => { done(null, user.id); }); koaPassport.deserializeUser((id, done) => { const user = { id: 1, username: 'test' }; done(null, user); }); koaPassport.use(new LocalStrategy((username, password, done) => { const user = { id: 1, username: 'test' }; if (username === 'test' && password === 'password') { return done(null, user); } else { return done(null, false); } })); const router = new Router(); router.post('/login', koaPassport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' })); router.get('/profile', (ctx, next) => { if (ctx.isAuthenticated()) { ctx.body = 'Profile page'; } else { ctx.redirect('/login'); } }); app.use(router.routes()).use(router.allowedMethods()); app.listen(3000, () => { console.log('Server started on port 3000'); });
With this setup, you can easily build a Koa application with authentication using koa-passport
.
Hash: 0af30059afb9c999ffd46e8ea9969b45e8ab236a395e0a27b3c28aa12382f142