Skip to content

Commit

Permalink
compatibility: userauth("", options) and improve ctx.session not exis…
Browse files Browse the repository at this point in the history
…ts case
  • Loading branch information
fengmk2 committed Sep 26, 2014
1 parent 9d0bed9 commit c139bb3
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 44 deletions.
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -17,6 +17,7 @@ test:
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require should \
--require should-http \
$(MOCHA_OPTS) \
$(TESTS)

Expand All @@ -27,6 +28,7 @@ test-cov:
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require should \
--require should-http \
$(MOCHA_OPTS) \
$(TESTS)

Expand All @@ -38,6 +40,7 @@ test-travis:
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
--require should \
--require should-http \
$(MOCHA_OPTS) \
$(TESTS)

Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -60,12 +60,16 @@ app.use(userauth({

### Arguments

If `options.match` or `options.ignore` is `String` instance,
we will use [path-match](https://github.com/expressjs/path-match) transfer it to `Regex` instance.

```js
/**
* User auth middleware.
*
* @param {Object} [options]
* - {String|Regex|Function(pathname, ctx)} match, detect which url need to check user auth.
* `''` empty string meaning match all, @see `path-match` package.
* - {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.
Expand Down
55 changes: 37 additions & 18 deletions index.js
@@ -1,7 +1,12 @@
/*!
/**!
* koa-userauth - index.js
* Copyright(c) 2014 dead_horse <dead_horse@qq.com>
*
* Copyright(c) koajs and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';
Expand Down Expand Up @@ -32,7 +37,7 @@ var defaultOptions = {
* User auth middleware.
*
* @param {String|Regex|Function(pathname, ctx)} match, detect which url need to check user auth.
* @param {Object} [options]
* @param {Object} options
* - {Function(url, rootPath)} loginURLForamter, format the login url.
* - {String} [rootPath], custom app url root path, default is '/'.
* - {String} [loginPath], default is '/login'.
Expand All @@ -47,13 +52,13 @@ var defaultOptions = {
* @public
*/

module.exports = function (match, options) {
// userauth(options)
if (arguments.length === 1) {
options = match;
match = null;
module.exports = function (options) {
// userauth(match, options)
if (arguments.length === 2) {
options = arguments[1];
options.match = arguments[0];
}
options = options || {};

copy(defaultOptions).to(options);

options.loginCallbackPath = options.loginCallbackPath
Expand All @@ -69,7 +74,7 @@ module.exports = function (match, options) {
options.getUser = options.getUser;
options.redirectHandler = options.redirectHandler || defaultRedirectHandler;

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

// need login checker
Expand Down Expand Up @@ -99,6 +104,7 @@ module.exports = function (match, options) {
} else {
// ignore all
needLogin = function () {};
debug('ignore all paths');
}
}

Expand All @@ -123,12 +129,23 @@ module.exports = function (match, options) {
*/

return function* userauth(next) {

debug('url: %s, loginPath: %s, session exists: %s',
this.url, options.loginPath, !!this.session);
var loginRequired = !!needLogin(this.path);
debug('url: %s, path: %s, loginPath: %s, session exists: %s, login required: %s',
this.url, this.path, options.loginPath, !!this.session, loginRequired);

if (!this.session) {
debug('this.session not exists');
// ignore not match path
if (!loginRequired) {
debug('not match needLogin path, %j', this.path);
return yield* next;
}
debug('relogin again');
return yield* loginHandler.call(this, next);
}

// get login path
if (!this.session || this.path === options.loginPath) {
if (this.path === options.loginPath) {
debug('match login path');
return yield* loginHandler.call(this, next);
}
Expand All @@ -146,15 +163,15 @@ module.exports = function (match, options) {
}

// ignore not match path
if (!needLogin(this.path)) {
debug('not match needLogin path, %j', needLogin(this.path));
if (!loginRequired) {
debug('ignore %j', this.path);
return yield* next;
}

if (this.session[options.userField]
&& options.loginCheck(this)) {
// 4. user logined, next() handler
debug('already login');
debug('already logined');
return yield* next;
}

Expand All @@ -163,8 +180,9 @@ module.exports = function (match, options) {
try {
user = yield* options.getUser(this);
} catch (err) {
console.error(err.stack);
console.error('[koa-userauth] options.getUser error: %s', err.stack);
}

if (!user) {
debug('can not get user');
var ctx = this;
Expand All @@ -186,6 +204,7 @@ module.exports = function (match, options) {

return yield* options.redirectHandler.call(this, nextHandler);
}

debug('get user directly');
var res = yield* options.loginCallback(this, user);
var loginUser = res[0];
Expand Down
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -29,19 +29,20 @@
"autod": "^0.1.3",
"istanbul-harmony": "*",
"jshint": "*",
"koa": "~0.10.0",
"koa": "~0.12.1",
"koa-generic-session": "~1.2.2",
"koa-route": "~2.1.0",
"koa-generic-session": "~1.0.0",
"mm": "~0.2.1",
"mocha": "*",
"pedding": "~1.0.0",
"should": "~3.3.2",
"should": "~4.0.4",
"should-http": "*",
"supertest": "~0.13.0"
},
"dependencies": {
"copy-to": "~1.0.1",
"debug": "~2.0.0",
"is-type-of": "~0.3.1",
"path-match": "~1.2.0"
"path-match": "~1.2.2"
}
}

0 comments on commit c139bb3

Please sign in to comment.