-
-
Save n3ps/0682e8e3323c201e4b84ec7f80983ea1 to your computer and use it in GitHub Desktop.
passport.js sample node server , to allow only user from your company.
Company must be hosted by Google. (eg Google Enterprise, Google Apps)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'); | |
| var http = require('http'); | |
| var path = require('path'); | |
| var passport = require('passport'); | |
| var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy; | |
| var GOOGLE_CLIENT_ID = "xyz1234.apps.googleusercontent.com"; | |
| var GOOGLE_CLIENT_SECRET = "--google client secret"; | |
| // Serialized and deserialized methods when got from session | |
| passport.serializeUser(function(user, done) { | |
| done(null, user); | |
| }); | |
| passport.deserializeUser(function(user, done) { | |
| done(null, user); | |
| }); | |
| // Define a middleware function to be used for every secured routes | |
| var ensureAuthenticated = function(req, res, next){ | |
| if (!req.isAuthenticated()) | |
| res.send(401); | |
| else | |
| next(); | |
| }; | |
| //================================================================== | |
| passport.use(new GoogleStrategy({ | |
| clientID: GOOGLE_CLIENT_ID, | |
| clientSecret: GOOGLE_CLIENT_SECRET, | |
| callbackURL: "http://127.0.0.1:3000/auth/google/return" | |
| }, | |
| function(accessToken, refreshToken, profile, done) { | |
| // asynchronous verification, for effect... | |
| process.nextTick(function () { | |
| // To keep the example simple, the user's Google profile is returned to | |
| // represent the logged-in user. In a typical application, you would want | |
| // to associate the Google account with a user record in your database, | |
| // and return that user instead. | |
| return done(null, profile); | |
| }); | |
| } | |
| )); | |
| // Start express application | |
| var app = express(); | |
| // all environments | |
| app.set('port', process.env.PORT || 3000); | |
| app.set('views', __dirname + '/views'); | |
| app.set('view engine', 'ejs'); | |
| app.use(express.favicon()); | |
| app.use(express.logger('dev')); | |
| app.use(express.cookieParser()); | |
| app.use(express.bodyParser()); | |
| app.use(express.methodOverride()); | |
| app.use(express.session({ secret: 'securedsession' })); | |
| app.use(passport.initialize()); // Add passport initialization | |
| app.use(passport.session()); // Add passport initialization | |
| app.use(app.router); | |
| app.use(express.static(path.join(__dirname, 'public'))); | |
| // development only | |
| if ('development' == app.get('env')) { | |
| app.use(express.errorHandler()); | |
| } | |
| //================================================================== | |
| // routes | |
| app.get('/', function(req, res){ | |
| res.render('index', { title: 'Express' }); | |
| }); | |
| app.get('/users', ensureAuthenticated, function(req, res){ | |
| res.send([{name: "user1"}, {name: "user2"}]); | |
| }); | |
| //================================================================== | |
| //================================================================== | |
| // route to test if the user is logged in or not | |
| app.get('/loggedin', function(req, res) { | |
| res.send(req.isAuthenticated() ? req.user : '0'); | |
| }); | |
| //using "prompt" option in passport-google-oauth | |
| app.get('/login-google', | |
| passport.authenticate('google', {prompt:'select_account', scope: ['https://www.googleapis.com/auth/userinfo.profile', | |
| 'https://www.googleapis.com/auth/userinfo.email'] }), | |
| function(req, res){ | |
| // The request will be redirected to Google for authentication, so this | |
| // function will not be called. | |
| }); | |
| app.get('/auth/google/return', | |
| passport.authenticate('google', { failureRedirect: '/#/loginfailure' }), | |
| function(req, res) { | |
| res.redirect('/'); | |
| }); | |
| // route to log out | |
| app.post('/logout', function(req, res){ | |
| req.logOut(); | |
| res.send(200); | |
| }); | |
| //================================================================== | |
| http.createServer(app).listen(app.get('port'), function(){ | |
| console.log('Express server listening on port ' + app.get('port')); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment