Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User.logout() return 500 error if auth token not found #1669

Closed
emgeee opened this issue Sep 13, 2015 · 12 comments
Closed

User.logout() return 500 error if auth token not found #1669

emgeee opened this issue Sep 13, 2015 · 12 comments
Labels

Comments

@emgeee
Copy link

emgeee commented Sep 13, 2015

I think this is related to #1489 but it seems to me that if an auth token is not found, Strongloop should assume the request is unauthorized and return http code 401. Currently, it returns a 500 error which implies the server is broken.

EDIT: upon further inspection of the code, it looks like the error is happening explicitly when User.logout() is called and the access token does not exist in the database. I can see how a 500 error could make sense in this situation but it might make more sense for the method to simply return success.

@superkhau
Copy link
Contributor

@bajtos @raymondfeng @ritch Opinions?

@raymondfeng
Copy link
Member

We have a pending PR at #1496. @ritch Can you finish it?

@emgeee
Copy link
Author

emgeee commented Sep 23, 2015

I don't think PR #1496 quite fixes the issue I'm running into. If User.logout() requires a user to first be logged in (as in, a valid AccessToken exists in the database), is it possible to log a user out without having to manually call window.localStorage.removeItem('$LoopBack$accessTokenId')?

@barocsi
Copy link

barocsi commented Oct 29, 2015

This nature has another bad result, as co-issue comes with Angular JS SDK User.logout(): it simply does not return anything, not even fires the callback. Meaning there is no way to detect the result of the logout nor the nature of the logout in this special case when accessToken had been deleted from the database.

Reference #1081

return User.logout(function (err, result) {
                 // Never receives any result
               });

As a workaround I would try to add a http rejction interceptor for 500s in angular but that would simply bad.

@bajtos
Copy link
Member

bajtos commented Nov 2, 2015

This nature has another bad result, as co-issue comes with Angular JS SDK User.logout(): it simply does not return anything, not even fires the callback. Meaning there is no way to detect the result of the logout nor the nature of the logout in this special case when accessToken had been deleted from the database.

Reference #1081

return User.logout(function (err, result) {
                 // Never receives any result
               });

Assuming the code snippet above is using the Angular client, then it is expected that the callback is not called on error. Angular's ngResource uses two callbacks - a success & an error callback, not a single callback with error as the first arg.

return User.logout(function onSuccess(result){}, function onError(response){});

@barocsi
Copy link

barocsi commented Nov 2, 2015

Great, it works, thanks @bajtos

@superkhau superkhau removed their assignment Nov 2, 2015
@kevintechie
Copy link

A missing or invalid access token should return a 401 per HTTP 1.1 RFC. See my response to related issue: #1496

@doublemarked
Copy link

I see the discussion from #1489 has been making its way through various issues and PRs. I want to again voice the suggestion I placed there - would the correct fix perhaps just to adjust the ACL? We all agree that logout should not be called without an access token. However, the ACL currently allows $everyone:

    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "logout"
    },

Change to $authenticated?

@dkarlovi
Copy link

dkarlovi commented Jun 15, 2016

Any way forward on this? Can I work around this from config somehow, via ACL?

Edit: Overriding in local model ACL doesn't work as expected, other than overriding the remote method, cannot proceed further without a upstream fix.

@seanmangar
Copy link

seanmangar commented Jun 15, 2016

@dkarlovi You can use a mixin to clear the base ACL. The code looks something like this. I got it somewhere around the issues on github but can't remember where.

'use strict';

const path = require('path');
const appRoot = require('app-root-path');

function slugify(name) {
  name = name.replace(/^[A-Z]+/, s => s.toLowerCase());
  return name.replace(/[A-Z]/g, s => '-' + s.toLowerCase());
}

module.exports = (Model, options) => {
  let configFile = options.config;
  if (!configFile) {
    // Works for 99% of cases. For others, set explicit path via options.
    configFile = path.join('./common/models/', slugify(Model.modelName) + '.json');
  }
  const config = appRoot.require(configFile);

  if (!config || !config.acls) {
    console.error('ClearBaseAcls: Failed to load model config from', configFile);
    return;
  }

  Model.settings.acls.length = 0;
  config.acls.forEach(r => Model.settings.acls.push(r));
};

@dkarlovi
Copy link

@seanmangar thanks, will try it out.

But, shouldn't Loopback merge ACL rules by default and allow for overriding them from local model? If it's not done because of security (overriding ACL might lead to unsecured apps), I guess it could emit a warning when in development environment to ensure the dev is not overriding accidentally.

@loay
Copy link
Contributor

loay commented Sep 20, 2016

This issue should have been fixed as per this pr: strongloop/loopback-sdk-angular#178

If the same problem is still persisting, please leave a comment so we can look further. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants