koajs / userauth

koa user auth middleware
MIT License
137 stars 18 forks source link

needLogin判断逻辑纠正 #12

Closed huqingliang closed 8 years ago

huqingliang commented 8 years ago
return function* userauth(next) {
    var loginRequired = !!needLogin(this.path, this);
    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);
    }

if (!this.session) 这个判断逻辑能否改成

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

因为needLogin这个判断逻辑本身就是让业务方自己去判断是否要走登录校验,如果这里加了只允许已经登录过的逻辑才能走进来,就不太合理了。