Skip to content

Instantly share code, notes, and snippets.

@nowelium
Created October 17, 2011 13:43
Show Gist options
  • Save nowelium/1292633 to your computer and use it in GitHub Desktop.
Save nowelium/1292633 to your computer and use it in GitHub Desktop.
connect-auth example (using: multi strategy, multi scope)
const express = require('express');
const auth = require('connect-auth');
const app = express.createServer();
// see: https://gist.github.com/1292608
(function (){
// yet another google oauth2
Object.defineProperty(auth, 'GoogleV2', {
get: function() {
return require('./lib/connect-oauth-google-v2/google2');
},
enumerable:true
});
})();
app.use(auth([
auth.Facebook({
appId: '...',
appSecret: '...',
callback: '/scopeA/callback'
scope: 'email, publish_stream, offline_access, read_stream',
name: 'scopeA'
}),
auth.GoogleV2({
appId: '...',
appSecret: '...',
callback: '/scopeB/callback',
scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
name: 'scopeB'
}),
auth.Facebook({
appId: '...',
appSecret: '...',
callback: '/scopeC/callback'
scope: 'email, publish_stream, offline_access, read_stream',
name: 'scopeC'
}),
auth.GoogleV2({
appId: '...',
appSecret: '...',
callback: '/scopeD/callback',
scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
name: 'scopeD'
})
]));
app.createProtection = function(scopeKey, authenticateCallback){
return function(req, res, next){
if(req.isAuthenticated(scopeKey)){
return next();
}
req.getAuthScopeData = function (){
return req.getAuthDetails().scopedUsers[scopeKey];
};
return req.authenticate(scopeKey, {scope: scopeKey}, function(err, authenticated){
if(err){
return next(err);
}
if(authenticated){
return authenticateCallback(req, res, function(err){
if(err){
return next(err);
}
return next();
});
}
});
};
};
(function (){
const protect = app.createProtection('scopeA', function(req, res, next){
console.log(req.getAuthScopeData());
});
app.get('/scopeA/callback', protect, function(req, res){
return res.send('scopeA');
});
})();
(function (){
const protect = app.createProtection('scopeB', function(req, res, next){
console.log(req.getAuthScopeData());
});
app.get('/scopeB/callback', protect, function(req, res){
return res.send('scopeB');
});
})();
(function (){
const protect = app.createProtection('scopeC', function(req, res, next){
console.log(req.getAuthScopeData());
});
app.get('/scopeC/callback', protect, function(req, res){
return res.send('scopeC');
});
})();
(function (){
const protect = app.createProtection('scopeD', function(req, res, next){
console.log(req.getAuthScopeData());
});
app.get('/scopeD/callback', protect, function(req, res){
return res.send('scopeD');
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment