Skip to content

Commit 31db193

Browse files
committed
new example
1 parent 95f9693 commit 31db193

5 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var passport = require('passport');
2+
3+
exports.install = function(framework) {
4+
framework.route('/', view_homepage);
5+
framework.route('/passport/', passport_login_bearear_callback, ['#passport.js']);
6+
};
7+
8+
function view_homepage() {
9+
var self = this;
10+
self.view('homepage');
11+
}
12+
13+
function passport_login_bearear_callback() {
14+
var self = this;
15+
passport.authenticate('bearer', { session: false })(self.req, self.res, function(err) {
16+
17+
// if err = passport answers automatically
18+
if (err)
19+
return;
20+
21+
self.json(self.user);
22+
});
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var passport = require('passport');
2+
var BearerStrategy = require('passport-http-bearer').Strategy;
3+
var users = [{ id: 1, username: 'bob', token: '123456789', email: 'bob@example.com' }, { id: 2, username: 'joe', token: 'abcdefghi', email: 'joe@example.com' }];
4+
5+
function findByToken(token, fn) {
6+
for (var i = 0, len = users.length; i < len; i++) {
7+
var user = users[i];
8+
if (user.token === token)
9+
return fn(null, user);
10+
}
11+
return fn(null, null);
12+
}
13+
14+
passport.use(new BearerStrategy({}, function(token, done) {
15+
process.nextTick(function () {
16+
findByToken(token, function(err, user) {
17+
if (err)
18+
return done(err);
19+
20+
if (!user)
21+
return done(null, false);
22+
23+
return done(null, user);
24+
});
25+
});
26+
}));
27+
28+
framework.middleware('passport.js', passport.initialize());

passport.js-bearer-local/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ===================================================
2+
// IMPORTANT: only for development
3+
// total.js - web application framework for node.js
4+
// http://www.totaljs.com
5+
// ===================================================
6+
7+
require('total.js').http('debug');

passport.js-bearer-local/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Installation
2+
3+
- `npm install passport`
4+
- `npm install passport-http-bearer`
5+
- `npm install passport-local`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@{layout('')}
2+
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<title>Passport.js</title>
7+
<meta charset="utf-8" />
8+
<meta http-equiv="X-UA-Compatible" content="IE=10" />
9+
<meta name="format-detection" content="telephone=no"/>
10+
<meta name="viewport" content="width=1024, user-scalable=yes" />
11+
<meta name="robots" content="all,follow" />
12+
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
13+
<style type="text/css">
14+
body { padding: 50px; margin: 0; font:normal 12px Arial; color: gray }
15+
p { background-color: #F0F0F0; padding: 5px; font:bold 15px Arial; }
16+
</style>
17+
</head>
18+
<body>
19+
20+
<a href="/passport/?access_token=123456789">@{host()}/passport/?access_token=123456789</a>
21+
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)