vinialbano / passport-magic-link

Magic Link authentication for Passport JS
MIT License
60 stars 9 forks source link

passport-magic-link

Magic Link authentication for Passport JS

Build Status Coverage Status

Installation

npm install passport-magic-link

Usage

Configure Strategy

The MagicLinkStrategy constructor receives three parameters:

Example

    const MagicLinkStrategy = require('passport-magic-link').Strategy

    passport.use(new MagicLinkStrategy({
       secret: 'my-secret',
       userFields: ['name', 'email'],
       tokenField: 'token'
    }, (user, token) => {
       return MailService.sendMail({
        to: user.email,
        token
       })
    }, (user) => {
      return User.findOrCreate({email: user.email, name: user.name})
    }))

Authenticate Requests

Use passport.authenticate(), specifying the 'magiclink' strategy for two actions:

request token

In this situation the passport authenticate middleware will send a token produced by the user information, which is returned by the verifyUser function. The delivery system is not provided by default and must be placed in the sendToken function.

  app.post('/auth/magiclink',
      passport.authenticate('magiclink', { action : 'requestToken' }),
      (req, res) => res.redirect('/check-your-inbox')
  )

accept token

In this situation (the default) the passport authenticate middleware will check for a token. The token value is returned by the verifyToken function.

  app.get('/auth/magiclink/callback',
    passport.authenticate('magiclink', { action : 'acceptToken' }),
    (req, res) => res.redirect('/profile')
  )

The options field can also receive some optional properties:

Acknowledgements

This module is forked and modified from Nick Balestra's Passport Zero