Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2668a6c
commit a119a67
Showing
1 changed file
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
var express = require('express'), | ||
passport = require('passport'), | ||
util = require('util'), | ||
BaseCRMStrategy = require('passport-basecrm').Strategy; | ||
|
||
var BASECRM_CLIENT_ID = "your-client-id", | ||
BASECRM_CLIENT_SECRET = "your-client-secret"; | ||
|
||
// Passport session setup. | ||
// To support persistent login sessions, Passport needs to be able to | ||
// serialize users into and deserialize users out of the session. Typically, | ||
// this will be as simple as storing the user ID when serializing, and finding | ||
// the user by ID when deserializing. However, since this example does not | ||
// have a database of user records, the complete BaseCRM profile is serialized | ||
// and deserialized. | ||
passport.serializeUser(function (user, done) { | ||
done(null, user); | ||
}); | ||
|
||
passport.deserializeUser(function (obj, done) { | ||
done(null, obj); | ||
}); | ||
|
||
// Use the BaseCRMStrategy within Passport. | ||
// Strategies in Passport require a `verify` function, which accept | ||
// credentials (in this case, an accessToken, refreshToken, and BaseCRM | ||
// profile), and invoke a callback with a user object. | ||
passport.use(new BaseCRMStrategy({ | ||
clientID: BASECRM_CLIENT_ID, | ||
clientSecret: BASECRM_CLIENT_SECRET, | ||
callbackURL: "http://127.0.0.1:3000/auth/basecrm/callback" | ||
}, | ||
function (accessToken, refreshToken, profile, done) { | ||
// asynchronous verification, for effect... | ||
process.nextTick(function () { | ||
|
||
// To keep the example simple, the user's BaseCRM profile is returned to | ||
// represent the logged-in user. In a typical application, you would want | ||
// to associate the BaseCRM account with a user record in your database, | ||
// and return that user instead. | ||
return done(null, profile); | ||
}); | ||
} | ||
)); | ||
|
||
var app = express.createServer(); | ||
|
||
// configure Express | ||
app.configure(function () { | ||
app.set('views', __dirname + '/views'); | ||
app.set('view engine', 'ejs'); | ||
app.use(express.logger()); | ||
app.use(express.cookieParser()); | ||
app.use(express.bodyParser()); | ||
app.use(express.methodOverride()); | ||
app.use(express.session({ secret: 'keyboard cat' })); | ||
// Initialize Passport! Also use passport.session() middleware, to support | ||
// persistent login sessions (recommended). | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
app.use(app.router); | ||
app.use(express.static(__dirname + '/public')); | ||
}); | ||
|
||
app.get('/', function (req, res) { | ||
res.render('index', { user: req.user }); | ||
}); | ||
|
||
app.get('/account', ensureAuthenticated, function (req, res) { | ||
res.render('account', { user: req.user }); | ||
}); | ||
|
||
app.get('/login', function (req, res) { | ||
res.render('login', { user: req.user }); | ||
}); | ||
|
||
// GET /auth/basecrm | ||
// Use passport.authenticate() as route middleware to authenticate the | ||
// request. The first step in BaseCRM authentication will involve redirecting | ||
// the user to getbase.com. After authorization, BaseCRM will redirect the user | ||
// back to this application at /auth/basecrm/callback | ||
app.get('/auth/basecrm', | ||
passport.authenticate('basecrm'), | ||
function (req, res) { | ||
// The request will be redirected to BaseCRM for authentication, so this | ||
// function will not be called. | ||
}); | ||
|
||
// GET /auth/basecrm/callback | ||
// Use passport.authenticate() as route middleware to authenticate the | ||
// request. If authentication fails, the user will be redirected back to the | ||
// login page. Otherwise, the primary route function function will be called, | ||
// which, in this example, will redirect the user to the home page. | ||
app.get('/auth/basecrm/callback', | ||
passport.authenticate('basecrm', { failureRedirect: '/login' }), | ||
function (req, res) { | ||
res.redirect('/'); | ||
}); | ||
|
||
app.get('/logout', function (req, res) { | ||
req.logout(); | ||
res.redirect('/'); | ||
}); | ||
|
||
app.listen(3000); | ||
|
||
// Simple route middleware to ensure user is authenticated. | ||
// Use this route middleware on any resource that needs to be protected. If | ||
// the request is authenticated (typically via a persistent login session), | ||
// the request will proceed. Otherwise, the user will be redirected to the | ||
// login page. | ||
function ensureAuthenticated(req, res, next) { | ||
if (req.isAuthenticated()) { | ||
return next(); | ||
} | ||
res.redirect('/login'); | ||
} |