strongloop / loopback-example-passport

LoopBack example for facebook login
Other
185 stars 134 forks source link

moves middleware functions in server.js to middleware.json #57

Closed sanori closed 7 years ago

sanori commented 8 years ago

static page handler, error page rendering, and error handler can be configured in middleware.json as follows:

  "files": {
      "loopback#static": {
      "params": "$!../client/public"
    }
  },
  "final": {
    "loopback#urlNotFound": {}
  },
  "final:after": {
    "errorhandler": {}
  }

Currently, it seems that app.middleware() calls in server.js and configuration of middleware.json is mixed.

slnode commented 8 years ago

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

sanori commented 8 years ago

This patch upgrades this example to use phases feature of loopback.io https://docs.strongloop.com/display/public/LB/Upgrading+applications+to+use+phases I think this resolves issue #56 , #39 I have consulted the answer in https://github.com/strongloop/loopback-example-middleware/issues/4 . Thanks @hacksparrow.

I tried to remove most of app.middleware calls in server.js. But, I cannot change

app.middleware('auth', loopback.token({
  model: app.models.accessToken
}));

to middleware.json entry.

Does anyone solve this problem?

bajtos commented 8 years ago

@loay please review.

@raymondfeng @superkhau PTAL too

bajtos commented 8 years ago

@sanori thank you for the pull request.

I tried to remove most of app.middleware calls in server.js. But, I cannot change

app.middleware('auth', loopback.token({
  model: app.models.accessToken
}));

to middleware.json entry.

I believe you can reference the model by name in middleware.json, e.g.

{
  "auth": {
    "loopback#token": {
      "params": { "model": "accessToken" }
    }
  }
}
bajtos commented 8 years ago

@slnode ok to test

sanori commented 8 years ago

@bajtos Thank you for your suggestion. But, in my case, it makes the following error

AssertionError: loopback.token() middleware requires a AccessToken model
    at C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\loopback\server\middleware\token.js:96:5
    at Layer.handle [as handle_request] (C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\loopback\node_mo
dules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\loopback\node_modules\express\lib\rou
ter\index.js:312:13)
    at C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\loopback\node_modules\express\lib\router\index.js:
280:7
    at Function.process_params (C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\loopback\node_modules\exp
ress\lib\router\index.js:330:12)
    at next (C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\loopback\node_modules\express\lib\router\ind
ex.js:271:10)
    at SessionStrategy.strategy.pass (C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\passport\lib\middle
ware\authenticate.js:318:9)
    at SessionStrategy.authenticate (C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\passport\lib\strateg
ies\session.js:67:10)
    at attempt (C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\passport\lib\middleware\authenticate.js:3
41:16)
    at authenticate (C:\Users\jwjung\Documents\Workspace\loopback-example-passport\node_modules\passport\lib\middleware\authenticate
.js:342:7)

I guess, when loading middleware.json, loopback could not access app.models.accessToken

superkhau commented 8 years ago

LGTM except for the access token error

loay commented 8 years ago

+1 getting as well the error AssertionError: loopback.token() middleware requires a AccessToken model

slnode commented 8 years ago

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

cicerohen commented 8 years ago

Hey guys, I'm having the same problem.

not work

"auth": { "loopback#token": { "params": { "model": "accessToken", "currentUserLiteral": true } } },

work !! app.middleware('auth', app.loopback.token({ model: app.models.accessToken, currentUserLiteral: 'me' }));

slnode commented 8 years ago

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

sanori commented 8 years ago

I guess that there is no way to set auth token as user defined model in middleware.json, currently. That is, this PR is the best possible way to move middleware functions to middleware.json, I guess. Is it right?

stale[bot] commented 7 years ago

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.