Code Monkey home page Code Monkey logo

passport-magic-link's Introduction

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:

  • options: A javascript object containing some configuration:
    • secret Mandatory string, used to sign tokens
    • userFields: An array of mandatory field names from the request query or body that are going to be used to create or retrieve the user.
    • tokenField: The name of the field which contains the token in the request query or body.
    • ttl: Optional integer, defaults to 10 minutes (in seconds). It's used to set the token expiration
    • passReqToCallbacks: Optional boolean, defaults to false. If true, the request is passed to the sendToken and verifyUser functions.
    • verifyUserAfterToken: Optional boolean, defaults to false. If true, the request data is passed to the token and the user is verified after the token confirmation.
    • storage: Optional token storage instance. Defaults to MemoryStorage.
  • sendToken: A function that is used to deliver the token to the user. You may use an email service, SMS or whatever method you want. It receives the user object, the token and optionally the request. It returns a promise indicating whether the token has been sent or not.
  • verifyUser: A function that receives the request and returns a promise containing the user object. It may be used to insert and/or find the user in the database. It may be executed before the token creation or after the token confirmation.

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:

  • allowReuse: A boolean indicating whether a token can be used more than once. Defaults to false.
  • userPrimaryKey: A string containing the primary key of the user object. This is only used if the token cannot be reused. Defaults to email.
  • tokenAlreadyUsedMessage: A string containing the error message if the token has already been used. Defaults to Token was already used.
app.get('/auth/magiclink/callback',
  passport.authenticate('magiclink', {
    action : 'acceptToken',
    userPrimaryKey: 'id'
   }),
  (req, res) => res.redirect('/profile')
)

Acknowledgements

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

passport-magic-link's People

Contributors

vinialbano avatar pablocaselas avatar imbhargav5 avatar phof avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.