Giter Club home page Giter Club logo

passport-autoconfigurator's Introduction

passport-autoconfigurator

passport-autoconfigurator is a utility to reduce code when you need to use passport.

Requirements

  • npm and Node.js

Import module

  • Execute :
npm install passport-autoconfigurator --save
  • Or manually add this to your package.json dependencies:
"dependencies": {
    "passport-autoconfigurator": "1.0.0"
}

Usage

Imagine a simple express app

var express = require('express');
var app = express();

app.get('/home', function(req, res) {
  res.type('text/plain');
  res.send('Hell , its about time!!');
});

app.listen(process.env.PORT || 8080);

With a correct session :

app.use(session({
	secret: '123456789',
	resave: true,
	saveUninitialized: true,
	cookie: {
		maxAge: (45 * 60 * 1000)
	}
}));

This works perfectly, but you need to protect /home. So only users logged in google are allowed.

To do that, just add this lines, before app.listen ...:

var passportAutoconfigurator = require('passport-autoconfigurator');

var options = {
  google: {
    client: {
      id: 'abcdreeghij.apps.googleusercontent.com',
      secret: 'abcdreeghij',
      callbackUrl: 'http://localhost:8080/auth/google/callback',
      scope: ['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile']
    }
  },
  express : {
    loginRoute : '/auth/google',
    callbackRoute : '/auth/google/callback',
    failureRedirectRoute : '/login'
  }
};


var googlePasssport = new passportAutoconfigurator.GooglePassportAutoconfigurator(options,app);

and add googlePasssport.ensureAuthenticated to /home route:

app.get('/home',googlePasssport.ensureAuthenticated, function(req, res) {
  res.type('text/plain');
  res.send('Hell , its about time!!');
});

Next time, when user access to localhost:8080/home will be redirected to google login form.

passport-autoconfigurator's People

Contributors

jrichardsz avatar

Watchers

James Cloos avatar  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.