loopback#token(middleware.json): err > loopback.token() middleware requires a AccessToken model

218 views
Skip to first unread message

Jorafali

unread,
Dec 2, 2016, 2:24:32 AM12/2/16
to LoopbackJS
Hi, 

I see this piece of code in loopback-example-passport :
// The access token is only available after boot
app.middleware('auth', loopback.token({
  model: app.models.accessToken,
}));

But as I read a few times in the doc, I try to configure things via config file whenever LB seems to allow me to do it. So I tried to configure loopback#token via middleware.json in my own project :
...
"auth": {
    "loopback#token": {
      "params": {
        "model":"accessToken",
        "headers":["LP_accessToken"]
      }
    }
  },
...

I get error like that straight off the bat while trying to register a new customer via the built-in API explorer:
loopback.token() middleware requires a AccessToken model
    at /Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/loopback/server/middleware/token.js:106:5
    at Layer.handle [as handle_request] (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:312:13)
    at /Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:271:10)
    at nosniff (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/dont-sniff-mimetype/index.js:4:5)
    at Layer.handle [as handle_request] (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:312:13)
    at /Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:271:10)
    at ienoopen (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/ienoopen/index.js:4:5)
    at Layer.handle [as handle_request] (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:312:13)
    at /Users/jorafali/Computing_projects/wwwMotto/mottoAPI/node_modules/express/lib/router/index.js:280:7




Here is my access-token.json and customer.json files 
customer.json:
{
  "name": "customer",
  "plural": "customers",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "songs": {
      "type": "hasMany",
      "model": "song",
      "foreignKey": "customerId"
    },
    "accessTokens" : {
      "type":"hasMany",
      "model":"accessToken",
      "foreignKey":"customerId"
    }
  },
  "acls": [],
  "methods": {}
}


access-token.json:
{
  "name": "accessToken",
  "plural": "accessTokens",
  "base": "AccessToken",
  "properties": {},
  "validations": [],
  "relations": {
    "aCustomer": {
      "type": "belongsTo",
      "model": "customer",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": []
}

finally model-config.json:
{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "ACL": {
    "dataSource": "db",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "db",
    "public": false
  },
  "Role": {
    "dataSource": "db",
    "public": false
  },
  "song": {
    "dataSource": "db",
    "public": true
  },
  "customer": {
    "dataSource": "db",
    "public": true
  },
  "accessToken": {
    "dataSource": "db",
    "public": false
  }
}


I looked in loopback module, namely token.js and access-token.js files, 
And I found that when executing the middleware function, somehow it goes and set the TokenModel as the options string. This is happening inside there in token.js:
if (registry === loopback.registry) {
        TokenModel = options.model || loopback.AccessToken;
      }

I cannot figure out what this condition is supposed to achieve to be honest ... Even after looking up the what I this registry is form the API doc.
Any idea anyone ?

Jorafali

unread,
Dec 2, 2016, 6:27:52 AM12/2/16
to LoopbackJS
So just in case you are wondering: 
modifying token.js to assign the actual model to the TokenModel variable inside the above condition fixes the error and I was able to carry on.
Though I still do not understand what that condition achieves and if this is a bug of some sort in LB.

Vipul Sharma

unread,
Apr 6, 2018, 11:04:19 AM4/6/18
to loopb...@googlegroups.com
Yeah, modifying the token.js file made it work. Did you find other solution as it's not right to modify token.js file like this?

Vipul Sharma

unread,
Apr 6, 2018, 11:29:36 AM4/6/18
to LoopbackJS
Just found that the new version doesn't have that code: https://github.com/strongloop/loopback/blob/master/server/middleware/token.js
Reply all
Reply to author
Forward
0 new messages