Create a gist now

Instantly share code, notes, and snippets.

Node Box SDK Middleware Example
<!--views/account.ejs-->
<p>ID: <%= user.id %></p>
<p>Name: <%= user.name %></p>
//Example adapted from passport-box's login example. (https://github.com/bluedge/passport-box/tree/master/examples/login)
//This example requires express-4.x.
var express = require('express'),
passport = require('passport'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
methodOverride = require('method-override'),
BoxStrategy = require('passport-box').Strategy,
box_sdk = require('box-sdk');
var BOX_CLIENT_ID = "client id"
var BOX_CLIENT_SECRET = "client secret";
var box = box_sdk.Box();
// 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 Box profile is
// serialized and deserialized.
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
// Use the BoxStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and 37signals
// profile), and invoke a callback with a user object.
passport.use(new BoxStrategy({
clientID: BOX_CLIENT_ID,
clientSecret: BOX_CLIENT_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/box/callback"
}, box.authenticate()));
var app = express();
var router = express.Router();
// configure Express
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(morgan());
app.use(cookieParser());
app.use(bodyParser());
app.use(methodOverride());
app.use(session({
secret: 'keyboard cat'
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(router);
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
var opts = {
user: req.user
};
if (req.user) {
var connection = box.getConnection(req.user.login);
connection.ready(function () {
connection.getFolderItems(0, null, function (err, result) {
if (err) {
opts.body = err;
} else {
opts.body = result;
}
res.render('index', opts);
});
});
} else {
res.render('index', opts);
}
});
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/Box
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Box authentication will involve
// redirecting the user to Box.com. After authorization, Box
// will redirect the user back to this application at /auth/box/callback
app.get('/auth/box',
passport.authenticate('box'),
function (req, res) {
// The request will be redirected to Box for authentication, so this
// function will not be called.
});
// GET /auth/box/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/box/callback',
passport.authenticate('box', {
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')
}
<!--views/index.ejs-->
<!DOCTYPE html>
<html>
<head>
<title>Passport-box Example</title>
</head>
<body>
<% if (!user) { %>
<h2>Welcome! Please log in.</h2>
<p>
<a href="/">Home</a> |
<a href="/login">Log In</a>
</p>
<% } else { %>
<h2>Hello, <%= user.name %>.</h2>
<code>
<%= JSON.stringify(body) %>
</code>
<p>
<a href="/">Home</a> |
<a href="/account">Account</a> |
<a href="/logout">Log Out</a>
</p>
<% } %>
</body>
</html>
<!--views/layout.ejs-->
<!DOCTYPE html>
<html>
<head>
<title>Passport-box Example</title>
</head>
<body>
<% if (!user) { %>
<h2>Welcome! Please log in.</h2>
<p>
<a href="/">Home</a> |
<a href="/login">Log In</a>
</p>
<% } else { %>
<h2>Hello, <%= user.displayName %>.</h2>
<p>
<a href="/">Home</a> |
<a href="/account">Account</a> |
<a href="/logout">Log Out</a>
</p>
<% } %>
<%- body %>
</body>
</html>
<!--views/login.ejs-->
<a href="/auth/box">Login with box</a>
@adityamukho
Owner

Please leave questions/comments at https://github.com/adityamukho/node-box-sdk/issues .

Gist comments do not send notifications, so I may not be able to respond on time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment