Skip to content
Skillv1.0.0

passport-js

Add authentication to Express with Passport.js. Use when implementing login with Google/GitHub/email, OAuth, session auth, or social login.

by terminalskills(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from terminalskills/skills (skills/passport-js/SKILL.md). Install upstream with npx skills add terminalskills/skills --skill passport-js. Copyright stays with the author (Apache-2.0).

Passport.js

Overview

Passport.js is the most popular auth middleware for Express with 500+ strategies (Google, GitHub, Facebook, SAML, LDAP, local). Session-based by default, works with JWTs too.

Instructions

Step 1: Local Strategy

import passport from 'passport'
import { Strategy as LocalStrategy } from 'passport-local'
import bcrypt from 'bcrypt'

passport.use(new LocalStrategy(
  { usernameField: 'email' },
  async (email, password, done) => {
    const user = await db.users.findByEmail(email)
    if (!user) return done(null, false, { message: 'No account' })
    if (!await bcrypt.compare(password, user.passwordHash)) return done(null, false)
    return done(null, user)
  }
))

passport.serializeUser((user: any, done) => done(null, user.id))
passport.deserializeUser(async (id, done) => done(null, await db.users.findById(id)))

Step 2: Google OAuth

import { Strategy as GoogleStrategy } from 'passport-google-oauth20'

passport.use(new GoogleStrategy({
  clientID: process.env.GOOGLE_CLIENT_ID!,
  clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
  callbackURL: '/auth/google/callback',
}, async (accessToken, refreshToken, profile, done) => {
  let user = await db.users.findByOAuthId('google', profile.id)
  if (!user) user = await db.users.create({
    email: profile.emails![0].value,
    name: profile.displayName,
    oauthProvider: 'google', oauthId: profile.id,
  })
  return done(null, user)
}))

Step 3: Routes

app.post('/login', passport.authenticate('local', {
  successRedirect: '/dashboard', failureRedirect: '/login',
}))
app.get('/auth/google', passport.authenticate('google', { scope: ['profile', 'email'] }))
app.get('/auth/google/callback', passport.authenticate('google', {
  successRedirect: '/dashboard', failureRedirect: '/login',
}))

Guidelines

  • Hash passwords with bcrypt (cost 12+). Never store plaintext.
  • Use express-session with Redis store in production.
  • Implement CSRF protection with session-based auth.
  • Link accounts: let users connect multiple OAuth providers to one account.

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/terminalskills-skills-passport-js/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

terminalskills-skills-passport-js.ocm.jsonjson
{
  "ocm": "1",
  "id": "terminalskills-skills-passport-js",
  "kind": "skill",
  "name": "passport-js",
  "description": "Add authentication to Express with Passport.js. Use when implementing login with Google/GitHub/email, OAuth, session auth, or social login.",
  "publisher": "terminalskills",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "general"
    ],
    "tags": [
      "skill-md",
      "passport",
      "authentication",
      "oauth",
      "express",
      "social-login",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "Add authentication to Express with Passport.js. Use when implementing login with Google/GitHub/email, OAuth, session auth, or social login."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/terminalskills/skills",
      "path": "skills/passport-js/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/terminalskills/skills/blob/HEAD/skills/passport-js/SKILL.md",
      "key": "terminalskills/skills/skills/passport-js/SKILL.md"
    },
    "compatibility": "Express, Node.js",
    "license": "Apache-2.0"
  },
  "instructions": "# Passport.js\n\n## Overview\n\nPassport.js is the most popular auth middleware for Express with 500+ strategies (Google, GitHub, Facebook, SAML, LDAP, local). Session-based by default, works with JWTs too.\n\n## Instructions\n\n### Step 1: Local Strategy\n\n```typescript\nimport passport from 'passport'\nimport { Strategy as LocalStrategy } from 'passport-local'\nimport bcrypt from 'bcrypt'\n\npassport.use(new LocalStrategy(\n  { usernameField: 'email' },\n  async (email, password, done) => {\n    const user = await db.users.findByEmail(email)\n    if (!user) return done(null, false, { message: 'No account' })\n",
  "cost": {
    "context_tokens": 528
  }
}

Fetch it by URL: GET /api/v1/registry/terminalskills-skills-passport-js/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.