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

passport-remember-me-session

v1.0.0

Published

Remember Me cookie authentication strategy for Passport.

Downloads

1

Readme

Passport-Remember Me Session

Extends the passport-remember-me strategy to include support for getting rememberMe cookie from sessions (in case you use cookie-session) because previously you couldn't use this strategy without cookie-parser, which was decoding the cookie twice if you were already using cookie-session. Also updates 7-year-old dependencies to modern versions.

Passport strategy for authenticating based on a remember me cookie.

This module lets you authenticate using a remember me cookie (aka persistent login) in your Node.js applications. By plugging into Passport, remember me authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Install

$ npm install passport-remember-me

Usage

Configure Strategy

The remember me authentication strategy authenticates users using a token stored in a remember me cookie. The strategy requires a verify callback, which consumes the token and calls done providing a user.

The strategy also requires an issue callback, which issues a new token. For security reasons, remember me tokens should be invalidated after being used. The issue callback supplies a new token that will be stored in the cookie for next use.

passport.use(
  new RememberMeStrategy(
    function (token, done) {
      Token.consume(token, function (err, user) {
        if (err) {
          return done(err)
        }
        if (!user) {
          return done(null, false)
        }
        return done(null, user)
      })
    },
    function (user, done) {
      var token = utils.generateToken(64)
      Token.save(token, { userId: user.id }, function (err) {
        if (err) {
          return done(err)
        }
        return done(null, token)
      })
    }
  )
)

Authenticate Requests

Use passport.authenticate(), specifying the 'remember-me' strategy, to authenticate requests.

This is typically used in an application's middleware stack, to log the user back in the next time they visit any page on your site. For example:

app.configure(function () {
  app.use(express.bodyParser())
  app.use(require('cookie-session')({ secret: 'keyboard cat' }))
  app.use(passport.initialize())
  app.use(passport.session())
  app.use(passport.authenticate('remember-me'))
  app.use(require('./routes'))
})

Note that passport.session() should be mounted above remember-me authentication, so that tokens aren't exchanged for currently active login sessions.

Setting the Remember Me Cookie

If the user enables "remember me" mode, an initial cookie should be set when they login.

app.post(
  '/login',
  passport.authenticate('local', {
    failureRedirect: '/login',
    failureFlash: true
  }),
  function (req, res, next) {
    // issue a remember me cookie if the option was checked
    if (!req.body.remember_me) {
      return next()
    }

    var token = utils.generateToken(64)
    Token.save(token, { userId: req.user.id }, function (err) {
      if (err) {
        return done(err)
      }
      res.cookie('remember_me', token, {
        path: '/',
        httpOnly: true,
        maxAge: 604800000
      }) // 7 days
      return next()
    })
  },
  function (req, res) {
    res.redirect('/')
  }
)

Security Considerations

If not managed correctly, using a "remember me" cookie for automatic authentication increases a service's exposure to potential security threats. There are a number of techniques to reduce and mitigate these threats, and it is a matter of application-level policy to asses the level of risk and implement appropriate counter measures.

The following list is recommended reading for understanding these risks:

Examples

For a complete, working example, refer to the login example.

Tests

$ npm install
$ make test

Build Status

Credits

License

The MIT License

Copyright (c) 2013 Jared Hanson <http://jaredhanson.net/> Copyright (c) 2020 Jane Jeon <http://janejeon.dev/>