Skip to content

Commit

Permalink
Merge a128e6e into 4b1a5fe
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Sep 23, 2014
2 parents 4b1a5fe + a128e6e commit f60014e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage
test
.jshint*
.travis.yml
Makefile
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
TESTS = test/*.test.js
REPORTER = tap
REPORTER = spec
TIMEOUT = 3000
MOCHA_OPTS =
REGISTRY = --registry=http://r.cnpmjs.org
REGISTRY = --registry=https://registry.npm.taobao.org

install:
@npm install $(REGISTRY) \
--disturl=http://dist.cnpmjs.org
--disturl=https://npm.taobao.org/dist

jshint: install
@-./node_modules/.bin/jshint ./
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ koa-userauth
[gittip-url]: https://www.gittip.com/dead-horse/



`koa` user auth abstraction layer middleware.

## Install
Expand All @@ -44,7 +43,8 @@ var app = koa();
app.keys = ['i m secret'];

app.use(session());
app.use(userauth('/user', {
app.use(userauth({
match: '/user',
// auth system login url
loginURLForamter: function (url) {
return 'http://login.demo.com/login?redirect=' + url;
Expand All @@ -64,8 +64,10 @@ app.use(userauth('/user', {
/**
* User auth middleware.
*
* @param {String|Regex|Function(pathname, ctx)} match, detect which url need to check user auth.
* @param {Object} [options]
* - {String|Regex|Function(pathname, ctx)} match, detect which url need to check user auth.
* - {String|Regex|Function(pathname, ctx)} ignore, detect which url no need to check user auth.
* If `match` exists, this argument will be ignored.
* - {Function(url, rootPath)} loginURLForamter, format the login url.
* - {String} [rootPath], custom app url root path, default is '/'.
* - {String} [loginPath], default is '/login'.
Expand Down
32 changes: 28 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ var defaultOptions = {
*/

module.exports = function (match, options) {
// userauth(options)
if (arguments.length === 1) {
options = match;
match = null;
}
options = options || {};
copy(defaultOptions).to(options);

Expand All @@ -64,18 +69,37 @@ module.exports = function (match, options) {
options.getUser = options.getUser;
options.redirectHandler = options.redirectHandler || defaultRedirectHandler;

match = options.match || match;
var ignore = options.ignore;

// need login checker
var needLogin = match;
if (is.string(match)) {
needLogin = match = route(match);
}
if (is.regExp(match)) {
needLogin = route(match);
} else if (is.regExp(match)) {
needLogin = function (path) {
return match.test(path);
};
}

if (!is.function(needLogin)) {
needLogin = function () {};
if (is.string(ignore)) {
var pathMatch = route(ignore);
needLogin = function (path) {
return !pathMatch(path);
};
} else if (is.regExp(ignore)) {
needLogin = function (path) {
return !ignore.test(path);
};
} else if (is.function(ignore)) {
needLogin = function (path) {
return !ignore(path);
};
} else {
// ignore all
needLogin = function () {};
}
}

options.loginCallback = options.loginCallback || defaultLoginCallback;
Expand Down
7 changes: 4 additions & 3 deletions test/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ var session = require('koa-generic-session');
var userauth = require('..');
var route = require('koa-route');

module.exports = function(match) {

module.exports = function(match, ignore) {
var app = koa();
app.keys = ['i m secret'];
app.use(session());
Expand All @@ -33,7 +32,9 @@ module.exports = function(match) {
}
});

app.use(userauth(match, {
app.use(userauth({
match: match,
ignore: ignore,
loginURLForamter: function (url) {
return '/mocklogin?redirect=' + url;
},
Expand Down
85 changes: 85 additions & 0 deletions test/ignore.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**!
* koa-userauth - test/ignore.test.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var request = require('supertest');
var koa = require('koa');
var session = require('koa-generic-session');
var route = require('koa-route');
var pedding = require('pedding');
var userauth = require('..');
var createApp = require('./app');

describe('ignore.test.js', function () {
it('should ignore /api/xxx', function (done) {
var app = createApp(null, '/api');
request(app)
.get('/api/xxx')
.expect(200)
.expect({
user: null,
message: 'GET /api/xxx'
}, done);
});

it('should ignore /api/xxx regex', function (done) {
var app = createApp(null, /^\/api\//);
request(app)
.get('/api/xxx')
.expect(200)
.expect({
user: null,
message: 'GET /api/xxx'
}, done);
});

it('should ignore /api/xxx when ignore is a function', function (done) {
done = pedding(2, done);
var app = createApp(null, function (path) {
return path.indexOf('/api/') >= 0;
});
request(app)
.get('/api/xxx')
.expect(200)
.expect({
user: null,
message: 'GET /api/xxx'
}, done);

request(app)
.get('/user/xxx')
.expect(302)
.expect('Location', '/login?redirect=%2Fuser%2Fxxx', done);
});

it('should /user 302 when ignore /api', function (done) {
var app = createApp(null, '/api');
request(app)
.get('/user/xxx')
.expect(302)
.expect('Location', '/login?redirect=%2Fuser%2Fxxx', done);
});

it('should not match any path when match and ignore all missing', function (done) {
var app = createApp();
request(app)
.get('/')
.expect(200)
.expect({
user: null,
message: 'GET /'
}, done);
});
});

0 comments on commit f60014e

Please sign in to comment.