Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Node Box SDK Middleware Example

View account.ejs
1 2 3 4
<!--views/account.ejs-->
 
<p>ID: <%= user.id %></p>
<p>Name: <%= user.name %></p>
View account.ejs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
//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')
}
View account.ejs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<!--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>
View account.ejs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<!--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>
View account.ejs
1 2 3
<!--views/login.ejs-->
 
<a href="/auth/box">Login with box</a>
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
Something went wrong with that request. Please try again.