Loopback resolving wrong AuthToken subclass
Consider the following entries in a model-config.json:
"AccessToken": {
"dataSource": "postgres",
"public": false,
"relations": {
"user": {
"type": "belongsTo",
"model": "Admin"
}
}
},
"UDIDToken": {
"dataSource": "postgres",
"public": false,
"relations": {
"user": {
"type": "belongsTo",
"model": "Patient"
}
}
},
"Admin": {
"dataSource": "postgres",
"public": true,
"relations": {
"accessTokens": {
"type": "hasMany",
"model": "AccessToken",
"options": {
"disableInclude": true
}
}
}
},
"Patient": {
"dataSource": "postgres",
"public": true,
"relations": {
"accessTokens": {
"type": "hasMany",
"model": "UDIDToken",
"options": {
"disableInclude": true
}
}
}
}
When I log in as Admin, it creates an AccessToken entry in the database. When trying to log out with that token, the class looked up for a token is UDIDToken, NOT AccessToken. Same goes for other requests (other than logout). Is something missing in my configuration? Or is this a bug?
No mate, is not a bug, you have 2 classes assuming there will be 2 different AccessTokens models. Patient hasMany accessTokens (replacing the name itself when the base is User, that is, everything), and the AccessToken in the Admin model. Well, that's not possible in this framework... yet. The solution to do that is using just one common AccessToken model, but with a polymorphic relation among the user models. In this link -> https://loopback.io/doc/en/lb3/Authentication-authorization-and-permissions.html#access-control-with-multiple-user-models you can find how to implement that. And BE AWARE with the issue #2516
Hope to have answered your question, Sebastián.
Yeah, I dug a bit and found these lines in rest.js:
if (app.isAuthEnabled) {
var AccessToken = registry.getModelByType('AccessToken');
handlers.push(loopback.token({model: AccessToken, app: app}));
}
That's when I realised the current implementation would not support my use case and I had to re-model. Thanks for the response, though. I guess you can close the issue, unless you want to keep it around for implementing a fix?
PS: polymorphic won't fix my specific issue.
Your welcome :). I don't have the privileges to close it, probably you could do it as owner of your issue.
@JuergenSimon Can you share your solution ? . I have a same issue , i don't have a approach .
I haven't implemented one yet, but I'm going to handle it like this:
- Add a custom remote method for logging in the second class of users.
- Get an authentication token from the user super class (in the remote method)
- Handle ACL by using $authenticated
- Log out can be handled normally.
@JuergenSimon
Why must create new method login? When i login by default method , it created a new token and stored to database. It just does not recognize the corresponding token when i login . Get an authentication token from the user super , how can that be done ? Thanks
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
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.
This behaviour should be more explicit. Also, the app should crash with an explicit message instead of just randomly picking one of the several Access Token classes available.